1. 程式人生 > >CSS實現三欄自適應佈局(兩邊寬度固定,中間自適應)

CSS實現三欄自適應佈局(兩邊寬度固定,中間自適應)

    所謂三列自適應佈局指的是兩邊定寬,中間block寬度自適應。這道題在今年網易內推前端工程師面試的時候也被問到。 我這裡主要分為兩大類,一類是基於position傳統的實現,一類是基於css3新特性彈性盒模型佈局實現。

 1. 基於傳統的position和margin等屬性進行佈局

   這裡也分為三種方法,分別為絕對定位法,聖盃佈局,自身浮動法。

  1).絕對定位法

    絕對定位法原理是將左右兩邊使用absolute定位,因為絕對定位使其脫離文件流,後面的center會自然流動到他們上面,然後使用margin屬性,留出左右元素的寬度,既可以使中間元素自適應螢幕寬度。
   程式碼如下: 文件程式碼:
[html] view plain copy print?
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <metacharset=“UTF-8”>
  5.         <title>layout_box</title>
  6.         <linkrel=“stylesheet”type=“text/css”href=“../css/layout_box.css”>
  7.     </head>
  8.     <body>
  9.         <h3>
    實現三列寬度自適應佈局</h3>
  10.         <divid = “left”>我是左邊</div>
  11.         <divid = “right”>我是右邊</div>
  12.         <divid = “center”>我是中間</div>
  13.     </body>
  14. </html>
<!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程式碼:
[css] view plain copy print?
  1. html,body{ margin0px;width100%; }  
  2. h3{height100px;margin:20px0 0;}  
  3. #left,#right{width200px;height200pxbackground-color#ffe6b8;positionabsolute;top:120px;}  
  4. #left{left:0px;}  
  5. #right{right: 0px;}  
  6. #center{margin:2px210px ;background-color#eee;height200px; }  
html,body{ margin: 0px;width: 100%; }
h3{height: 100px;margin:20px 0 0;}




#left,#right{width: 200px;height: 200px; background-color: #ffe6b8;position: absolute;top:120px;} #left{left:0px;} #right{right: 0px;} #center{margin:2px 210px ;background-color: #eee;height: 200px; }    該法佈局的好處,三個div順序可以任意改變。不足是 因為絕對定位,所以如果頁面上還有其他內容,top的值需要小心處理,最好能夠對css樣式進行一個初始化,就像在上面例子中加了一個標題,如果不對樣式進行初始化,則兩邊和中間的值會對不齊。 另外,隨著瀏覽器視窗縮小,小於200px的時候,會發生壓縮。 結果如圖,可以看到中間欄寬度隨著螢幕大小伸縮。

 2).使用自身浮動法

    自身浮動法的原理就是使用對左右使用分別使用float:left和float:right,float使左右兩個元素脫離文件流,中間元素正常在正常文件流中,使用margin指定左右外邊距對其進行一個定位。    HTML程式碼: [html] view plain copy print?
  1. <h3>使用自身浮動法定位</h3>
  2. <divid = “left_self”>我是左邊</div>
  3. <divid = “right_self”>我是右邊</div>
  4. <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?
  1. #left_self,#right_self{ width200px;height200pxbackground-color#ffe6b8 }  
  2. #left_self {floatleft;}  
  3. #right_self{floatright;}  
  4. #center_self{margin0 210px;height200pxbackground-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?
  1. <h3>使用margin負值法進行佈局</h3>
  2.         <divid = “wrap”>
  3.             <divid = “center”></div>
  4.         </div>
  5.         <divid = “left_margin”></div>
  6.         <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?
  1. #wrapwidth100%;height100pxbackground-color#fff;floatleft;}  
  2. #wrap#centermargin:0 210pxheight100px;background-color#ffe6b8; }  
  3. #left_margin,#right_margin{ floatleft;width200px;height100px;background-color: darkorange }  
  4. #left_margin {margin-left-100%background-color: lightpink}  
  5. #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?
  1. <divid = “box”>
  2.          <divid = “left_box”></div>
  3.          <divid = “center_box”></div>
  4.          <divid = “right_box”></div>
  5.         </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?
  1. #box{width:100%;display: flex; height100px;margin10px;}  
  2. #left_box,#right_box{width200px;height100pxmargin10pxbackground-color: lightpink}  
  3. #center_box{ flex:1height100px;margin10pxbackground-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一定要放到中間。

效果圖如下: