css實現兩欄固定中間自適應
此方法的原理說將左右兩側進行定位,讓其脫離文件流。 中心區域自然流動到它們下面,再為其設定margin值
此方法頁面元素結構可以順序可以隨意變動, 注意top值需要進行處理,不然可能會出現對不齊現象
HTML
<div id='container'> <div class='left'>左側</div> <div class='center'>中間</div> <div class='right'>右側</div> </div> 複製程式碼
CSS
#container { position: relative; } .left, .right{ position: absolute; top: 0; width: 200px; min-height: 500px; background-color: red; } .left { left: 0; } .right { right: 0; } .center { margin: 0px 210px; min-height: 500px; background-color: yellow; } 複製程式碼
2、利用浮動和margin
此方法的原理說將左右兩側進行float 浮動讓其脫離文件流,中心部分處於正常文件流,再為其設定margin值
此方法一定要將center中間部分放到最後,當視窗特別小時右側會被擠下來
HTML
<div id='container'> <div class='left'>左側</div> <div class='right'>右側</div> <div class='center'>中間</div> </div> 複製程式碼
CSS
#container { position: relative; } .left, .right { width: 200px; min-height: 500px; background-color: red; } .left { float: left; } .right { float: right; } .center { min-height: 500px; margin: 0px 210px; background-color: yellow; } 複製程式碼
3、聖盃佈局
此方法最常見,三者相互關聯,最穩健。 首先需要將中間部分放再最前面,外面用一層容器包裹。外層容器讓其佔滿整個螢幕100%, 左中右三者都float: left。 將center左右margin設定為兩邊容器的寬度加上邊距,將left左側margin-left設定為-100%,讓其出現在最左側,將right右側margin-right設定為-200px,讓其出現在最右側。
HTML
<div id='container'> <div class='center_wrap'> <div class='center'>中間</div> </div> <div class='left'>左側</div> <div class='right'>右側</div> </div> 複製程式碼
CSS
#container { position: relative; } .center_wrap, .left, .right{ float: left; min-height: 500px; } .center_wrap { width: 100%; } .center_wrap .center{ min-height: 500px; margin: 0px 210px; background-color: yellow; } .left, .right { width: 200px; background-color: red; } .left { margin-left: -100%; } .right { margin-left: -200px; } 複製程式碼
4、CSS3 flex
HTML
<div id='container'> <div class='left'>左側</div> <div class='center'>中間</div> <div class='right'>右側</div> </div> 複製程式碼
CSS
#container { width: 100%; display: flex; } .left, .right { width: 200px; background-color: red; min-height: 500px; } .center { flex: 1; min-height: 500px; margin: 0 10px; background-color: yellow; } 複製程式碼