简体   繁体   English

在父div上溢出子div背景颜色

[英]Overflow child div background-color over parent div

Is there a way to overflow the background color of a child-div over its parent-div container? 有没有办法使child-div的背景颜色溢出其parent-div容器? I'm trying to add full-screen width background-color but the parent-div has a fixed width. 我正在尝试添加全屏宽度的background-color,但parent-div具有固定的宽度。 here is my HTML structure: 这是我的HTML结构:

<div id="parent">
    <div class="child">PAGE TITLE</div>
</div>

The CSS: CSS:

#parent {
    max-width: 760px;
}

.child {
    width: 100%;
    background-color: red;
}

What you are trying to do is impossible with the current code setup as the child's z-index is set to the same stacking index as its parent as are its dimensions if you are using percentages. 在当前代码设置下,您尝试做的事情是不可能的,因为如果您使用百分比,则将子级的z-index设置为其父级的堆叠索引以及其尺寸。

You will need to do something like this to achieve your desired result. 您将需要执行以下操作才能获得所需的结果。

HTML HTML

<div id="parent">
    <div class="child">PAGE TITLE</div>
</div>

<div id="page"></div>

CSS CSS

#parent {
    max-width: 760px;
    background: green;
}

.child {
    width: 100%;
    background-color: red;
}

#page {
    width: 760px;
    height: 760px;
    background: green;
    position: relative;
    z-index: 1;
}

don't know if this is what you want. 不知道这是不是你想要的。

#parent {
    max-width: 760px;
    margin: 10px auto;
}

.child:before {
    width: 100%;
    height: 24px;
    content: ' ';   
    position: absolute;
    left: 0;
    z-index: -1;
    background-color: green;
}

Note: #parent's background-color must be transparent to make these css work. 注意:#parent的背景色必须透明才能使这些CSS工作。

Check my fiddle 检查我的小提琴

Use box-shadow to simulate the background overflow since it's a solid color: 因为它是纯色,所以使用box-shadow来模拟背景溢出:

 #parent { max-width: 760px; height: 100px; padding: 10px 0; margin:auto; background:blue; } .child { width: 100%; height: 100%; background-color: red; box-shadow: 760px 0 0 red, 1520px 0 0 red, -760px 0 0 red, -1520px 0 0 red; } 
 <div id="parent"> <div class="child">PAGE TITLE</div> </div> 

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

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