Html 将元素从容器中分离出来
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11723417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Break element out of container
提问by Shpigford
I have all my content wrapped in a container element with a fixed width.
我将所有内容都包裹在一个具有固定宽度的容器元素中。
But I have a <div>
that I want to "break out" of that container to span the full width of the page.
但是我有一个<div>
我想“突破”该容器以跨越页面的整个宽度。
http://dabblet.com/gist/3207168
http://dabblet.com/gist/3207168
As you can see in that example, I've got the <div>
to break out, but since it's positioned absolutely, it doesn't affect the flow of the page...which is what I'd like it to do.
正如您在该示例中看到的那样,我已经获得了<div>
突破口,但由于它是绝对定位的,因此不会影响页面的流动……这正是我希望它做的。
I want it to act like it's in the flow of the page, but expand the full width of the window.
我希望它表现得像在页面流中一样,但扩展了窗口的整个宽度。
回答by fboes
Another idea, which in modern browsers does stretch the .breakout
only to the width of the browser window:
另一个想法,在现代浏览器中确实.breakout
只将其拉伸到浏览器窗口的宽度:
body, html {
overflow-x: hidden;
margin: 0;
padding: 0;
}
div {
padding:0.5em;
}
.container {
width:300px;
background-color:rgba(255,255,0,0.5);
margin:auto;
}
.breakout {
margin:1em -100%; /* old browsers fallback */
margin:1em calc(50% - 50vw);
background-color:rgba(255,0,255,0.5)
}
<div class="container">
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
<div class="breakout">
Break out of container
</div>
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
</div>
Edit:The one and only Chris Coyier explains Full Width Containers in Limited Width Parents, which is somewhat similiar.
编辑:唯一的 Chris Coyier 解释了有限宽度父母中的全宽容器,这有点相似。
回答by voodoo417
Maybe http://jsfiddle.net/sYv9g/1/?
也许http://jsfiddle.net/sYv9g/1/?
.wrapper {
width:300px;
margin:0 auto;
background:yellow;
}
.break {
text-align:center;
font-weight:bold;
background:rgba(255,0,0, 0.5);
margin-left:-100%;
margin-right:-100%;
}
<div class="wrapper">
<h1>Ipsum Dapibus Pellentesque Pharetra</h1>
<p>Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Etiam porta sem malesuada magna mollis euismod. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Morbi leo risus, porta ac consectetur ac, vestibulum at eros. Sed posuere consectetur est at lobortis.</p>
<div class="break">This should be full width</div>
<p>Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor. Maecenas sed diam eget risus varius blandit sit amet non magna. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.</p>
<div class="break">This should be full width</div>
<p>Nullam id dolor id nibh ultricies vehicula ut id elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas faucibus mollis interdum. Donec id elit non mi porta gravida at eget metus. Donec ullamcorper nulla non metus auctor fringilla.</p>
<p>Vestibulum id ligula porta felis euismod semper. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur blandit tempus porttitor.</p>
</div>
回答by Zachary Kniebel
You can't actually make it break out of the wrapping div, but you can simulate this same effect by having the wrapping div be the full width of the page, and by wrapping the other elements on that page that are not supposed to be in your "break out" div inside a div that has a yellow background and 300px width.
您实际上无法使其脱离包装 div,但您可以通过将包装 div 设为页面的全宽,并通过包装该页面上不应该出现的其他元素来模拟相同的效果您在具有黄色背景和 300px 宽度的 div 中的“突破”div。
回答by smilly92
You could use negative margins to achieve this.
您可以使用负边距来实现这一点。
.wrapper {
width:300px;
margin:0 auto;
}
.break {
width: 600px;
margin-left:-150px;
}
The only problem is that you would need to specify the width of both elements.
唯一的问题是您需要指定两个元素的宽度。
EDIT: voodoo417's solution would be better...
编辑:voodoo417 的解决方案会更好......
回答by kraftner
Building on top of voodoo417's solution with just adding a second wrapper and some slight modifications this can be done properly:
建立在 voodoo417 的解决方案之上,只需添加第二个包装器和一些轻微的修改,就可以正确完成:
.outer-wrapper {
overflow:hidden;
min-width:300px;
}
.wrapper {
width:300px;
margin:0 auto;
background:yellow;
}
.break {
text-align:center;
font-weight:bold;
background:rgba(255,0,0, 0.5);
margin-left:-9999px;
margin-right:-9999px;
}
<div class="outer-wrapper">
<div class="wrapper">
<h1>Ipsum Dapibus Pellentesque Pharetra</h1>
<div class="break">This should be full width</div>
</div>
</div>
Here is the adapted jsfiddle:
这是改编的 jsfiddle:
回答by CodeBiker
I like this simple technique (thanks to Bust elements out of their containers with one line of CSS):
我喜欢这种简单的技术(感谢Bust 元素用一行 CSS 从它们的容器中取出):
margin: 0 calc(50% - 50vw)