<style>
*{
padding:0;
margin:0;
}
.big div{
height:100px;
}
.big .left{
width:300px;
float:left;
background:red;
}
.big .right{
width:300px;
float:right;
background:yellow;
}
.big .center{
background:blue;
}
</style>
<div class="big">
<div class="left">
</div>
<div class="right">
</div>
<div class="center">
浮動解決方案
</div>
</div>
複製代碼
<style>
.position{
margin-top:10px;
}
.position>div{
position:absolute;
height:100px;
}
.position .left{
left:0;
width:300px;
background:red;
}
.position .right{
right:0;
width:300px;
background:yellow;
}
.position .center{
left:300px;
right:300px;
background:blue;
}
</style>
<div class="position">
<div class="left">
</div>
<div class="right">
</div>
<div class="center">
絕對定位方案2
</div>
</div>
複製代碼
<style>
.flex{
margin-top:120px;
display:flex;
}
.flex>div{
height:100px;
}
.flex .left{
width:300px;
background:red;
}
.flex .center{
flex:1;
background:blue;
}
.flex .right{
width:300px;
background:yellow;
}
</style>
<div class="flex">
<div class="left">
</div>
<div class="center">
flex方案
</div>
<div class="right">
</div>
</div>
複製代碼
<style>
.table{
margin-top:10px;
width:100%;
display:table;
height:100px;
}
.table>div{
display:table-cell;
}
.table .left{
width:300px;
background:red;
}
.table .center{
background:blue;
}
.table .right{
width:300px;
background:yellow;
}
</style>
<div class="table">
<div class="left">
</div>
<div class="center">
table方案
</div>
<div class="right">
</div>
</div>
複製代碼
<style>
.grid{
margin-top:10px;
display:grid;
width:100%;
grid-template-rows:100px;
grid-template-columns: 300px auto 300px;
}
.grid .left{
background:red;
}
.grid .center{
background:blue;
}
.grid .right{
background:yellow;
}
</style>
<body>
<div class="grid">
<div class="left">
</div>
<div class="center">
grid方案
</div>
<div class="right">
</div>
</div>
</body>
複製代碼
問題沒有結束,咱們繼續討論。五種解決方案哪個更好呢?筆者一直認爲技術沒有好壞之分,徹底取決於你用在什麼地方。css
浮動:兼容性好,若是對兼容性有明確的要求使用浮動應該知足需求,可是必定要處理好與周邊元素的關係,由於一不注意浮動就可能形成頁面佈局混亂等問題,不過解決浮動帶來的反作用方法也很多這裏咱們不作討論。bootstrap
絕對定位:簡單直接,可是會使父元素脫離正常文檔流裏面的子元素也會跟着脫離。佈局
flex:目前看來比較完美,不過如今稍微完美一點的技術都存在或多或少的兼容性問題,一樣這個樣式IE9及如下是不支持的!(IE呀!)性能
表格佈局:在咱們要細化結構的時候會顯得很繁瑣,同時表格佈局三個單元格的高度會一塊兒變更也不利於咱們進行佈局。最重要的固然仍是表格渲染性能不好。flex
網格佈局:代碼優美簡潔,不過仍是兼容性問題。可是將來是美好的!flexbox