我有页面结构为:

<div class="parent">
    <div class="child-left floatLeft">
    </div>

    <div class="child-right floatLeft">
    </div>
</div>

现在,左子DIV将有更多的内容,因此父DIV的高度会随着子DIV的增加而增加。

但问题是适合儿童的身高并没有增加。如何让它的高度等于它的父结点?


当前回答

我找到了很多答案,但可能对我来说最好的解决方案是

.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/

其他回答

我是在一次实习面试中学会这个妙招的。最初的问题是如何确保三列中每个顶部组件的高度具有相同的高度,以显示所有可用的内容。基本上创建一个不可见的子组件,呈现最大可能的高度。

<div class="parent">
    <div class="assert-height invisible">
        <!-- content -->
    </div>
    <div class="shown">
        <!-- content -->
    </div>
</div>

如果你了解bootstrap,你可以通过使用'flex'属性轻松完成。你所需要做的就是将下面的css属性传递给父div

.homepageSection {
  overflow: hidden;
  height: auto;
  display: flex;
  flex-flow: row;
}

其中。homepagesection是我的父div。 现在在你的html中添加子div

<div class="abc col-md-6">
<div class="abc col-md-6">

abc是我的子div,你可以检查两个子div的高度相等,而不考虑边界,只要给子div边界

CSS表显示是理想的:

.parent { 显示:表; 宽度:100%; } .parent > div { 显示:表格单元; } .child-left { 背景:浅蓝色; } .child-right { 背景:番木瓜; } < div class = "父" > < div class = "让孩子“> < / div > < div class = "生" >高< br >高< / div > < / div >

原来的答案(假设任何一列都可以更高):

你试图让父结点的高度依赖于子结点的高度而子结点的高度依赖于父结点的高度。不会计算。CSS Faux列是最好的解决方案。有不止一种方法。我宁愿不使用JavaScript。

<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>

我使用内联样式只是为了表达想法。

父结点有身高吗?如果你像这样设置父结点高度。

div.parent { height: 300px };

然后你可以让孩子像这样伸展到全身高度。

div.child-right { height: 100% };

EDIT

下面是如何使用JavaScript实现的。