我有以下标记,其中#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;
我试过了:
答案 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%);
}
答案 2 :(得分:1)
根据您需要的浏览器支持,这可能非常简单,也可能需要一些棘手的CSS。
一个简单版本使用vw
(viewport width unit)decent 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
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;
答案 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;
}