绝对位置div没有填充容器div css

时间:2013-11-30 09:48:22

标签: html css html5 css3

我在容器div中有一个absolute positioned div。绝对定位div的背景颜色不会填充容器div。容器div的高度根据内容是动态的。如何使定位div的背景颜色填充它的容器div?

HTML:

<div class="container">
test<br />
test<br />
test<br />

<div class="showbg">
test
</div>

</div>

的CSS:

.container{ background-color:#CCCCCC;  width:300px;}
.showbg{ background-color:#FFFFFF; position:absolute; width:300px; margin-top:-65px; opacity:0.4;
filter:alpha(opacity=40);}

4 个答案:

答案 0 :(得分:7)

.container {position: relative;}
.showbg {position:absolute; top:0;bottom:0;left:0;right:0;}

答案 1 :(得分:1)

写:

.container{position:relative;}
.showbg{height:100%;}

DEMO here.

答案 2 :(得分:0)

编辑以下

的绝对div的样式
.showbg
{
    top:0;
    bottom:0
}

用于其他风格的绝对div。

答案 3 :(得分:0)

http://jsfiddle.net/pvr2Y/

上查看
.container {
    background-color:#CCCCCC;
    width:300px;
    position:relative;
}
.showbg {
    background-color:#FFFFFF;
    position:absolute;
    top:0;
    left:0;
    width:100%;
    height:100%;
    opacity:0.4;
    filter:alpha(opacity=40);
}
相关问题