<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>
第一種解決方案基本上沒有什麼難度,平時應該也會用到不少!bootstrap
<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>
table方案一樣實現,只是如今咱們可能已經不多使用表格佈局了。頁面渲染性能也要差一點!性能
<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>
網格佈局方法也實現了,CSS3的網格佈局有點相似於bootstrap的柵格化佈局,都是以網格的方式來劃分元素所佔的區塊。flex
問題沒有結束,咱們繼續討論。五種解決方案哪個更好呢?筆者一直認爲技術沒有好壞之分,徹底取決於你用在什麼地方。flexbox