断开容器外的元素

时间:2012-07-30 14:19:56

标签: html css position

我将所有内容都包装在一个固定宽度的容器元素中。

但是我有一个<div>,我希望“突破”该容器以跨越页面的整个宽度。

http://dabblet.com/gist/3207168

enter image description here

正如你在那个例子中看到的那样,我有<div>突破,但由于它绝对定位,它不会影响页面的流动...这就是我所说的喜欢这样做。

我希望它在页面流程中表现得像它,但扩展窗口的整个宽度。

7 个答案:

答案 0 :(得分:32)

另一个想法,在现代浏览器中确实只将.breakout拉伸到浏览器窗口的宽度:

&#13;
&#13;
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)
}
&#13;
<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>
&#13;
&#13;
&#13;

编辑:唯一的Chris Coyier解释Full Width Containers in Limited Width Parents,这有点类似。

答案 1 :(得分:31)

也许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>

答案 2 :(得分:4)

你实际上不能让它脱离包装div,但你可以通过让包装div成为页面的整个宽度来模拟同样的效果,并通过将其他元素包装在该页面上不应该在你的&#34;爆发&#34; div内有一个黄色背景和300px宽度的div。

答案 3 :(得分:1)

您可以使用负边距来实现此目的。

.wrapper {
    width:300px;
    margin:0 auto;
}
.break {
    width: 600px;
    margin-left:-150px;
}

唯一的问题是你需要指定两个元素的宽度。

编辑:voodoo417的解决方案会更好......

答案 4 :(得分:1)

建立在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>

这是改编的jsfiddle:

http://jsfiddle.net/v53vv78d/2/

答案 5 :(得分:0)

我喜欢这种简单的技术(感谢Bust elements out of their containers with one line of CSS):

margin: 0 calc(50% - 50vw)

答案 6 :(得分:0)

考虑到容器宽度的更好版本。

.wrapper {
    width:300px;
    margin:0 auto;
    background:yellow;
}
.break {
    text-align:center;
    font-weight:bold;
    background:rgba(255,0,0, 0.5);
    margin-left: calc((-100vw + 300px)*.5);
    margin-right: calc((-100vw + 300px)*.5);
}

https://jsfiddle.net/Lxnmr01h/