我<div>在父容器中有两个子元素<div>,如下所示:
<div class="row">
<div class="item">
<p>Sup?</p>
</div>
<div class="item">
<p>Sup?</p>
<p>Wish that other guy was the same height as me</p>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我尝试了以下CSS:
.item {
background: rgba(0,0,0,0.1);
display: inline-block;
}
.row {
border: 1px solid red;
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能让两个孩子(div.item)成为最高孩子的身高?
jsFiddle:http://jsfiddle.net/7m4f7/
Yan*_*ana 20
一种解决方案是在显示值从改变inline-block到table-cell为后代div.item元素:
.row {
border: 1px solid red;
display: table;
border-spacing: 20px; /* For controlling spacing between cells... */
}
.item {
background: rgba(0,0,0,0.1);
display: table-cell;
}
Run Code Online (Sandbox Code Playgroud)
nul*_*ity 11
另一个解决方案是使用flexbox:http://jsfiddle.net/7m4f7/4/
.item {
background: rgba(0,0,0,0.1);
-webkit-box-flex: 1;
-moz-box-flex: 1;
-webkit-flex: 1;
-ms-flex: 1;
flex: 1;
}
.row {
border: 1px solid red;
display: -webkit-box;
display: -moz-box;
display: -webkit-flexbox;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
}
Run Code Online (Sandbox Code Playgroud)
但是,支持有限(但越来越好!)请参阅http://caniuse.com/flexbox
否则,您需要使用javascript手动设置高度.
jquery版本:FIDDLE
var maxheight = 0;
$('div div.y').each(function () {
maxheight = ($(this).height() > maxheight ? $(this).height() : maxheight);
});
$('div div.y').height(maxheight);
Run Code Online (Sandbox Code Playgroud)
编辑:只注意到你不处理的高度<div>的,但<p>的