简体   繁体   English

将子元素宽度扩展到父容器之外

[英]Expand child element width beyond parent container

I have webpage with Bootstrap responsive design. 我的网页具有Bootstrap响应式设计。 I have added padding from left and right to wrapper, so on mobile there will be some spaces between edge. 我从左到右添加了填充到包装器,因此在移动设备上,边缘之间会有一些间隔。 But my expanding menu has some background color, that must take full browser width. 但是我正在扩展的菜单具有一些背景色,必须使用完整的浏览器宽度。 How can i make it? 我该怎么做? I can make that with negative margin, but it will not expand background color. 我可以用负边距制作,但不会扩展背景色。 Is there any way to do that without changing layout? 有什么办法可以做到而又不改变布局?

 #wrapper { box-sizing: border-box; /* responsive, so it's real 'width: 100%' */ width: 150px; height: 150px; padding: 0 10px; background-color: #ddd; border: 1px solid green; } #wrapper .div { background-color: #afa; width: 100%; height: 90px; border: 1px solid red; box-sizing: border-box; margin-bottom: 10px; } #wrapper .div2 { width: 100%; height: 50px; border: 1px solid red; box-sizing: border-box; background-color: #aaf; } 
 <div id="wrapper"> <div class="div">Must expand till green borders</div> <div class="div2">Must stay as is</div> </div> 

You can use negative margins: http://jsfiddle.net/3f1t35xv/ . 您可以使用负边距: http : //jsfiddle.net/3f1t35xv/

Change #wrapper .div to the following: #wrapper .div更改为以下内容:

#wrapper .div {
    background-color: #afa;
    /* width: 100%; */
    height: 90px;
    border: 1px solid red;
    box-sizing: border-box;
    margin: 0 -10px 10px;
}

Here's another (a lot less elegant) way to do it: http://jsfiddle.net/au73dL07/ . 这是另一种方法(不太优雅): http : //jsfiddle.net/au73dL07/

#wrapper .div {
    background-color: #afa;
    height: 90px;
    border: 1px solid red;
    box-sizing: border-box;
    position: relative;
    left: -10px;
    width: calc(100% + 2 * 10px);
    margin-bottom: 10px;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM