我有页面结构为:
<div class="parent">
<div class="child-left floatLeft">
</div>
<div class="child-right floatLeft">
</div>
</div>
现在,左子DIV将有更多的内容,因此父DIV的高度会随着子DIV的增加而增加。
但问题是适合儿童的身高并没有增加。如何让它的高度等于它的父结点?
我有页面结构为:
<div class="parent">
<div class="child-left floatLeft">
</div>
<div class="child-right floatLeft">
</div>
</div>
现在,左子DIV将有更多的内容,因此父DIV的高度会随着子DIV的增加而增加。
但问题是适合儿童的身高并没有增加。如何让它的高度等于它的父结点?
当前回答
我可以看到公认的答案使用位置:绝对;而不是float: left。如果你想使用float:留给下面的结构,
<div class="parent">
<div class="child-left floatLeft"></div>
<div class="child-right floatLeft"></div>
</div>
给出位置:自动;赋给父节点,以包含子节点的高度。
.parent {
position: auto;
}
.floatLeft {
float: left
}
其他回答
<div class="parent" style="height:500px;">
<div class="child-left floatLeft" style="height:100%">
</div>
<div class="child-right floatLeft" style="height:100%">
</div>
</div>
我使用内联样式只是为了表达想法。
对于父元素,添加以下属性:
.parent {
overflow: hidden;
position: relative;
width: 100%;
}
然后是。child-right这些:
.child-right {
background:green;
height: 100%;
width: 50%;
position: absolute;
right: 0;
top: 0;
}
在这里可以找到更多关于CSS示例的详细结果,在这里可以找到关于等高列的更多信息。
我最近在我的网站上用jQuery做了这个。该代码计算最高div的高度,并将其他div设置为相同的高度。技巧如下:
http://www.broken-links.com/2009/01/20/very-quick-equal-height-columns-in-jquery/
我不相信高度:100%将工作,所以如果你不明确知道div高度,我不认为有一个纯粹的CSS解决方案。
我找到了很多答案,但可能对我来说最好的解决方案是
.parent {
overflow: hidden;
}
.parent .floatLeft {
# your other styles
float: left;
margin-bottom: -99999px;
padding-bottom: 99999px;
}
你可以在这里查看其他解决方案http://css-tricks.com/fluid-width-equal-height-columns/
我可以看到公认的答案使用位置:绝对;而不是float: left。如果你想使用float:留给下面的结构,
<div class="parent">
<div class="child-left floatLeft"></div>
<div class="child-right floatLeft"></div>
</div>
给出位置:自动;赋给父节点,以包含子节点的高度。
.parent {
position: auto;
}
.floatLeft {
float: left
}