CSS實現三欄自適應佈局(兩邊寬度固定,中間自適應)
阿新 • • 發佈:2018-12-23
所謂三列自適應佈局指的是兩邊定寬,中間block寬度自適應。這道題在今年網易內推前端工程師面試的時候也被問到。 我這裡主要分為兩大類,一類是基於position傳統的實現,一類是基於css3新特性彈性盒模型佈局實現。
1. 基於傳統的position和margin等屬性進行佈局
這裡也分為三種方法,分別為絕對定位法,聖盃佈局,自身浮動法。1).絕對定位法
絕對定位法原理是將左右兩邊使用absolute定位,因為絕對定位使其脫離文件流,後面的center會自然流動到他們上面,然後使用margin屬性,留出左右元素的寬度,既可以使中間元素自適應螢幕寬度。[html] view plain copy print?
- <!DOCTYPE html>
- <html>
- <head>
- <metacharset=“UTF-8”>
- <title>layout_box</title>
- <linkrel=“stylesheet”type=“text/css”href=“../css/layout_box.css”>
- </head>
- <body>
- <h3>
- <divid = “left”>我是左邊</div>
- <divid = “right”>我是右邊</div>
- <divid = “center”>我是中間</div>
- </body>
- </html>
css程式碼:<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>layout_box</title> <link rel="stylesheet" type="text/css" href="../css/layout_box.css"> </head> <body> <h3>實現三列寬度自適應佈局</h3> <div id = "left">我是左邊</div> <div id = "right">我是右邊</div> <div id = "center">我是中間</div> </body> </html>
[css] view plain copy print?
- html,body{ margin: 0px;width: 100%; }
- h3{height: 100px;margin:20px0 0;}
- #left,#right{width: 200px;height: 200px; background-color: #ffe6b8;position: absolute;top:120px;}
- #left{left:0px;}
- #right{right: 0px;}
- #center{margin:2px210px ;background-color: #eee;height: 200px; }
html,body{ margin: 0px;width: 100%; }
h3{height: 100px;margin:20px 0 0;}
2).使用自身浮動法
自身浮動法的原理就是使用對左右使用分別使用float:left和float:right,float使左右兩個元素脫離文件流,中間元素正常在正常文件流中,使用margin指定左右外邊距對其進行一個定位。 HTML程式碼: [html] view plain copy print?- <h3>使用自身浮動法定位</h3>
- <divid = “left_self”>我是左邊</div>
- <divid = “right_self”>我是右邊</div>
- <divid = “center_self”>我是中間</div>
<h3>使用自身浮動法定位</h3>
<div id = "left_self">我是左邊</div>
<div id = "right_self">我是右邊</div>
<div id = "center_self">我是中間</div>
css程式碼:
[css] view plain copy print?- #left_self,#right_self{ width: 200px;height: 200px; background-color: #ffe6b8 }
- #left_self {float: left;}
- #right_self{float: right;}
- #center_self{margin: 0 210px;height: 200px; background-color: #a0b3d6}
#left_self,#right_self{ width: 200px;height: 200px; background-color: #ffe6b8 }
#left_self {float: left;}
#right_self{float: right;}
#center_self{margin: 0 210px;height: 200px; background-color: #a0b3d6} 該佈局法的好處是受外界影響小,但是不足是 三個元素的順序,center一定要放在最後,這是和絕對定位不一樣的地方,center佔據文件流位置,所以一定要放在最後,左右兩個元素位置沒有關係。當瀏覽器視窗很小的時候,右邊元素會被擊倒下一行。
3). 聖盃佈局
聖盃佈局的原理是margin負值法。使用聖盃佈局首先需要在center元素外部包含一個div,包含div需要設定float屬性使其形成一個BFC,並設定寬度,並且這個寬度要和left塊的margin負值進行配合,具體原理參考這裡。這裡對聖盃佈局解釋特別詳細。 實現程式碼:HTML文件: [html] view plain copy print?- <h3>使用margin負值法進行佈局</h3>
- <divid = “wrap”>
- <divid = “center”></div>
- </div>
- <divid = “left_margin”></div>
- <divid = “right_margin”></div>
<h3>使用margin負值法進行佈局</h3>
<div id = "wrap">
<div id = "center"></div>
</div>
<div id = "left_margin"></div>
<div id = "right_margin"></div>
CSS程式碼:
[css] view plain copy print?- #wrap{ width: 100%;height: 100px; background-color: #fff;float: left;}
- #wrap#center{ margin:0 210px; height: 100px;background-color: #ffe6b8; }
- #left_margin,#right_margin{ float: left;width: 200px;height: 100px;background-color: darkorange }
- #left_margin {margin-left: -100%; background-color: lightpink}
- #right_margin{margin-left: -200px;}
#wrap{ width: 100%;height: 100px; background-color: #fff;float: left;}
#wrap #center{ margin:0 210px; height: 100px;background-color: #ffe6b8; }
#left_margin,#right_margin{ float: left;width: 200px;height: 100px;background-color: darkorange }
#left_margin {margin-left: -100%; background-color: lightpink}
#right_margin{margin-left: -200px;} 該方法在網站佈局中非常常見,也是面試常考點,優點是三欄相互關聯,有一定的抗性。需要注意的是,佈局中間部分一定要放在前面,左右順序不限制。對於left快的margin負值一定要等於wrap的寬度。
三種方法實現三欄網頁寬度自適應佈局方法見下圖。
2 , css3新特性
在外圍包裹一層div,設定為display:flex;中間設定flex:1;但是盒模型預設緊緊挨著,可以使用margin控制外邊距。
程式碼:
[html] view plain copy print?- <divid = “box”>
- <divid = “left_box”></div>
- <divid = “center_box”></div>
- <divid = “right_box”></div>
- </div>
<div id = "box">
<div id = "left_box"></div>
<div id = "center_box"></div>
<div id = "right_box"></div>
</div>
css樣式:[css] view plain copy print?- #box{width:100%;display: flex; height: 100px;margin: 10px;}
- #left_box,#right_box{width: 200px;height: 100px; margin: 10px; background-color: lightpink}
- #center_box{ flex:1; height: 100px;margin: 10px; background-color: lightgreen}
#box{width:100%;display: flex; height: 100px;margin: 10px;}
left_box,#right_box{width: 200px;height: 100px; margin: 10px; background-color: lightpink}
center_box{ flex:1; height: 100px;margin: 10px; background-color: lightgreen}
注意: center一定要放到中間。
效果圖如下: