制作一个像祖父母一样宽的元素

时间:2015-04-30 09:27:48

标签: html css

我有以下标记,其中#content宽度为80%且包含.slide个元素。我希望幻灯片和祖父母一样宽(即本例中的正文)。这是我的标记,无法更改



body {
  margin: 0;
  font: medium monospace;
  background: lightgray;
}
#content {
  margin: auto;
  width: 80%;
  background: white;
}
#content:before,
#content:after {
  content: "";
  display: table;
}
.slide {
  height: 6em;
  background: indianred;
}

<div id="content">
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
  <blockquote>
    <p>Phasellus euismod dolor imperdiet!</p>
  </blockquote>
  <div class="slide">Donec mauris tellus</div>
  <p>Pellentesque sit amet venenatis diam, at interdum tortor.</p>
  <ul>
    <li>Quisque ornare mi in pharetra porttitor.</li>
    <li>Nulla ultrices quam nec vehicula porta.</li>
  </ul>
</div>
&#13;
&#13;
&#13;

我试过了:

  • 相对绝对定位,需要修复幻灯片的高度(幻灯片包含可变长度文本)
  • 在段落上设置80%的宽度而不是内容,但这不是优雅的(内容包含不能有80%宽度或10%左边距的元素)

6 个答案:

答案 0 :(得分:5)

如果您周围的内容可以自己要求不同的定位属性组合,您可以随时使用以下内容。

body {
  margin: 0;
  font: medium monospace;
  background: lightgray;
}
#content {
  margin: auto;
  width: 80%;
  background: white;
}
#content:before,
#content:after {
  content: "";
  display: table;
}
.slide {
  height: 6em;
  background: indianred;
  width: 125%; /*100*(100/80)*/
  margin-left: 50%;
  transform: translateX(-50%);
}
<div id="content">
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
  <div class="slide">Donec mauris tellus</div>
  <p>Pellentesque sit amet venenatis diam, at interdum tortor.</p>
</div>

答案 1 :(得分:1)

你可以试试这个:

.slide {
  height: 6em;
  background: indianred;
  width: 100vw;
  margin-left: 50%;
  transform: translateX(-50%);
}

JSFiddle Demo

答案 2 :(得分:1)

根据您需要的浏览器支持,这可能非常简单,也可能需要一些棘手的CSS。

一个简单版本使用vwviewport width unitdecent support并且需要对您的设置进行一些简单的修正:

body {
  margin: 0;
  font: medium monospace;
  background: lightgray;
}
#content {
  margin: auto;
  width: 80vw;
  background: white;
}
#content:before,
#content:after {
  content: "";
  display: table;
}
.slide {
  width: 100vw;
  height: 6em;
  margin-left: -10vw;
  background: indianred;
}
<div id="content">
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
  <div class="slide">Donec mauris tellus</div>
  <p>Pellentesque sit amet venenatis diam, at interdum tortor.</p>
</div>

答案 3 :(得分:1)

尝试使用:

body {
  margin: 0;
  font: medium monospace;
  background: lightgray;
  overflow: hidden;
}
#content {
  margin: auto;
  width: 80%;
  background: white;
}
#content:before,
#content:after {
  content: "";
  display: table;
}
.slide {
  height: 6em;
  background: indianred;
  width: 100vw;
  transform: translateX(-10%);
}
<div id="content">
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
  <blockquote>
    <p>Phasellus euismod dolor imperdiet!</p>
  </blockquote>
  <div class="slide">Donec mauris tellus</div>
  <p>Pellentesque sit amet venenatis diam, at interdum tortor.</p>
  <ul>
    <li>Quisque ornare mi in pharetra porttitor.</li>
    <li>Nulla ultrices quam nec vehicula porta.</li>
  </ul>
</div>

答案 4 :(得分:1)

您可以使用带有相应填充的负边距+ box-sizing:border-box

FIDDLE

&#13;
&#13;
body {
  margin: 0;
  font: medium monospace;
  background: lightgray;
}
#content {
  margin: auto;
  width: 80%;
  background: white;
}
#content:before,
#content:after {
  content: "";
  display: table;
}
.slide {
  height: 6em;
  background: indianred;
  margin: 0 -12.5%; /* 20% of body = 25% of 80% content... so 12.5% on each side */
  padding: 0 12.5%;
  box-sizing: border-box;
}
&#13;
<div id="content">
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
  <blockquote>
    <p>Phasellus euismod dolor imperdiet!</p>
  </blockquote>
  <div class="slide">Donec mauris tellus</div>
  <p>Pellentesque sit amet venenatis diam, at interdum tortor.</p>
  <ul>
    <li>Quisque ornare mi in pharetra porttitor.</li>
    <li>Nulla ultrices quam nec vehicula porta.</li>
  </ul>
</div>
&#13;
&#13;
&#13;

答案 5 :(得分:0)

请改用它。这是 fiddle

<强> HTML

<div class="content">
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>

<div class="slide">Donec mauris tellus</div>

<div class="content">
  <p>Pellentesque sit amet venenatis diam, at interdum tortor.</p>
</div>

CSS

body {
  margin: 0;
  font: medium monospace;
  background: lightgray;
}
.content {
  margin: auto;
  width: 80%;
  background: white;
}
.content:before,
.content:after {
  content: "";
  display: table;
}
.slide {
  height: 6em;
  background: indianred;
}