CSS - 将float子DIV高度展开为父级的高度

个人页面结构以下: css

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

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

如今, child-left DIV将有更多内容,所以parent DIV的高度根据孩子DIV增长。 html

但问题是child-right身高不会增长。 我怎样才能使它的高度等于它的父母? bootstrap


#1楼

请将parent div设置为overflow: hidden
而后在子div中你能够为padding-bottom设置很大的数量。 例如
padding-bottom: 5000px
margin-bottom: -5000px
而后全部子div都将是父级的高度。
固然,若是你试图将内容放在父div中(在其余div以外),这将没法工做 ide

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


#2楼

我找到了不少答案,但对我来讲多是最好的解决方案 ui

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


#3楼

对于父母: .net

display: flex;

为孩子: code

align-items: stretch;

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


#4楼

我用这个评论部分:

.parent { display: flex; float: left; border-top:2px solid black; width:635px; margin:10px 0px 0px 0px; padding:0px 20px 0px 20px; background-color: rgba(255,255,255,0.5); } .child-left { align-items: stretch; float: left; width:135px; padding:10px 10px 10px 0px; height:inherit; border-right:2px solid black; } .child-right { align-items: stretch; float: left; width:468px; padding:10px; }
<div class="parent"> <div class="child-left">Short</div> <div class="child-right">Tall<br>Tall</div> </div>

你能够将孩子向右浮动,但在这种状况下,我精确地计算了每一个div的宽度。


#5楼

若是您了解bootstrap,可使用'flex'属性轻松完成。您须要作的是将下面的css属性传递给parent 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的边界

相关文章
相关标签/搜索