我有页面结构为:

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

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

现在,左子DIV将有更多的内容,因此父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示例的详细结果,在这里可以找到关于等高列的更多信息。

其他回答

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

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

对于父元素,添加以下属性:

.parent {
    overflow: hidden;
    position: relative;
    width: 100%;
}

然后是。child-right这些:

.child-right {
    background:green;
    height: 100%;
    width: 50%;
    position: absolute;
    right: 0;
    top: 0;
}

在这里可以找到更多关于CSS示例的详细结果,在这里可以找到关于等高列的更多信息。

请将父div设置为overflow: hidden 然后在儿童潜水你可以设置大量填充底部。例如 padding-bottom: 5000像素 然后margin-bottom: -5000px 然后所有子div都是父节点的高度。 当然,这不会工作,如果你试图把内容放在父div(外面的其他div是)

.parent{ border: 1px solid black; overflow: hidden; height: auto; } .child{ float: left; padding-bottom: 1500px; margin-bottom: -1500px; } .child1{ background: red; padding-right: 10px; } .child2{ background: green; padding-left: 10px; } <div class="parent"> <div class="child1 child"> One line text in child1 </div> <div class="child2 child"> Three line text in child2<br /> Three line text in child2<br /> Three line text in child2 </div> </div>

例如:http://jsfiddle.net/Tareqdhk/DAFEC/

对于父节点:

display: flex;

儿童:

align-items: stretch;

你应该添加一些前缀,检查caniuse。