根据子div高度动态调整容器div的大小

时间:2015-03-31 23:51:13

标签: html css

我试图让容器div能够自动调整大小,具体取决于是否存在某些子div。到目前为止这是我的代码,当我删除子div时,父容器div的高度保持不变。

http://jsfiddle.net/sm7L6fqy/3/

#containerdiv {
width: 400px;
height: 185px;
background-color: rgba(39, 39, 39, 0.89);
position: absolute;
display: block;
top: 73px;
z-index: 99;
color: white;
border-radius: 2px;
}
#childdiv1 {
margin-left: 5px;
font-weight: bold;
margin-top: 3px;
color: rgb(223, 114, 53);
}
#childdiv2 {
margin-left: 5px;
font-size: .95em;
margin-top: -3px;
}
#childdiv5 {
margin-left: 5px;
margin-top: 5px;
}
#childdiv6 {
margin-left: 5px;
margin-bottom: -1px;
position: relative;
padding: 100px 3px;
}
#childdiv7srcparent {
border: 1px solid white;
border-radius: 3px;
margin-left: 5px;
left: -2px;
position: absolute;
top: 32px;
}
#childdiv8 {
position: absolute;
top: 67px;
left: 5px;
}
#childdiv3 {
font-size: .95em;
color: rgb(136, 71, 255);
position: absolute;
top: 43px;
left: 5px;
}   
#childdiv4src {
font-size: .95em;
color: white;
text-decoration: none;
top: 3px;
position: absolute;
right: 5px;
}

由于

1 个答案:

答案 0 :(得分:0)

#containerDiv的高度和宽度更改为auto

#containerdiv {
    width: auto;
    height: auto;
    background-color: rgba(39, 39, 39, 0.89);
    position: absolute;
    display: block;
    top: 73px;
    z-index: 99;
    color: white;
    border-radius: 2px;
}