乾貨!各類常見佈局實現+知名網站實例分析

各類頁面常見佈局+知名網站實例分析+相關閱讀推薦

閱前必看:本文總結了各類常見的佈局實現,每一個方法的優缺點分析日後在github上陸續補充。還有就是這篇文章沒提到的其餘佈局,待本人後續想到或遇到定會在github上及時更新。
因爲文章篇幅較長,排版有些混亂,但願別介意哈。掘金PC端是會自動生成目錄的,爲了方便移動端閱讀,還特地作了個錨點目錄,一個個標題重寫,真心累!(==後來發現掘金對文章的標題通過處理的,錨點不生效,在其餘網站沒問題的,移動端能夠進去github的在線demo閱讀)。各位讀者若是發現問題或者有什麼意見,歡迎提出!——歡迎收藏!歡迎Star!
css

from github github.com/Sweet-KK/cs…
本文原創,轉載請標明出處,摘要引流則隨意。html


目錄

注:PC端推薦用法後面加▲,至於移動端在兼容性容許的狀況下優先考慮flexcss3

1、水平居中
(1)文本/行內元素/行內塊級元素▲
(2)單個塊級元素▲
(3)多個塊級元素
(4)使用絕對定位實現▲
(5)任意個元素(flex)
★本章小結:git

2、垂直居中
(1)單行文本/行內元素/行內塊級元素▲
(2)多行文本/行內元素/行內塊級元素
(3)圖片▲
(4)單個塊級元素
--(1) 使用tabel-cell實現:
--(2) 使用絕對定位實現:▲
--(3) 使用flex實現:
(5)任意個元素(flex)
★本章小結:github

3、水平垂直居中
(1)行內/行內塊級/圖片▲
(2)table-cell
(3)button做爲父元素
(4)絕對定位▲
(5)絕對居中▲
(6)flex
(7)視窗居中
★本章小結:web

4、兩列布局
4.1 左列定寬,右列自適應
--(1)利用float+margin實現
--(2)利用float+margin(fix)實現
--(3)使用float+overflow實現▲
--(4)使用table實現▲
--(5)使用絕對定位實現
--(6)使用flex實現
--(7)使用Grid實現
4.2 左列自適應,右列定寬
--(1)使用float+margin實現
--(2)使用float+overflow實現▲
--(3)使用table實現▲
--(4)使用絕對定位實現
--(5)使用flex實現
--(6)使用Grid實現
4.3 一列不定,一列自適應
--(1)使用float+overflow實現▲
--(2)使用flex實現
--(3)使用Grid實現瀏覽器

5、三列布局
5.1 兩列定寬,一列自適應
--(1)使用float+margin實現
--(2)使用float+overflow實現▲
--(3)使用table實現▲
--(4)使用flex實現
--(5)使用Grid實現
5.2 兩側定寬,中間自適應
5.2.1 雙飛翼佈局方法
5.2.2 聖盃佈局方法
5.2.3 使用Grid實現
5.2.4 其餘方法
--(1)使用table實現▲
--(2)使用flex實現
--(3)使用position實現▲bash

6、多列布局
6.1 等寬佈局
6.1.1四列等寬
--(1)使用float實現▲
--(2)使用table實現▲
--(3)使用flex實現
6.1.2多列等寬
--(1)使用float實現▲
--(2)使用table實現▲
--(3)使用flex實現
--(4)使用Grid實現
6.2 九宮格佈局
--(1)使用table實現▲
--(2)使用flex實現
--(3)使用Grid實現
6.3 柵格系統▲
--(1)用Less生成app

7、全屏佈局
(1)使用絕對定位實現▲
(2)使用flex實現
(3)使用Grid實現ide

8、網站實例佈局分析:
8.1 小米官網
8.1.1 兼容IE9+的方法
--(1)頁面總體
--(2)局部——header
--(3)局部——top
--(4)局部——center
--(5)局部——bottom
--(6)局部——footer
--(7)所有代碼(優化後)
8.1.2 Flexbox+Grid搭配用法(將來...)

9、其餘補充:
9.1 移動端viewport
--設置viewport:
--閱讀推薦:
9.2 媒體查詢
--代碼示例:
--閱讀推薦:
9.3 REM
--閱讀推薦:
9.4 Flexbox
--閱讀推薦:
9.5 CSS Grid
--閱讀推薦:

End:感謝


1、水平居中

一,二,三章都是parent+son的簡單結構,html代碼和效果圖就不貼出來了,第四章之後纔有

(1)文本/行內元素/行內塊級元素▲

原理:text-align只控制行內內容(文字、行內元素、行內塊級元素)如何相對他的塊父元素對齊

#parent{
    text-align: center;
}
複製代碼

優缺點

  • 優勢:簡單快捷,容易理解,兼容性很是好
  • 缺點:只對行內內容有效;屬性會繼承影響到後代行內內容;若是子元素寬度大於父元素寬度則無效,只有後代行內內容中寬度小於設置text-align屬性的元素寬度的時候,纔會水平居中
(2)單個塊級元素▲

原理:根據規範介紹得很清楚了,有這麼一種狀況:在margin有節餘的同時若是左右margin設置了auto,將會均分剩餘空間。另外,若是上下的margin設置了auto,其計算值爲0

#son{
    width: 100px; /*必須定寬*/
    margin: 0 auto;
}
複製代碼

優缺點

  • 優勢:簡單;兼容性好
  • 缺點:必須定寬,而且值不能爲auto;寬度要小於父元素,不然無效
(3)多個塊級元素

原理:text-align只控制行內內容(文字、行內元素、行內塊級元素)如何相對他的塊父元素對齊

#parent{
    text-align: center;
}
.son{
    display: inline-block; /*改成行內或者行內塊級形式,以達到text-align對其生效*/
}
複製代碼

優缺點

  • 優勢:簡單,容易理解,兼容性很是好
  • 缺點:只對行內內容有效;屬性會繼承影響到後代行內內容;塊級改成inline-block換行、空格會產生元素間隔
(4)使用絕對定位實現▲

原理:子絕父相,top、right、bottom、left的值是相對於父元素尺寸的,而後margin或者transform是相對於自身尺寸的,組合使用達到水平居中的目的

#parent{
    height: 200px;
    width: 200px;  /*定寬*/
    position: relative;  /*父相*/
    background-color: #f00;
}
#son{
    position: absolute;  /*子絕*/
    left: 50%;  /*父元素寬度一半,這裏等同於left:100px*/
    transform: translateX(-50%);  /*自身寬度一半,等同於margin-left: -50px;*/
    width: 100px;  /*定寬*/
    height: 100px;
    background-color: #00ff00;
}
複製代碼

優缺點

  • 優勢:使用margin-left兼容性好;無論是塊級仍是行內元素均可以實現
  • 缺點:代碼較多;脫離文檔流;使用margin-left須要知道寬度值;使用transform兼容性很差(ie9+)
(5)任意個元素(flex)

原理:就是設置當前主軸對齊方式爲居中。說不上爲何,flex無非就是主軸側軸是重點,而後就是排列方式的設置,能夠去看看文末的flex閱讀推薦

#parent{
    display: flex;
    justify-content: center;
}
複製代碼

優缺點

  • 優勢:功能強大;簡單方便;容易理解
  • 缺點:PC端兼容性很差,移動端(Android4.0+)
本章小結:
  • 對於水平居中,咱們應該先考慮,哪些元素有自帶的居中效果,最早想到的應該就是 text-align:center 了,可是這個只對行內內容有效,因此咱們要使用 text-align:center 就必須將子元素設置爲 display: inline; 或者 display: inline-block;
  • 其次就是考慮能不能用margin: 0 auto; ,由於這都是一兩句代碼能搞定的事,實在不行就是用絕對定位去實現了。
  • 移動端能用flex就用flex,簡單方便,靈活而且功能強大,無愧爲網頁佈局的一大利器!

2、垂直居中

一,二,三章都是parent+son的簡單結構,html代碼和效果圖就不貼出來了,第四章之後纔有

(1)單行文本/行內元素/行內塊級元素▲

原理:line-height的最終表現是經過inline box實現的,而不管inline box所佔據的高度是多少(不管比文字大仍是比文字小),其佔據的空間都是與文字內容公用水平中垂線的。

#parent{
    height: 150px;
    line-height: 150px;  /*與height等值*/
}
複製代碼

優缺點

  • 優勢:簡單;兼容性好
  • 缺點:只能用於單行行內內容;要知道高度的值
(2)多行文本/行內元素/行內塊級元素

原理同上

#parent{ /*或者用span把全部文字包裹起來,設置display:inline-block轉換成圖片的方式解決*/
    height: 150px;
    line-height: 30px;  /*元素在頁面呈現爲5行,則line-height的值爲height/5*/
}
複製代碼

優缺點

  • 優勢:簡單;兼容性好
  • 缺點:只能用於行內內容;須要知道高度和最終呈現多少行來計算出line-height的值,建議用span包裹多行文本
(3)圖片▲

原理:vertical-align和line-height的基友關係

#parent{
    height: 150px;
    line-height: 150px;
    font-size: 0;
}
img#son{vertical-align: middle;} /*默認是基線對齊,改成middle*/
複製代碼

優缺點

  • 優勢:簡單;兼容性好
  • 缺點:須要添加font-size: 0; 才能夠徹底的垂直居中;不過須要主要,html#parent包裹img之間須要有換行或空格
(4)單個塊級元素

html代碼:

<div id="parent">
    <div id="son"></div>
</div>
複製代碼
(4-1) 使用tabel-cell實現:
原理:CSS Table,使表格內容對齊方式爲middle
#parent{
    display: table-cell;
    vertical-align: middle;
}
複製代碼

優缺點

  • 優勢:簡單;寬高不定;兼容性好(ie8+)
  • 缺點:設置tabl-cell的元素,寬度和高度的值設置百分比無效,須要給它的父元素設置display: table; 才生效;table-cell不感知margin,在父元素上設置table-row等屬性,也會使其不感知height;設置float或position會對默認佈局形成破壞,能夠考慮爲之增長一個父div定義float等屬性;內容溢出時會自動撐開父元素
(4-2) 使用絕對定位實現:▲
/*原理:子絕父相,top、right、bottom、left的值是相對於父元素尺寸的,而後margin或者transform是相對於自身尺寸的,組合使用達到水平居中的目的*/
#parent{
    height: 150px;
    position: relative;  /*父相*/
}
#son{
    position: absolute;  /*子絕*/
    top: 50%;  /*父元素高度一半,這裏等同於top:75px;*/
    transform: translateY(-50%);  /*自身高度一半,這裏等同於margin-top:-25px;*/
    height: 50px;
}

/*優缺點
- 優勢:使用margin-top兼容性好;無論是塊級仍是行內元素均可以實現
- 缺點:代碼較多;脫離文檔流;使用margin-top須要知道高度值;使用transform兼容性很差(ie9+)*/

或

/*原理:當top、bottom爲0時,margin-top&bottom會無限延伸佔滿空間而且平分*/
#parent{position: relative;}
#son{
    position: absolute;
    margin: auto 0;
    top: 0;
    bottom: 0;
    height: 50px;
}

/*優缺點
- 優勢:簡單;兼容性較好(ie8+)
- 缺點:脫離文檔流*/
複製代碼
(4-3) 使用flex實現:

原理:flex設置對齊方式罷了,請查閱文末flex閱讀推薦

#parent{
    display: flex;
    align-items: center;
}

或

#parent{display: flex;}
#son{align-self: center;}

或
/*原理:這個還沒有搞清楚,應該是flex使margin上下邊界無限延伸至剩餘空間並平分了*/
#parent{display: flex;}
#son{margin: auto 0;}
複製代碼

優缺點

  • 優勢:簡單靈活;功能強大
  • 缺點:PC端兼容性很差,移動端(Android4.0+)
(5)任意個元素(flex)

原理:flex設置對齊方式罷了,請查閱文末flex閱讀推薦

#parent{
    display: flex;
    align-items: center;
}

或

#parent{
    display: flex;
}
.son{
    align-self: center;
}

或 

#parent{
    display: flex;
    flex-direction: column;
    justify-content: center;
}
複製代碼

優缺點

  • 優勢:簡單靈活;功能強大
  • 缺點:PC端兼容性很差,移動端(Android4.0+)
★本章小結:
  • 對於垂直居中,最早想到的應該就是 line-height 了,可是這個只能用於行內內容;
  • 其次就是考慮能不能用vertical-align: middle; ,不過這個必定要熟知原理才能用得順手,建議看下vertical-align和line-height的基友關係
  • 而後即是絕對定位,雖然代碼多了點,可是勝在適用於不一樣狀況;
  • 移動端兼容性容許的狀況下能用flex就用flex

3、水平垂直居中

一,二,三章都是parent+son的簡單結構,html代碼和效果圖就不貼出來了,第四章之後纔有

(1)行內/行內塊級/圖片▲

原理:text-align: center; 控制行內內容相對於塊父元素水平居中,而後就是line-heightvertical-align的基友關係使其垂直居中,font-size: 0; 是爲了消除近似居中的bug

#parent{
    height: 150px;
    line-height: 150px;  /*行高的值與height相等*/
    text-align: center;
    font-size: 0;   /*消除幽靈空白節點的bug*/
}
#son{
    /*display: inline-block;*/  /*若是是塊級元素需改成行內或行內塊級才生效*/
    vertical-align: middle;
}
複製代碼

優缺點

  • 優勢:代碼簡單;兼容性好(ie8+)
  • 缺點:只對行內內容有效;須要添加font-size: 0; 才能夠徹底的垂直居中;不過須要注意html中#parent包裹#son之間須要有換行或空格;熟悉line-heightvertical-align的基友關係較難
(2)table-cell

原理:CSS Table,使表格內容垂直對齊方式爲middle,而後根據是行內內容仍是塊級內容採起不一樣的方式達到水平居中

#parent{
    height: 150px;
    width: 200px;
    display: table-cell;
    vertical-align: middle;
    /*text-align: center;*/   /*若是是行內元素就添加這個*/
}
#son{
    /*margin: 0 auto;*/    /*若是是塊級元素就添加這個*/
    width: 100px;
    height: 50px;
}
複製代碼

優缺點

  • 優勢:簡單;適用於寬度高度未知狀況;兼容性好(ie8+)
  • 缺點:設置tabl-cell的元素,寬度和高度的值設置百分比無效,須要給它的父元素設置display: table; 才生效;table-cell不感知margin,在父元素上設置table-row等屬性,也會使其不感知height;設置float或position會對默認佈局形成破壞,能夠考慮爲之增長一個父div定義float等屬性;內容溢出時會自動撐開父元素
(3)button做爲父元素
原理:button的默認樣式,再把須要居中的元素表現形式改成行內或行內塊級就好
button#parent{ /*改掉button默認樣式就行了,不須要居中處理*/
    height: 150px;
    width: 200px;
    outline: none;  
    border: none;
}
#son{
    display: inline-block; /*button自帶text-align: center,改成行內水平居中生效*/
}
複製代碼

優缺點

  • 優勢:簡單方便,充分利用默認樣式
  • 缺點:只適用於行內內容;須要清除部分默認樣式;水平垂直居中兼容性很好,可是ie下點擊會有凹陷效果!
(4)絕對定位

原理:子絕父相,top、right、bottom、left的值是相對於父元素尺寸的,而後margin或者transform是相對於自身尺寸的,組合使用達到幾何上的水平垂直居中

#parent{
    position: relative;
}
#son{
    position: absolute;
    top: 50%;
    left: 50%;
    /*定寬高時等同於margin-left:負自身寬度一半;margin-top:負自身高度一半;*/
    transform: translate(-50%,-50%); 
}
複製代碼

優缺點

  • 優勢:使用margin兼容性好;無論是塊級仍是行內元素均可以實現
  • 缺點:代碼較多;脫離文檔流;使用margin須要知道寬高;使用transform兼容性很差(ie9+)
(5)絕對居中

原理:當top、bottom爲0時,margin-top&bottom設置auto的話會無限延伸佔滿空間而且平分;當left、right爲0時,margin-left&right設置auto的話會無限延伸佔滿空間而且平分

#parent{
    position: relative;
}
#son{
    position: absolute;
    margin: auto;
    width: 100px;
    height: 50px;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
}
複製代碼

優缺點

  • 優勢:無需關注寬高;兼容性較好(ie8+)
  • 缺點:代碼較多;脫離文檔流
(6)flex
原理:flex設置對齊方式罷了,請查閱文末flex閱讀推薦
#parent{
    display: flex;
}
#son{
    margin: auto;
}

或

#parent{
    display: flex;
    justify-content: center;
    align-items: center;
}

或

#parent{
    display: flex;
    justify-content: center;
}
#son{
    align-self: center;
}
複製代碼

優缺點

  • 優勢:簡單靈活;功能強大
  • 缺點:PC端兼容性很差,移動端(Android4.0+)
(7)視窗居中

原理:vh爲視口單位,視口即文檔可視的部分,50vh就是視口高度的50/100,設置50vh上邊距再

#son{
	/*0若是去掉,則會多出滾動條而且上下都是50vh的margin。若是去掉就給body加上overflow:hidden;*/
    margin: 50vh auto 0;  
    transform: translateY(-50%);
}
複製代碼

優缺點

  • 優勢:簡單;容易理解;兩句代碼達到屏幕水平垂直居中
  • 缺點:兼容性很差(ie9+,Android4.4+)
★本章小結:
  • 通常狀況下,水平垂直居中,咱們最經常使用的就是絕對定位加負邊距了,缺點就是須要知道寬高,使用transform卻是能夠不須要,可是兼容性很差(ie9+);
  • 其次就是絕對居中,絕對定位設置top、left、right、bottom爲0,而後margin:auto; 讓瀏覽器自動平分邊距以達到水平垂直居中的目的;
  • 若是是行內/行內塊級/圖片這些內容,能夠優先考慮line-heightvertical-align 結合使用,不要忘了還有text-align ,這個方法代碼其實很少,就是理解原理有點困難,想要熟練應對各類狀況還需好好研究;
  • 移動端兼容性容許的狀況下能用flex就用flex。

4、兩列布局

4.1 左列定寬,右列自適應

效果:

(1)利用float+margin實現

html代碼:

<body>
<div id="left">左列定寬</div>
<div id="right">右列自適應</div>
</body>
複製代碼

css代碼:

#left {
    background-color: #f00;
    float: left;
    width: 100px;
    height: 500px;
}
#right {
    background-color: #0f0;
    height: 500px;
    margin-left: 100px; /*大於等於#left的寬度*/
}
複製代碼
(2)利用float+margin(fix)實現

html代碼:

<body>
<div id="left">左列定寬</div>
<div id="right-fix">
    <div id="right">右列自適應</div>
</div>
</body>
複製代碼

css代碼:

#left {
    background-color: #f00;
    float: left;
    width: 100px;
    height: 500px;
}
#right-fix {
    float: right;
    width: 100%;
    margin-left: -100px; /*正值大於或等於#left的寬度,才能顯示在同一行*/
}
#right{
    margin-left: 100px; /*大於或等於#left的寬度*/
    background-color: #0f0;
    height: 500px;
}
複製代碼
(3)使用float+overflow實現

html代碼:

<body>
<div id="left">左列定寬</div>
<div id="right">右列自適應</div>
</body>
複製代碼

css代碼:

#left {
    background-color: #f00;
    float: left;
    width: 100px;
    height: 500px;
}
#right {
    background-color: #0f0;
    height: 500px;
    overflow: hidden; /*觸發bfc達到自適應*/
}
複製代碼

優缺點:

  • 優勢:代碼簡單,容易理解,無需關注定寬的寬度,利用bfc達到自適應效果
  • 缺點:浮動脫離文檔流,須要手動清除浮動,不然會產生高度塌陷;不支持ie6
(4)使用table實現

html代碼:

<div id="parent">
    <div id="left">左列定寬</div>
    <div id="right">右列自適應</div>
</div>
複製代碼

css代碼:

#parent{
    width: 100%;
    display: table;
    height: 500px;
}
#left {
    width: 100px;
    background-color: #f00;
}
#right {
    background-color: #0f0;
}
#left,#right{
    display: table-cell;  /*利用單元格自動分配寬度*/
}
複製代碼

優缺點:

  • 優勢:代碼簡單,容易理解,無需關注定寬的寬度,利用單元格自動分配達到自適應效果
  • 缺點:margin失效;設置間隔比較麻煩;不支持ie8-
(5)使用絕對定位實現

html代碼:

<body>
<div id="parent">
    <div id="left">左列定寬</div>
    <div id="right">右列自適應</div>
</div>
</body>
複製代碼

css代碼:

#parent{
    position: relative;  /*子絕父相*/
}
#left {
    position: absolute;
    top: 0;
    left: 0;
    background-color: #f00;
    width: 100px;
    height: 500px;
}
#right {
    position: absolute;
    top: 0;
    left: 100px;  /*值大於等於#left的寬度*/
    right: 0;
    background-color: #0f0;
    height: 500px;
}
複製代碼
(6)使用flex實現

html代碼:

<body>
<div id="parent">
    <div id="left">左列定寬</div>
    <div id="right">右列自適應</div>
</div>
</body>


複製代碼

css代碼:

#parent{
    width: 100%;
    height: 500px;
    display: flex;
}
#left {
    width: 100px;
    background-color: #f00;
}
#right {
    flex: 1; /*均分了父元素剩餘空間*/
    background-color: #0f0;
}

複製代碼
(7)使用Grid實現

html代碼:

<body>
<div id="parent">
    <div id="left">左列定寬</div>
    <div id="right">右列自適應</div>
</div>
</body>

複製代碼

css代碼:

#parent {
    width: 100%;
    height: 500px;
    display: grid;
    grid-template-columns: 100px auto;  /*設定2列就ok了,auto換成1fr也行*/
}
#left {
    background-color: #f00;
}
#right {
    background-color: #0f0;
}

複製代碼

4.2 左列自適應,右列定寬

效果:

image.png

(1)使用float+margin實現

html代碼:

<body>
<div id="parent">
    <div id="left">左列自適應</div>
    <div id="right">右列定寬</div>
</div>
</body>

複製代碼

css代碼:

#parent{
    height: 500px;
    padding-left: 100px;  /*抵消#left的margin-left以達到#parent水平居中*/
}
#left {
    width: 100%;
    height: 500px;
    float: left;
    margin-left: -100px; /*正值等於#right的寬度*/
    background-color: #f00;
}
#right {
    height: 500px;
    width: 100px;
    float: right;
    background-color: #0f0;
}

複製代碼
(2)使用float+overflow實現

html代碼:

<body>
<div id="parent">
    <div id="right">右列定寬</div>
    <div id="left">左列自適應</div>   <!--順序要換一下-->
</div>
</body>

複製代碼

css代碼:

#left {
    overflow: hidden;  /*觸發bfc*/
    height: 500px;
    background-color: #f00;
}
#right {
    margin-left: 10px;  /*margin須要定義在#right中*/
    float: right;
    width: 100px;
    height: 500px;
    background-color: #0f0;
}

複製代碼

優缺點:

  • 優勢:代碼簡單,容易理解,無需關注定寬的寬度,利用bfc達到自適應效果
  • 缺點:浮動脫離文檔流,須要手動清除浮動,不然會產生高度塌陷;不支持ie6
(3)使用table實現

html代碼:

<body>
<div id="parent">
    <div id="left">左列自適應</div>
    <div id="right">右列定寬</div>
</div>
</body>

複製代碼

css代碼:

#parent{
    width: 100%;
    height: 500px;
    display: table;
}
#left {
    background-color: #f00;
    display: table-cell;
}
#right {
    width: 100px;
    background-color: #0f0;
    display: table-cell;
}

複製代碼

優缺點:

  • 優勢:代碼簡單,容易理解,無需關注定寬的寬度,利用單元格自動分配達到自適應效果
  • 缺點:margin失效;設置間隔比較麻煩;不支持ie8-
(4)使用絕對定位實現

html代碼:

<body>
<div id="parent">
    <div id="left">左列自適應</div>
    <div id="right">右列定寬</div>
</div>
</body>

複製代碼

css代碼:

#parent{
    position: relative;  /*子絕父相*/
}
#left {
    position: absolute;
    top: 0;
    left: 0;
    right: 100px;  /*大於等於#rigth的寬度*/
    background-color: #f00;
    height: 500px;
}
#right {
    position: absolute;
    top: 0;
    right: 0;
    background-color: #0f0;
    width: 100px;
    height: 500px;
}

複製代碼
(5)使用flex實現

html代碼:

<body>
<div id="parent">
    <div id="left">左列自適應</div>
    <div id="right">右列定寬</div>
</div>
</body>

複製代碼

css代碼:

#parent{
    height: 500px;
    display: flex;
}
#left {
    flex: 1;
    background-color: #f00;
}
#right {
    width: 100px;
    background-color: #0f0;
}

複製代碼
(6)使用Grid實現

html代碼:

<body>
<div id="parent">
    <div id="left">左列自適應</div>
    <div id="right">右列定寬</div>
</div>
</body>

複製代碼

css代碼:

#parent {
    height: 500px;
    display: grid;
    grid-template-columns: auto 100px;  /*設定2列,auto換成1fr也行*/
}
#left {
    background-color: #f00;
}
#right {
    background-color: #0f0;
}

複製代碼

4.3 一列不定,一列自適應

(盒子寬度隨着內容增長或減小發生變化,另外一個盒子自適應)

效果圖:

image.png

變化後:

image.png

(1)使用float+overflow實現

html代碼:

<body>
<div id="parent">
    <div id="left">左列不定寬</div>
    <div id="right">右列自適應</div>
</div>
</body>

複製代碼

css代碼:

#left {
    margin-right: 10px;
    float: left;  /*只設置浮動,不設寬度*/
    height: 500px;
    background-color: #f00;
}
#right {
    overflow: hidden;  /*觸發bfc*/
    height: 500px;
    background-color: #0f0;
}

複製代碼

優缺點:

  • 優勢:代碼簡單,容易理解,無需關注寬度,利用bfc達到自適應效果
  • 缺點:浮動脫離文檔流,須要手動清除浮動,不然會產生高度塌陷;不支持ie6
(2)使用flex實現

html代碼:

<body>
<div id="parent">
    <div id="left">左列不定寬</div>
    <div id="right">右列自適應</div>
</div>
</body>

複製代碼

css代碼:

#parent{
    display: flex;
}
#left { /*不設寬度*/
    margin-right: 10px;
    height: 500px;
    background-color: #f00;
}
#right {
    height: 500px;
    background-color: #0f0;
    flex: 1;  /*均分#parent剩餘的部分*/
}

複製代碼
(3)使用Grid實現

html代碼:

<body>
<div id="parent">
    <div id="left">左列不定寬</div>
    <div id="right">右列自適應</div>
</div>
</body>

複製代碼

css代碼:

#parent{
    display: grid;
    grid-template-columns: auto 1fr;  /*auto和1fr換一下順序就是左列自適應,右列不定寬了*/
}
#left {
    margin-right: 10px;
    height: 500px;
    background-color: #f00;
}
#right {
    height: 500px;
    background-color: #0f0;
}

複製代碼

左列自適應,右列不定寬同理(參考4.1和4.3對應代碼示例)

5、三列布局

5.1 兩列定寬,一列自適應

效果圖:

image.png

(1)使用float+margin實現

html代碼:

<body>
<div id="parent">
    <div id="left">左列定寬</div>
    <div id="center">中間定寬</div>
    <div id="right">右列自適應</div>
</div>
</body>

複製代碼

css代碼:

#parent{
    min-width: 310px; /*100+10+200,防止寬度不夠,子元素換行*/
}
#left {
    margin-right: 10px;  /*#left和#center間隔*/
    float: left;
    width: 100px;
    height: 500px;
    background-color: #f00;
}
#center{
    float: left;
    width: 200px;
    height: 500px;
    background-color: #eeff2b;
}
#right {
    margin-left: 320px;  /*等於#left和#center的寬度之和加上間隔,多出來的就是#right和#center的間隔*/
    height: 500px;
    background-color: #0f0;
}

複製代碼
(2)使用float+overflow實現

html代碼:

<body>
<div id="parent">
    <div id="left">左列定寬</div>
    <div id="center">中間定寬</div>
    <div id="right">右列自適應</div>
</div>
</body>

複製代碼

css代碼:

#parent{
    min-width: 320px; /*100+10+200+20,防止寬度不夠,子元素換行*/
}
#left {
    margin-right: 10px; /*間隔*/
    float: left;
    width: 100px;
    height: 500px;
    background-color: #f00;
}
#center{
    margin-right: 10px; /*在此定義和#right的間隔*/
    float: left;
    width: 200px;
    height: 500px;
    background-color: #eeff2b;
}
#right {
    overflow: hidden;  /*觸發bfc*/
    height: 500px;
    background-color: #0f0;
}

複製代碼

優缺點:

  • 優勢:代碼簡單,容易理解,無需關注定寬的寬度,利用bfc達到自適應效果
  • 缺點:浮動脫離文檔流,須要手動清除浮動,不然會產生高度塌陷;不支持ie6
(3)使用table實現

html代碼:

<body>
<div id="parent">
    <div id="left">左列定寬</div>
    <div id="center">中間定寬</div>
    <div id="right">右列自適應</div>
</div>
</body>

複製代碼

css代碼:

#parent {
    width: 100%; 
    height: 520px; /*抵消上下間距10*2的高度影響*/
    margin: -10px 0;  /*抵消上下邊間距10的位置影響*/
    display: table;
    /*左右兩邊間距大了一點,子元素改用padding設置盒子間距就沒有這個問題*/
    border-spacing: 10px;  /*關鍵!!!設置間距*/
}
#left {
    display: table-cell;
    width: 100px;
    background-color: #f00;
}
#center {
    display: table-cell;
    width: 200px;
    background-color: #eeff2b;
}
#right {
    display: table-cell;
    background-color: #0f0;
}

複製代碼

優缺點:

  • 優勢:代碼簡單,容易理解,無需關注定寬的寬度,利用單元格自動分配達到自適應效果
  • 缺點:margin失效;設置間隔比較麻煩;不支持ie8-
(4)使用flex實現

html代碼:

<body>
<div id="parent">
    <div id="left">左列定寬</div>
    <div id="center">中間定寬</div>
    <div id="right">右列自適應</div>
</div>
</body>

複製代碼

css代碼:

#parent {
    height: 500px;
    display: flex;
}
#left {
    margin-right: 10px;  /*間距*/
    width: 100px;
    background-color: #f00;
}
#center {
    margin-right: 10px;  /*間距*/
    width: 200px;
    background-color: #eeff2b;
}
#right {
    flex: 1;  /*均分#parent剩餘的部分達到自適應*/
    background-color: #0f0;
}

複製代碼
(5)使用Grid實現

html代碼:

<body>
<div id="parent">
    <div id="left">左列定寬</div>
    <div id="center">中間定寬</div>
    <div id="right">右列自適應</div>
</div>
</body>

複製代碼

css代碼:

#parent {
    height: 500px;
    display: grid;
    grid-template-columns: 100px 200px auto; /*設置3列,固定第一第二列的寬度,第三列auto或者1fr*/
}
#left {
    margin-right: 10px;  /*間距*/
    background-color: #f00;
}
#center {
    margin-right: 10px;  /*間距*/
    background-color: #eeff2b;
}
#right {
    background-color: #0f0;
}

複製代碼

5.2 兩側定寬,中間自適應

5.2.1 雙飛翼佈局方法

效果圖:

image.png

html代碼:

<body>
<div id="header"></div>
<!--中間欄須要放在前面-->
<div id="parent">
    <div id="center">
        <div id="center_inbox">中間自適應</div>
        <hr>  <!--方便觀察原理-->
    </div>
    <div id="left">左列定寬</div>
    <div id="right">右列定寬</div>
</div>
<div id="footer"></div>
</body>

複製代碼

css代碼:

#header {
    height: 60px;
    background-color: #ccc;
}
#left {
    float: left;
    width: 100px;
    height: 500px;
    margin-left: -100%; /*調整#left的位置,值等於自身寬度*/
    background-color: #f00;
    opacity: 0.5;
}
#center {
    height: 500px;
    float: left;
    width: 100%;
    background-color: #eeff2b;
}
#center_inbox{
    height: 480px;
    border: 1px solid #000;
    margin: 0 220px 0 120px;  /*關鍵!!!左右邊界等於左右盒子的寬度,多出來的爲盒子間隔*/
}
#right {
    float: left;
    width: 200px;
    height: 500px;
    margin-left: -200px;  /*使right到指定的位置,值等於自身寬度*/
    background-color: #0f0;
    opacity: 0.5;
}
#footer {
    clear: both;  /*注意清除浮動!!*/
    height: 60px;
    background-color: #ccc;
}

複製代碼
5.2.2 聖盃佈局方法

效果圖:

image.png

html代碼:

<body>
<div id="header"></div>
<div id="parent">
    <!--#center須要放在前面-->
    <div id="center">中間自適應
        <hr>
    </div>
    <div id="left">左列定寬</div>
    <div id="right">右列定寬</div>
</div>
<div id="footer"></div>
</body>

複製代碼

css代碼:

#header{
    height: 60px;
    background-color: #ccc;
}
#parent {
    box-sizing: border-box;
    height: 500px;
    padding: 0 215px 0 115px;  /*爲了使#center擺正,左右padding分別等於左右盒子的寬,能夠結合左右盒子相對定位的left調整間距*/
}
#left {
    margin-left: -100%;  /*使#left上去一行*/
    position: relative;
    left: -115px;  /*相對定位調整#left的位置,正值大於或等於自身寬度*/
    float: left;
    width: 100px;
    height: 500px;
    background-color: #f00;
    opacity: 0.5;
}
#center {
    float: left;
    width: 100%;  /*因爲#parent的padding,達到自適應的目的*/
    height: 500px;
    box-sizing: border-box;
    border: 1px solid #000;
    background-color: #eeff2b;
}
#right {
    position: relative;
    left: 215px; /*相對定位調整#right的位置,大於或等於自身寬度*/
    width: 200px;
    height: 500px;
    margin-left: -200px;  /*使#right上去一行*/
    float: left;
    background-color: #0f0;
    opacity: 0.5;
}
#footer{
    height: 60px;
    background-color: #ccc;
}

複製代碼
5.2.3 使用Grid實現

效果圖:

image.png

html代碼:

<body>
<div id="parent">
    <div id="header"></div>
    <!--#center須要放在前面-->
    <div id="center">中間自適應
        <hr>
    </div>
    <div id="left">左列定寬</div>
    <div id="right">右列定寬</div>
    <div id="footer"></div>
</div>
</body>

複製代碼

css代碼:

#parent {
    height: 500px;
    display: grid;
    grid-template-columns: 100px auto 200px; /*設定3列*/
    grid-template-rows: 60px auto 60px; /*設定3行*/
    /*設置網格區域分佈*/
    grid-template-areas: 
        "header header header" 
        "leftside main rightside" 
        "footer footer footer";
}
#header {
    grid-area: header; /*指定在哪一個網格區域*/
    background-color: #ccc;
}
#left {
    grid-area: leftside;
    background-color: #f00;
    opacity: 0.5;
}
#center {
    grid-area: main; /*指定在哪一個網格區域*/
    margin: 0 15px; /*設置間隔*/
    border: 1px solid #000;
    background-color: #eeff2b;
}
#right {
    grid-area: rightside; /*指定在哪一個網格區域*/
    background-color: #0f0;
    opacity: 0.5;
}
#footer {
    grid-area: footer; /*指定在哪一個網格區域*/
    background-color: #ccc;
}

複製代碼
5.2.4 其餘方法

效果圖:

image.png

(1)使用table實現

html代碼:

<body>
<div id="parent">
    <div id="left">左列定寬</div>
    <div id="center">中間自適應</div>
    <div id="right">右列定寬</div>
</div>
</body>

複製代碼

css代碼:

#parent {
    width: 100%;
    height: 500px;
    display: table;
}
#left {
    display: table-cell;
    width: 100px;
    background-color: #f00;
}
#center {
    display: table-cell;
    background-color: #eeff2b;
}
#right {
    display: table-cell;
    width: 200px;
    background-color: #0f0;
}

複製代碼

優缺點:

  • 優勢:代碼簡潔,容易理解;
  • 缺點:margin失效,採用border-spacing表格兩邊的間隔沒法消除;不支持ie8-
(2)使用flex實現

html代碼:

<body>
<div id="parent">
    <div id="left">左列定寬</div>
    <div id="center">中間自適應</div>
    <div id="right">右列定寬</div>
</div>
</body>

複製代碼

css代碼:

#parent {
    height: 500px;
    display: flex;
}
#left {
    width: 100px;
    background-color: #f00;
}
#center {
    flex: 1;  /*均分#parent剩餘的部分*/
    background-color: #eeff2b;
}
#right {
    width: 200px;
    background-color: #0f0;
}

複製代碼
(3)使用position實現

html代碼:

<body>
<div id="parent">
    <div id="left">左列定寬</div>
    <div id="center">中間自適應</div>
    <div id="right">右列定寬</div>
</div>
</body>

複製代碼

css代碼:

#parent {
    position: relative; /*子絕父相*/
}
#left {
    position: absolute;
    top: 0;
    left: 0;
    width: 100px;
    height: 500px;
    background-color: #f00;
}
#center {
    height: 500px;
    margin-left: 100px; /*大於等於#left的寬度,或者給#parent添加一樣大小的padding-left*/
    margin-right: 200px;  /*大於等於#right的寬度,或者給#parent添加一樣大小的padding-right*/
    background-color: #eeff2b;
}
#right {
    position: absolute;
    top: 0;
    right: 0;
    width: 200px;
    height: 500px;
    background-color: #0f0;
}

複製代碼

優缺點:

  • 優勢:容易理解,兼容性比較好
  • 缺點:需手動計算寬度肯定邊距;脫離文檔流;代碼繁多

6、多列布局

6.1 等寬佈局

6.1.1 四列等寬
(1)使用float實現

效果圖:

image.png

html代碼:

<body>
<div id="parent">
    <div class="column">1 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
    <div class="column">2 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
    <div class="column">3 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
    <div class="column">4 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
</div>
</body>

複製代碼

css代碼:

#parent {
    margin-left: -20px; /*使總體內容看起來居中,抵消padding-left的影響*/
}
.column{
    padding-left: 20px;  /*盒子的邊距*/
    width: 25%;
    float: left;
    box-sizing: border-box;
    border: 1px solid #000;
    background-clip: content-box; /*背景色從內容開始繪製,方便觀察*/
    height: 500px;
}
.column:nth-child(odd){
    background-color: #f00;
}
.column:nth-child(even){
    background-color: #0f0;
}

複製代碼

優缺點:

  • 優勢:代碼簡單,容易理解;兼容性較好
  • 缺點:須要手動清除浮動,不然會產生高度塌陷
(2)使用table實現

效果圖:

image.png

html代碼:

<body>
<div id="parent">
    <div class="column">1 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
    <div class="column">2 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
    <div class="column">3 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
    <div class="column">4 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
</div>
</body>

複製代碼

css代碼:

#parent {
    height: 540px;  /*抵消上下邊20*2間距的高度影響*/
    display: table;
    margin: -20px 0;  /*抵消上下邊20*2間距的位置影響*/
    /*兩邊離頁面間距較大,改用子元素設置padding來當成間隔就不會有這樣的問題*/
    border-spacing: 20px;  /*設置間距*/
}
.column{
    display: table-cell;
}
.column:nth-child(odd){
    background-color: #f00;
}
.column:nth-child(even){
    background-color: #0f0;
}

複製代碼

優缺點:

  • 優勢:代碼簡單,容易理解;無需關注寬度,單元格自動等分
  • 缺點:margin失效;設置間隔比較麻煩;不支持ie8-
(3)使用flex實現

效果圖:

image.png

html代碼:

<body>
<div id="parent">
    <div class="column">1 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
    <div class="column">2 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
    <div class="column">3 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
    <div class="column">4 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
</div>
</body>

複製代碼

css代碼:

#parent {
    margin-left: -15px;  /*使內容看起來居中*/
    height: 500px;
    display: flex;
}
.column{
    flex: 1; /*一塊兒平分#parent*/
    margin-left: 15px; /*設置間距*/
}
.column:nth-child(odd){
    background-color: #f00;
}
.column:nth-child(even){
    background-color: #0f0;
}

複製代碼
多列等寬

效果圖:

image.png

(1)使用float實現

html代碼:

<body>
<div id="parent">
    <div class="column">1 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
    <div class="column">2 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
    <div class="column">3 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
    <div class="column">4 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
    <div class="column">5 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
    <div class="column">6 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
</div>
</body>

複製代碼

css代碼:

#parent {
    height: 500px;
}
.column{
    float: left;  /*添加浮動*/
    width: 16.66666666666667%;  /*100÷列數,得出百分比*/
    height: 500px;
}
.column:nth-child(odd){
    background-color: #f00;
}
.column:nth-child(even){
    background-color: #0f0;
}

複製代碼

優缺點:

  • 優勢:代碼簡單,容易理解;兼容性較好
  • 缺點:須要手動清除浮動,不然會產生高度塌陷
(2)使用table實現

html代碼

<body>
<div id="parent">
    <div class="column">1 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
    <div class="column">2 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
    <div class="column">3 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
    <div class="column">4 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
    <div class="column">5 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
    <div class="column">6 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
</div>
</body>

複製代碼

css代碼:

#parent {
    width: 100%;
    height: 500px;
    display: table;
}
.column{
    display: table-cell; /*無需關注列數,單元格自動平分*/
}
.column:nth-child(odd){
    background-color: #f00;
}
.column:nth-child(even){
    background-color: #0f0;
}

複製代碼

優缺點:

  • 優勢:代碼簡單,容易理解;無需關注寬度。單元格自動等分
  • 缺點:margin失效;設置間隔比較麻煩;不兼容ie8-
(3)使用flex實現

html代碼:

<body>
<div id="parent">
    <div class="column">1 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
    <div class="column">2 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
    <div class="column">3 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
    <div class="column">4 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
    <div class="column">5 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
    <div class="column">6 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
</div>
</body>

複製代碼

css代碼:

#parent {
    height: 500px;
    display: flex;
}
.column{
    flex: 1;  /*無需關注列數,一塊兒平分#parent*/
}
.column:nth-child(odd){
    background-color: #f00;
}
.column:nth-child(even){
    background-color: #0f0;
}

複製代碼
(4)使用Grid實現

html代碼:

<body>
<div id="parent">
    <div class="column">1 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
    <div class="column">2 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
    <div class="column">3 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
    <div class="column">4 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
    <div class="column">5 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
    <div class="column">6 <p>我是文字我是文字我輸文字我是文字我是文字</p></div>
</div>
</body>

複製代碼

css代碼:

#parent {
    height: 500px;
    display: grid;
    grid-template-columns: repeat(6,1fr);  /*6就是列數*/
}
.column{}
.column:nth-child(odd){
    background-color: #f00;
}
.column:nth-child(even){
    background-color: #0f0;
}

複製代碼

6.2 九宮格佈局

效果圖:

image.png

(1)使用table實現

html代碼:

<body>
<div id="parent">
    <div class="row">
        <div class="item">1</div>
        <div class="item">2</div>
        <div class="item">3</div>
    </div>
    <div class="row">
        <div class="item">4</div>
        <div class="item">5</div>
        <div class="item">6</div>
    </div>
    <div class="row">
        <div class="item">7</div>
        <div class="item">8</div>
        <div class="item">9</div>
    </div>
</div>
</body>

複製代碼

css代碼:

#parent {
    width: 1200px;
    height: 500px;
    margin: 0 auto;
    display: table;
}
.row {
    display: table-row;
}
.item {
    border: 1px solid #000;
    display: table-cell;
}

複製代碼

優缺點:

  • 優勢:代碼簡潔,容易理解;
  • 缺點:margin失效,採用border-spacing表格兩邊的間隔沒法消除;不支持ie8-
(2)使用flex實現

html代碼:

<body>
<div id="parent">
    <div class="row">
        <div class="item">1</div>
        <div class="item">2</div>
        <div class="item">3</div>
    </div>
    <div class="row">
        <div class="item">4</div>
        <div class="item">5</div>
        <div class="item">6</div>
    </div>
    <div class="row">
        <div class="item">7</div>
        <div class="item">8</div>
        <div class="item">9</div>
    </div>
</div>
</body>

複製代碼

css代碼:

#parent {
    width: 1200px;
    height: 500px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
}
.row {
    display: flex;
    flex: 1;
}
.item {
    flex: 1;
    border: 1px solid #000;
}

複製代碼
(3)使用Grid實現

CSS Grid很是強大,能夠實現各類各樣的三維佈局,可查閱本文結尾的閱讀推薦

html代碼:

<body>
<div id="parent">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
    <div class="item">4</div>
    <div class="item">5</div>
    <div class="item">6</div>
    <div class="item">7</div>
    <div class="item">8</div>
    <div class="item">9</div>
</div>
</body>

複製代碼

css代碼:

#parent {
    width: 1200px;
    height: 500px;
    margin: 0 auto;
    display: grid;
    grid-template-columns: repeat(3, 1fr); /*等同於1fr 1fr 1fr,此爲重複的合併寫法*/
    grid-template-rows: repeat(3, 1fr);  /*等同於1fr 1fr 1fr,此爲重複的合併寫法*/
}
.item {
    border: 1px solid #000;
}

複製代碼

6.3 柵格系統

優缺點:

  • 優勢:代碼簡潔,容易理解;提升頁面內容的流動性,能適應多種設備;
(1)用Less生成
/*生成柵格系統*/
@media screen and (max-width: 768px){
  .generate-columns(12);     /*此處設置生成列數*/
  .generate-columns(@n, @i: 1) when (@i <= @n) {
    .column-xs-@{i} {
      width: (@i * 100% / @n);
    }
    .generate-columns(@n, (@i+1));
  }
}
@media screen and (min-width: 768px){
  .generate-columns(12);    /*此處設置生成列數*/
  .generate-columns(@n, @i: 1) when (@i <= @n) {
    .column-sm-@{i} {
      width: (@i * 100% / @n);
    }
    .generate-columns(@n, (@i+1));
  }
}
div[class^="column-xs-"]{
	float: left;
}
div[class^="column-sm-"]{
	float: left;
}
複製代碼

編譯後的CSS代碼:

@media screen and (max-width: 768px) {
  .column-xs-1 {  width: 8.33333333%;  }
  .column-xs-2 {  width: 16.66666667%;  }
  .column-xs-3 {  width: 25%;  }
  .column-xs-4 {  width: 33.33333333%;  }
  .column-xs-5 {  width: 41.66666667%;  }
  .column-xs-6 {  width: 50%;  }
  .column-xs-7 {  width: 58.33333333%;  }
  .column-xs-8 {  width: 66.66666667%;  }
  .column-xs-9 {  width: 75%;  }
  .column-xs-10 {  width: 83.33333333%;  }
  .column-xs-11 {  width: 91.66666667%;  }
  .column-xs-12 {  width: 100%;  }
}
@media screen and (min-width: 768px) {
  .column-sm-1 {  width: 8.33333333%;  }
  .column-sm-2 {  width: 16.66666667%;  }
  .column-sm-3 {  width: 25%;  }
  .column-sm-4 {  width: 33.33333333%;  }
  .column-sm-5 {  width: 41.66666667%;  }
  .column-sm-6 {  width: 50%;  }
  .column-sm-7 {  width: 58.33333333%;  }
  .column-sm-8 {  width: 66.66666667%;  }
  .column-sm-9 {  width: 75%;  }
  .column-sm-10 {  width: 83.33333333%;  }
  .column-sm-11 {  width: 91.66666667%;  }  
  .column-sm-12 {  width: 100%;  }
}
div[class^="column-xs-"]{
	float: left;
}
div[class^="column-sm-"]{
	float: left;
}

複製代碼

7、全屏佈局

效果圖:

image.png

(1)使用絕對定位實現

html代碼:

<body>
<div id="parent">
    <div id="top">top</div>
    <div id="left">left</div>
    <div id="right">right</div>
    <div id="bottom">bottom</div>
</div>
</body>

複製代碼

css代碼:

html, body, #parent {height: 100%;overflow: hidden;}
#parent > div {
    border: 1px solid #000;
}
#top {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 100px;
}
#left {
    position: absolute;
    top: 100px;  /*值大於等於#top的高度*/
    left: 0;
    bottom: 50px;  /*值大於等於#bottom的高度*/
    width: 200px;
}
#right {
    position: absolute;
    overflow: auto;
    left: 200px;  /*值大於等於#left的寬度*/
    right: 0;
    top: 100px;  /*值大於等於#top的高度*/
    bottom: 50px;  /*值大於等於#bottom的高度*/
}
#bottom {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    height: 50px;
}

複製代碼

優缺點:

  • 優勢:容易理解
  • 缺點:代碼繁多;須要計算好各個盒子的寬高;
(2)使用flex實現

html代碼:

<body>
<div id="parent">
    <div id="top">top</div>
    <div id="middle">
        <div id="left">left</div>
        <div id="right">right</div>
    </div>
    <div id="bottom">bottom</div>
</div>
</body>

複製代碼

css代碼:

*{
    margin: 0;
    padding: 0;
}
html,body,#parent{
    height:100%;
}
#parent {
    display: flex;
    flex-direction: column;
}
#top {
    height: 100px;
}
#bottom {
    height: 50px;
}
#middle {
    flex: 1;
    display: flex;
}
#left {
    width: 200px;
}
#right {
    flex: 1;
    overflow: auto;
}

複製代碼
(3)使用Grid實現

html代碼:

<body>
<div id="parent">
    <div id="top">top</div>
    <div id="left">left</div>
    <div id="right">right</div>
    <div id="bottom">bottom</div>
</div>
</body>

複製代碼

css代碼:

*{
    margin: 0;
    padding: 0;
}
html, body, #parent {
    height: 100%;
}
#parent {
    width: 100%;
    height: 100%;
    display: grid;
    /*分紅2列,第一列寬度200px,第二列1fr平分剩餘的部分,此處換成auto也行*/
    grid-template-columns: 200px 1fr;  
    /*分紅3行,第一行高度100px,第二行auto爲自適應,此處換成1fr也行,第3行高度爲50px*/
    grid-template-rows: 100px auto 50px; 
    /*定義網格區域分佈*/
    grid-template-areas:
        "header header"
        "aside main"
        "footer footer";
}
#parent>div{
    border: 1px solid #000;
}
#top{
    grid-area: header;  /*指定在哪一個網格區域*/
}
#left{
    grid-area: aside;  /*指定在哪一個網格區域*/
}
#right{
    grid-area: main;  /*指定在哪一個網格區域*/
}
#bottom{
    grid-area: footer;  /*指定在哪一個網格區域*/
}

複製代碼

8、網站實例佈局分析:

因爲方法衆多,分析的時候想到哪一種用哪一種了,只要IE9和谷歌上表現一致,我就不一一測試其餘瀏覽器了,若是有什麼問題或意見,請留言!

8.1 小米官網

https://www.mi.com/

8.1.1 兼容IE9+的方法
(1)頁面總體

整個頁面咱們能夠分紅頂、上、中、下、底五個結構,如圖所示:

image.png

html代碼:

<body>
<div class="header"></div>
<div class="top"></div>
<div class="center"></div>
<div class="bottom"></div>
<div class="footer"></div>
</body>

複製代碼

css代碼:

*{  /*爲了方便,就這樣清空默認樣式了*/
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    list-style: none;
}
body{
    background-color: #f5f5f5;
}
.header{
    margin-bottom: 20px;
    height: 40px;
    background-color: #333;
}
.top{
    height: 1210px;
    background-color: #fff;
}
.center{
    width: 1226px;
    margin: 0 auto;
    margin-bottom: 60px;
    height: 1791px;
    background-color: #fff;
}
.bottom{
    height: 274px;
    background-color: #fff;
}
.footer{
    margin: 0 auto;
    width: 1226px;
    height: 166px;
    border: 1px solid #000;
}

複製代碼
(2)局部——header

header部分首先是一個水平居中的內容,內容盒子能夠分紅左右兩個部分,如圖所示:

image.png

html代碼:

<div class="header">
    <div class="container">
        <div class="header-left"></div>
        <div class="header-rigth"></div>
    </div>
</div>

複製代碼

css代碼:

.container{  /*後面會繼續用到*/
    width: 1226px;
    height: 100%;
    margin: 0 auto;
    border: 1px solid #f00;
}
.header-left{
    width: 380px;
    height: 100%;
    float: left;
    background-color: #0f0;
}
.header-rigth{
    width: 260px;
    height: 100%;
    float: right;
    background-color: #0f0;
}

複製代碼
(3)局部——top

top部分先有一個水平居中的內容,再就是內容由上到下能夠分紅四個部分,而後每一個部分再細分......說不下去了,直接上圖:

image.png

html代碼:

<div class="top">
    <div class="container">
        <div class="top-nav"></div>
        <div class="top-slider">
            <div class="slider-navbar"></div>
        </div>
        <div class="top-recommend">
            <div class="recommend-left"></div>
            <div class="recommend-right">
                <ul>
                    <li></li>
                    <li></li>
                    <li></li>
                </ul>
            </div>
        </div>
        <div class="top-flashsale">
            <div class="flashsale-title"></div>
            <div class="flashsale-content">
                <div class="content-timer"></div>
                <ul class="content-shops">
                    <li></li>
                    <li></li>
                    <li></li>
                    <li></li>
                </ul>
            </div>
        </div>
    </div>
</div>

複製代碼

css代碼:

.top {
    height: 1210px;
    background-color: #fff;
}
.top-nav {
    height: 100px;
    background-color: #f00;
}
.top-slider {
    margin-bottom: 14px;
    position: relative;
    height: 460px;
    background-color: #00f;
}
.slider-navbar {
    position: absolute;
    top: 0;
    left: 0;
    width: 234px;
    height: 100%;
    background-color: black;
    opacity: .5;
}
.top-recommend {
    margin-bottom: 26px;
    height: 170px;
    background-color: #0f0;
}
.recommend-left {
    float: left;
    height: 100%;
    width: 234px;
    background-color: skyblue;
}
.recommend-right {
    float: right;
    width: 978px;
    height: 100%;
    border: 1px solid #000;
}
.recommend-right > ul {
    height: 100%;
}
.recommend-right > ul li {
    float: left;
    width: 316px;
    height: 100%;
    background-color: deepskyblue;
}
.recommend-right > ul li + li {
    margin-left: 14px;
}
.top-flashsale {
    height: 438px;
    background-color: #ff4455;
}
.flashsale-title {
    height: 58px;
    background-color: purple;
}
.flashsale-content {
    border: 1px solid #000;
    padding-bottom: 40px;
    height: 380px;
}
.content-timer {
    margin-right: 14px;
    float: left;
    width: 234px;
    height: 100%;
    background-color: #fff;
}
.content-shops {
    overflow: hidden;
    height: 100%;
    background-color: #6effb1;
}
.content-shops > li {
    float: left;
    width: 234px;
    height: 100%;
    background-color: #fff;
}
.content-shops > li+li {
    margin-left: 12.5px;
}

複製代碼
(4)局部——center

center部分都是一些單元格展現,有不少相似的模塊,就挑幾個來實現了,直接看圖吧:

image.png

image.png

image.png

html代碼:

<div class="center">
    <div class="center-phone">
        <div class="phone-title"></div>
        <div class="phone-content">
            <div class="phone-left"></div>
            <ul class="phone-right">
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
            </ul>
        </div>
    </div>

    <div class="center-household">
        <div class="household-title"></div>
        <div class="household-content">
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li>
                <p></p>
                <p></p>
            </li>
        </div>
    </div>

    <div class="center-video">
        <div class="video-title"></div>
        <ul class="video-content">
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>
</div>

複製代碼

css代碼:

.center {
    margin: 0 auto;
    margin-bottom: 60px;
    padding-top: 60px;
    width: 1226px;
    height: 1791px;
    background-color: #fff;
}
.center-phone{
    margin-bottom: 8px;
    height: 686px;
    background-color: yellow;
}
.phone-title{
    height: 58px;
    background-color: black;
}
.phone-content{
    height: 628px;
    background-color: pink;
}
.phone-left{
    margin-right: 14px;
    float: left;
    width: 234px;
    height: 100%;
    background-color: darkseagreen;
}
.phone-right{
    overflow: hidden;
    height: 100%;
    background-color: #ccc;
}
.phone-right>li{
    margin-bottom: 28px;
    padding-left: 14px;
    float: left;
    width: 25%;
    height: 300px;
    border: 1px solid #000;
    background-color: #f00;
    background-clip: content-box;
}
.phone-right>li:nth-child(1),
.phone-right>li:nth-child(5){
    margin-left: 0;
}
.center-household{
    margin-bottom: 8px;
    height: 686px;
    background-color: yellow;
}
.household-title{
    height: 58px;
    background-color: black;
}
.household-content{
    height: 614px;
}
.household-content>li{
    position: relative;
    margin-left: 14px;
    margin-bottom: 28px;
    float: left;
    width: 234px;
    height: 300px;
    background-color: #d7d7d7;
}
.household-content>li:nth-child(1),
.household-content>li:nth-child(6){
    margin-left: 0;
}
.household-content>li:last-child p:first-child{
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 143px;
    border: 1px solid #000;
}
.household-content>li:last-child p:last-child{
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 143px;
    border: 1px solid #000;
}
.center-video{
    height: 343px;
    background-color: pink;
}
.video-title{
    height: 58px;
    background-color: black;
}
.video-content{
    height: 285px;
}
.video-content>li{
    float: left;
    width: 296px;
    height: 100%;
    border: 1px solid #000;
}
.video-content>li+li{
    margin-left: 14px;
}

複製代碼
(5)局部——bottom

bottom部分首先是一個水平居中的內容,而後內容能夠劃分爲上下兩部分,每一個部分都是浮動的li,如圖:

image.png

html代碼:

<div class="bottom">
    <div class="container">
        <div class="bottom-service">
            <ul>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
            </ul>
        </div>
        <div class="bottom-links">
            <div class="links-left">
                <ul>
                    <li></li>
                    <li></li>
                    <li></li>
                    <li></li>
                    <li></li>
                    <li></li>
                </ul>
            </div>
            <div class="links-right"></div>
        </div>
    </div>
</div>

複製代碼

css代碼:

.bottom {
    height: 274px;
    background-color: #fff;
}
.bottom-service{
    height: 80px;
    background-color: seagreen;
}
.bottom-service>ul{
    height: 100%;
}
.bottom-service>ul li{
    position: relative;
    padding: 0 50px;
    float: left;
    width: 20%;
    height: 100%;
    background-color: goldenrod;
    background-clip: content-box;
}
.bottom-service>ul li+li::before{
    position: absolute;
    top: 28px;
    left: 0;
    content: '';
    width: 1px;
    height: 24px;
    background-color: #999;
}
.bottom-links{
    height: 192px;
    background-color: #8545e0;
}
.links-left{
    float: left;
    width: 960px;
    height: 100%;
    background-color: yellow;
}
.links-left>ul{
    height: 100%;
}
.links-left>ul li{
    padding-right: 60px;
    float: left;
    width: 16.666666666666667%;
    height: 100%;
    border: 1px solid #000;
    background-color: #ff0000;
    background-clip: content-box;
}
.links-right{
    float: right;
    width: 252px;
    height: 100%;
    background-color: yellow;
}

複製代碼
(6)局部——footer

footer劃分如圖:

image.png

html代碼:

<div class="footer">
    <div class="footer-info">
        <div class="info-left"></div>
        <div class="info-right"></div>
    </div>
    <div class="footer-slogan"></div>
</div>

複製代碼

css代碼:

.footer {
    margin: 0 auto;
    padding: 30px 0;
    width: 1226px;
    height: 166px;
    border: 1px solid #000;
}
.footer-info{
    height: 57px;
    background-color: #6effb1;
}
.info-left{
    float: left;
    width: 630px;
    height: 100%;
    border: 1px solid #000;
}
.info-right{
    float: right;
    width: 436px;
    height: 100%;
    border: 1px solid #000;
}
.footer-slogan{
    margin-top: 30px;
    height: 19px;
    background-color: #8545e0;
}

複製代碼
(7)所有代碼(優化後)

html代碼:

<body>
<div class="header">
    <div class="container">
        <div class="header-left fl"></div>
        <div class="header-rigth fr"></div>
    </div>
</div>
<div class="top">
    <div class="container">
        <div class="top-nav"></div>
        <div class="top-slider">
            <div class="slider-navbar"></div>
        </div>
        <div class="top-recommend">
            <div class="recommend-left fl"></div>
            <div class="recommend-right fr">
                <ul>
                    <li></li>
                    <li></li>
                    <li></li>
                </ul>
            </div>
        </div>
        <div class="top-flashsale">
            <div class="flashsale-title common-title"></div>
            <div class="flashsale-content">
                <div class="content-timer fl"></div>
                <ul class="content-shops">
                    <li></li>
                    <li></li>
                    <li></li>
                    <li></li>
                </ul>
            </div>
        </div>
    </div>
</div>
<div class="center">
    <div class="center-phone module-box">
        <div class="phone-title common-title"></div>
        <div class="phone-content">
            <div class="phone-left fl"></div>
            <ul class="phone-right">
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
            </ul>
        </div>
    </div>

    <div class="center-household module-box">
        <div class="household-title common-title"></div>
        <div class="household-content">
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li>
                <p></p>
                <p></p>
            </li>
        </div>
    </div>

    <div class="center-video">
        <div class="video-title common-title"></div>
        <ul class="video-content">
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>
</div>
<div class="bottom">
    <div class="container">
        <div class="bottom-service">
            <ul>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
            </ul>
        </div>
        <div class="bottom-links">
            <div class="links-left fl">
                <ul>
                    <li></li>
                    <li></li>
                    <li></li>
                    <li></li>
                    <li></li>
                    <li></li>
                </ul>
            </div>
            <div class="links-right fr"></div>
        </div>
    </div>
</div>
<div class="footer">
    <div class="footer-info">
        <div class="info-left fl"></div>
        <div class="info-right fr"></div>
    </div>
    <div class="footer-slogan"></div>
</div>
</body>

複製代碼

css代碼:

/*-------------------抽取公共樣式-----------------*/
* { /*爲了方便,就這樣清空默認樣式了*/
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    list-style: none;
}
body {
    background-color: #f5f5f5;
}
.container {  /*水平居中的內容盒子*/
    width: 1226px;
    height: 100%;
    margin: 0 auto;
    border: 1px solid #f00;
}
.common-title {
    height: 58px;
    background-color: #000;
}
.fl {float: left;}
.fr {float: right;}
.recommend-right,
.flashsale-content,
.phone-right > li,
.household-content > li:last-child > p,
.video-content > li,
.links-left > ul li,
.footer,
.info-left,
.info-right {border: 1px solid #000;} /*添加邊框樣式只是爲了方便觀察,不是佈局必須,可刪*/


/*-----header部分-----*/
.header {
    margin-bottom: 20px;
    height: 40px;
    background-color: #333;
}
.header-left {
    width: 380px;
    height: 100%;
    background-color: #0f0;
}
.header-rigth {
    width: 260px;
    height: 100%;
    background-color: #0f0;
}


/*--------top部分--------*/
.top {
    /*height: 1210px;*/
    background-color: #fff;
}
.top-nav {
    height: 100px;
    background-color: #f00;
}
.top-slider {
    margin-bottom: 14px;
    position: relative; /*父相*/
    height: 460px;
    background-color: #00f;
}
.slider-navbar {
    position: absolute; /*子絕*/
    top: 0;
    left: 0;
    width: 234px;
    height: 100%;
    background-color: black;
    opacity: .5;
}
.top-recommend {
    margin-bottom: 26px;
    height: 170px;
    background-color: #0f0;
}
.recommend-left {
    height: 100%;
    width: 234px;
    background-color: skyblue;
}
.recommend-right {
    width: 978px;
    height: 100%;
}
.recommend-right > ul {height: 100%;}
.recommend-right > ul li {
    float: left; /*三列等寬,浮動佈局*/
    width: 316px;
    height: 100%;
    background-color: deepskyblue;
}
.recommend-right > ul li + li { margin-left: 14px;}  /*設置浮動間隔*/
.top-flashsale {
    height: 438px;
    background-color: #ff4455;
}
.flashsale-title {}
.flashsale-content {
    padding-bottom: 40px;
    height: 380px;
}
.content-timer {
    margin-right: 14px;
    width: 234px;
    height: 100%;
    background-color: #fff;
}
.content-shops {
    overflow: hidden; /*觸發bfc,以達到自適應*/
    height: 100%;
    background-color: #6effb1;
}
.content-shops > li {
    float: left; /*四列等寬,浮動佈局*/
    width: 234px;
    height: 100%;
    background-color: #fff;
}
.content-shops > li + li {margin-left: 12.5px;}  /*設置浮動間隔*/


/*--------center部分--------*/
.module-box {  /*相似的模塊*/
    margin-bottom: 8px;
    height: 686px;
}
.center {
    margin: 0 auto;
    margin-bottom: 60px;
    padding-top: 60px;
    width: 1226px;
    /*height: 1791px;*/
    background-color: #fff;
}
.center-phone {background-color: yellow;}
.phone-title {}
.phone-content {
    height: 628px;
    background-color: pink;
}
.phone-left {
    width: 234px;
    height: 100%;
    background-color: darkseagreen;
}
.phone-right {
    overflow: hidden; /*觸發bfc以達到自適應*/
    height: 100%;
    background-color: #ccc;
}
.phone-right > li {
    margin-bottom: 28px; /*設置下邊距*/
    padding-left: 14px; /*用padding模擬盒子間隔*/
    float: left; /*四列等寬,浮動佈局*/
    width: 25%;
    height: 300px;
    background-color: #f00;
    background-clip: content-box; /*背景色從content開始繪起*/
}
.center-household {background-color: yellow;}
.household-title {}
.household-content {height: 614px;}
.household-content > li {
    position: relative; /*父相*/
    margin-left: 14px; /*設置浮動間隔*/
    margin-bottom: 28px; /*設置下邊距*/
    float: left; /*五列等寬,浮動佈局*/
    width: 234px;
    height: 300px;
    background-color: #d7d7d7;
}
.household-content > li:nth-child(1),
.household-content > li:nth-child(6) {margin-left: 0; }  /*消除每行第一個的間隔*/
.household-content > li:last-child p:first-child {
    position: absolute; /*子絕*/
    top: 0;
    left: 0;
    right: 0;
    height: 143px;
}
.household-content > li:last-child p:last-child {
    position: absolute; /*子絕*/
    bottom: 0;
    left: 0;
    right: 0;
    height: 143px;
}
.center-video {
    height: 343px;
    background-color: pink;
}
.video-title {}
.video-content {height: 285px;}
.video-content > li {
    float: left; /*四列等寬,浮動佈局*/
    width: 296px;
    height: 100%;
}
.video-content > li + li {margin-left: 14px; }   /*設定浮動間隔*/


/*--------bottom部分--------*/
.bottom {
    /*height: 274px;*/
    background-color: #fff;
}
.bottom-service {
    height: 80px;
    background-color: seagreen;
}
.bottom-service > ul {height: 100%;}
.bottom-service > ul li {
    position: relative; /*父相*/
    padding: 0 50px; /*用padding模擬盒子間隔*/
    float: left; /*五列等寬,浮動佈局*/
    width: 20%;
    height: 100%;
    background-color: goldenrod;
    background-clip: content-box; /*背景色從content開始繪起*/
}
.bottom-service > ul li + li::before { /*用僞元素模擬分割線*/
    position: absolute; /*子絕*/
    top: 28px;
    left: 0;
    content: ''; /*僞元素必須有content*/
    width: 1px;
    height: 24px;
    background-color: #999;
}
.bottom-links {
    height: 192px;
    background-color: #8545e0;
}
.links-left {
    width: 960px;
    height: 100%;
    background-color: yellow;
}
.links-left > ul {height: 100%;}
.links-left > ul li {
    padding-right: 60px;
    float: left; /*六列等寬,浮動佈局*/
    width: 16.666666666666667%;
    height: 100%;
    background-color: #ff0000;
    background-clip: content-box; /*背景色從content開始繪起*/
}
.links-right {
    width: 252px;
    height: 100%;
    background-color: yellow;
}


/*--------footer部分---------*/
.footer {
    margin: 0 auto;
    padding: 30px 0;
    width: 1226px;
    height: 166px;
}
.footer-info {
    height: 57px;
    background-color: #6effb1;
}
.info-left {
    width: 630px;
    height: 100%;
}
.info-right {
    width: 436px;
    height: 100%;
}
.footer-slogan {
    margin-top: 30px;
    height: 19px;
    background-color: #8545e0;
}

複製代碼

以上就是優化後的代碼了,因爲在下才疏學淺,所用方法不敢保證是最簡單的,優化也確定不是最優的,僅僅是的個人一種思路而已,各位參考參考就好。

8.1.2 Flexbox+Grid搭配用法(將來...)

html代碼:

<body>
<div class="header">
    <div class="container">
        <div class="header-left"></div>
        <div class="header-rigth"></div>
    </div>
</div>
<div class="top">
    <div class="container">
        <div class="top-nav"></div>
        <div class="top-slider">
            <div class="slider-navbar"></div>
        </div>
        <div class="top-recommend-left"></div>
        <div class="top-recommend-right">
            <ul>
                <li></li>
                <li></li>
                <li></li>
            </ul>
        </div>
        <div class="top-flashsale-title"></div>
        <div class="top-flashsale-timer"></div>
        <ul class="top-flashsale-shops">
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>
</div>
<div class="center">
    <div class="center-phone-title"></div>
    <div class="center-phone-content">
        <div class="phone-content-item1"></div>
        <div class="phone-content-item2"></div>
        <div class="phone-content-item3"></div>
        <div class="phone-content-item4"></div>
        <div class="phone-content-item5"></div>
        <div class="phone-content-item6"></div>
        <div class="phone-content-item7"></div>
        <div class="phone-content-item8"></div>
        <div class="phone-content-item9"></div>
    </div>

    <div class="center-household-title"></div>
    <div class="center-household-content">
        <div class="row">
            <div class="household-content-item"></div>
            <div class="household-content-item"></div>
            <div class="household-content-item"></div>
            <div class="household-content-item"></div>
            <div class="household-content-item"></div>
        </div>
        <div class="row">
            <div class="household-content-item"></div>
            <div class="household-content-item"></div>
            <div class="household-content-item"></div>
            <div class="household-content-item"></div>
            <div class="household-content-item">
                <p></p>
                <p></p>
            </div>
        </div>


    </div>

    <div class="center-video-title"></div>
    <ul class="center-video-content">
        <li></li>
        <li></li>
        <li></li>
        <li></li>
    </ul>
</div>
<div class="bottom">
    <div class="container">
        <div class="bottom-service">
            <div class="service-item"></div>
            <div class="service-item"></div>
            <div class="service-item"></div>
            <div class="service-item"></div>
            <div class="service-item"></div>
        </div>
        <div class="bottom-links-left">
            <div class="links-left-item"></div>
            <div class="links-left-item"></div>
            <div class="links-left-item"></div>
            <div class="links-left-item"></div>
            <div class="links-left-item"></div>
            <div class="links-left-item"></div>
        </div>
        <div class="bottom-links-right"></div>
    </div>
</div>
<div class="footer">
    <div class="footer-info">
        <div class="info-left"></div>
        <div class="info-right"></div>
    </div>
    <div class="footer-slogan"></div>
</div>
</body>

複製代碼

css代碼:

/*-------------------抽取公共樣式-----------------*/
* { /*爲了方便,就這樣清空默認樣式了*/
    margin: 0;
    padding: 0;
    list-style: none;
}
body {
    background-color: #f5f5f5;
    display: grid;
    /*總體佈局 設置網格列,設置網格行,再設定網格區域*/
    grid-template-columns: 1fr 1226px 1fr;
    grid-template-rows: 40px 20px auto auto 274px 166px;
    grid-template-areas:
        "header header header"
        ". . ." 
        "top top top"
        ". center ."
        "bottom bottom bottom"
        ". footer .";
}
.container { /*水平居中的內容盒子*/
    width: 1226px;
    height: 100%;
    margin: 0 auto;
    border: 1px solid #f00;
}
.top-recommend-right,
.top-flashsale-timer,
.top-flashsale-shops li,
.center-phone-content > div,
.center-household-content .row,
.household-content-item:last-of-type p,
.center-video-content li,
.service-item,
.links-left-item,
.info-left,
.info-right,
.info-right {border: 1px solid #000;} /*添加邊框樣式只是爲了方便觀察,不是佈局必須,可刪*/


/*-----header部分-----*/
.header {
    grid-area: header; /*指定網格區域*/
    background-color: #333;
}
.header .container {
    display: flex;
    justify-content: space-between;
}
.header-left {
    width: 380px;
    background-color: #0f0;
}
.header-rigth {
    width: 260px;
    background-color: #0f0;
}


/*--------top部分--------*/
.top {
    grid-area: top; /*指定網格區域*/
    background-color: #fff;
}
.top .container {
    display: grid;
    /*top部分佈局 設置網格行,設置網格列,再設定網格區域*/
    grid-template-rows: 100px 460px 14px 170px 26px 58px 340px 40px;
    grid-template-columns: auto 14px 978px;
    grid-template-areas:
        "top-nav top-nav top-nav"
        "top-slider top-slider top-slider"
        ". . ."
        "recommend-left . recommend-right"
        ". . ."
        "flashsale-title flashsale-title flashsale-title"
        "flashsale-timer . flashsale-shops"
        ". . .";
}
.top-nav {
    grid-area: top-nav;
    background-color: #f00;
}
.top-slider {
    position: relative;
    grid-area: top-slider;
    background-color: #00f;
}
.top-slider .slider-navbar {
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    width: 234px;
    background-color: black;
    opacity: .5;
}
.top-recommend-left {
    grid-area: recommend-left;
    background-color: skyblue;
}
.top-recommend-right {grid-area: recommend-right;}
.top-recommend-right > ul {
    display: flex;
    justify-content: space-between;
    height: 100%;
}
.top-recommend-right li {
    width: 316px;
    background-color: deepskyblue;
}
.top-flashsale-title {
    grid-area: flashsale-title;
    background-color: #000;
}
.top-flashsale-timer {
    grid-area: flashsale-timer;
    background-color: #fff;
}
.top-flashsale-shops {
    display: flex;
    justify-content: space-between;
    grid-area: flashsale-shops;
    background-color: #6effb1;
}
.top-flashsale-shops li {width: 234px;}


/*--------center部分--------*/
.center {
    margin-bottom: 60px;  /*邊距能夠在網格分區的時候考慮進去,把邊距設成一行或一列,不要放內容就行了*/
    padding-top: 60px;
    grid-area: center; /*指定網格區域*/
    display: flex;
    flex-direction: column;
    background-color: #fff;
}
.center-phone-title {
    height: 58px;
    background-color: black;
}
.center-phone-content {
    margin-bottom: 8px;
    display: grid;
    /*這裏用flex分格更好,代碼更少更簡潔*/
    grid-template-columns: repeat(5, 1fr);
    grid-template-rows: repeat(2, 1fr);
    grid-template-areas:
        "big1 normal2 normal3 normal4 normal5"
        "big1 normal6 normal7 normal8 normal9";
    grid-gap: 14px; /*網格間隔*/
    height: 628px;
    background-color: pink;
}
.phone-content-item1 {grid-area: big1;}
.phone-content-item2 {grid-area: normal2;}
.phone-content-item3 {grid-area: normal3;}
.phone-content-item4 {grid-area: normal4;}
.phone-content-item5 {grid-area: normal5;}
.phone-content-item6 {grid-area: normal6;}
.phone-content-item7 {grid-area: normal7;}
.phone-content-item8 {grid-area: normal8;}
.phone-content-item9 {grid-area: normal9;}
.center-household-title {
    height: 58px;
    background-color: black;
}
.center-household-content {
    margin-bottom: 8px;
    display: flex;
    flex-direction: column;
    height: 614px;
    background-color: pink;
}
.center-household-content .row {
    display: flex;
    justify-content: space-between;
    flex: 1;
}
.row .household-content-item {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    width: 234px;
    background-color: #fff;
}
.household-content-item:last-of-type p {height: 143px;}
.center-video-title {
    height: 58px;
    background-color: black;
}
.center-video-content {
    display: flex;
    justify-content: space-between;
    height: 285px;
    background-color: pink;
}
.center-video-content li {width: 296px;}


/*--------bottom部分--------*/
.bottom {
    grid-area: bottom; /*指定網格區域*/
    background-color: #fff;
}
.bottom .container {
    display: grid;
    grid-template-columns: auto 252px;
    grid-template-rows: 80px auto;
    grid-template-areas: "service service" "links-left links-right";
}
.container .bottom-service {
    display: flex;
    grid-area: service;
    background-color: seagreen;
}
.service-item {flex: 1;}
.container .bottom-links-left {
    display: flex;
    grid-area: links-left;
    background-color: yellow;
}
.links-left-item {flex: 1;}
.container .bottom-links-right {
    grid-area: links-right;
    background-color: yellowgreen;
}


/*--------footer部分---------*/
.footer {
    padding: 30px 0;
    grid-area: footer; /*指定網格區域*/
}
.footer-info {
    display: flex;
    justify-content: space-between;
    height: 57px;
    background-color: #6effb1;
}
.info-left {width: 630px;}
.info-right {width: 436px;}
.footer-slogan {
    margin-top: 30px;
    height: 19px;
    background-color: #8545e0;
}

複製代碼

9、其餘補充:

9.1 移動端viewport

設置viewport:
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">

複製代碼
閱讀推薦:

解讀 viewport—網頁自適應移動 app 神器

https://juejin.im/entry/58e750a02f301e0062367ded

9.2 媒體查詢

代碼示例:
@media (max-width: 767px) { ...css代碼... }
@media (min-width: 768px) and (max-width: 991px) { ...css代碼... }
@media (min-width: 992px) and (max-width: 1199px) { ...css代碼... }
@media (min-width: 1200px) { ...css代碼... }

複製代碼
閱讀推薦:

MDN文檔介紹

https://developer.mozilla.org/zh-CN/docs/Web/Guide/CSS/Media_queries

隨方逐圓 -- 全面理解 CSS 媒體查詢

https://juejin.im/entry/595b6208f265da6c3902041e

9.3 REM

閱讀推薦:

Rem佈局的原理解析

http://yanhaijing.com/css/2017/09/29/principle-of-rem-layout/

rem是如何實現自適應佈局的?

http://caibaojian.com/web-app-rem.html

9.4 Flexbox

閱讀推薦

理解Flexbox:你須要知道的一切

https://www.w3cplus.com/css3/understanding-flexbox-everything-you-need-to-know.html

深刻理解 flex 佈局以及計算

https://www.w3cplus.com/css3/flexbox-layout-and-calculation.html?from=groupmessage

9.5 CSS Grid

閱讀推薦

grid佈局學習指南

http://blog.jirengu.com/?p=990

grid規範草稿

https://drafts.csswg.org/css-grid/

End:感謝

感謝你們閱讀,喜歡的就點個收藏或者Star吧!各位的支持,就是本人的最大動力,謝謝!

相關文章
相關標籤/搜索