使用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!!! */
}
和HTML對CSS以上:
<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/
設置基本佈局不「很多編碼「,使用一個框架來說你可以手動完成的事情對於下載速度,可維護性等是不值得的。只需自己寫下 – Andy