使用 CSS 表格,很容易做到。但是 Chrome 需要技巧。
示例:CSS:
/* important */
*, *:before, *:after {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
#test {
background: #ddd;
display: block; /* important */
margin: 0;
/* max-width: 400px; */
min-height: 0; /* important */
overflow: hidden; /* important */
padding-bottom: 20px;
width: 100%;
}
#test .twoColumnLayout {
display: table; /* important */
height: 100%; /* important */
margin-top: 20px;
min-height: 10px; /* necessary for IE */
}
/* important, but only for IE, for others the row doesn't need to be present */
#test .twoColumnLayout .row {
display: table-row;
height: 100%;
}
#test .twoColumnLayout .col {
display: table-cell; /* important */
height: 100%; /* important */
text-align: center;
width: 50%; /* for 2 columns */
}
#test .twoColumnLayout .col > .content {
background: #fff;
border: 1px solid #e5e5e5;
border-radius: 3px;
height: 100%; /* important */
margin: 0 10px;
padding: 20px;
position: relative;
/* webkit fix!!! */
-webkit-box-sizing: content-box; /* necessary for Chrome!!! */
}
和上面的 CSS 的 HTML:
<section id="test">
<div class="twoColumnLayout">
<div class="row">
<div class="col">
<div class="content">
Some content here, little bit shorter...
</div>
</div>
<div class="col">
<div class="content">
Some content here, little bit longer. Some more content here, so it's even longer...
</div>
</div>
</div>
</div>
</section>
链接到 jsfiddle:
https ://jsfiddle.net/slonisko/xml5jjct/2/