CSS佈局解決方案(終結版)

前端佈局很是重要的一環就是頁面框架的搭建,也是最基礎的一環。在頁面框架的搭建之中,又有居中佈局、多列布局以及全局佈局,今天咱們就來總結總結前端乾貨中的CSS佈局。css

居中佈局

水平居中

1)使用inline-block+text-align
(1)原理、用法html

  • 原理:先將子框由塊級元素改變爲行內塊元素,再經過設置行內塊元素居中以達到水平居中。
  • 用法:對子框設置display:inline-block,對父框設置text-align:center。

(2)代碼實例前端

<div class="parent">
    <div class="child>DEMO</div>
</div>
.child{
    display:inline-block;
}
.parent{
    text-align:center;
}

(3)優缺點css3

  • 優勢:兼容性好,甚至能夠兼容ie六、ie7
  • 缺點:child裏的文字也會水平居中,能夠在.child添加text-align:left;還原

2)使用table+margin
(1)原理、用法web

  • 原理:先將子框設置爲塊級表格來顯示(相似 <table>),再設置子框居中以達到水平居中。
  • 用法:對子框設置display:table,再設置margin:0 auto。

(2)代碼實例瀏覽器

<div class="parent">
    <div class="child>DEMO</div>
</div>
.child {
    display:table;
    margin:0 auto;
}

(3)優缺點:框架

  • 優勢:只設置了child,ie8以上都支持
  • 缺點:不支持ie六、ie7,將div換成table

3)使用absolute+transform
(1)原理、用法佈局

  • 原理:將子框設置爲絕對定位,移動子框,使子框左側距離相對框左側邊框的距離爲相對框寬度的一半,再經過向左移動子框的一半寬度以達到水平居中。固然,在此以前,咱們須要設置父框爲相對定位,使父框成爲子框的相對框。
  • 用法:對父框設置position:relative,對子框設置position:absolute,left:50%,transform:translateX(-50%)。

(2)代碼實例性能

<div class="parent">
    <div class="child>DEMO</div>
</div>
.parent {
    position:relative;
}
.child {
    position:absolute;
    left:50%;
    transform:translateX(-50%);
}

(3)優缺點flex

  • 優勢:居中元素不會對其餘的產生影響
  • 缺點:transform屬於css3內容,兼容性存在必定問題,高版本瀏覽器須要添加一些前綴

4)使用flex+margin
(1)原理、用法

  • 原理:經過CSS3中的佈局利器flex將子框轉換爲flex item,再設置子框居中以達到居中。
  • 用法:先將父框設置爲display:flex,再設置子框margin:0 auto。

(2)代碼實例

<div class="parent">
    <div class="child>DEMO</div>
</div>
.parent {
    display:flex;
}
.child {
    margin:0 auto;
}

(3)優缺點

  • 缺點:低版本瀏覽器(ie6 ie7 ie8)不支持

5)使用flex+justify-content
(1)原理、用法

  • 原理:經過CSS3中的佈局利器flex中的justify-content屬性來達到水平居中。
  • 用法:先將父框設置爲display:flex,再設置justify-content:center。

(2)代碼實例

<div class="parent">
    <div class="child>DEMO</div>
</div>
.parent {
    display:flex;
    justify-content:center;
}

(3)優缺點

  • 優勢:設置parent便可
  • 缺點:低版本瀏覽器(ie6 ie7 ie8)不支持

垂直居中

1)使用table-cell+vertical-align
(1)原理、用法

  • 原理:經過將父框轉化爲一個表格單元格顯示(相似 <td> 和 <th>),再經過設置屬性,使表格單元格內容垂直居中以達到垂直居中。
  • 用法:先將父框設置爲display:table-cell,再設置vertical-align:middle。

(2)代碼實例

<div class="parent">
    <div class="child>DEMO</div>
</div>
.parent {
    display:table-cell;
    vertical-align:middle;
}

(3)優缺點

  • 優勢:兼容性較好,ie8以上均支持

2)使用absolute+transform
(1)原理、用法

  • 原理:相似於水平居中時的absolute+transform原理。將子框設置爲絕對定位,移動子框,使子框上邊距離相對框上邊邊框的距離爲相對框高度的一半,再經過向上移動子框的一半高度以達到垂直居中。固然,在此以前,咱們須要設置父框爲相對定位,使父框成爲子框的相對框。
  • 用法:先將父框設置爲position:relative,再設置子框position:absolute,top:50%,transform:translateY(-50%)。

(2)代碼實例

<div class="parent">
    <div class="child>DEMO</div>
</div>
.parent {
    position:relative;
}
.child {
    position:absolute;
    top:50%;
    transform:translateY(-50%);
}

(3)優缺點

  • 優勢:居中元素不會對其餘的產生影響
  • 缺點:transform屬於css3內容,兼容性存在必定問題,高版本瀏覽器須要添加一些前綴

3)使用flex+align-items
(1)原理、用法

  • 原理:經過設置CSS3中的佈局利器flex中的屬性align-times,使子框垂直居中。
  • 用法:先將父框設置爲position:flex,再設置align-items:center。

(1)代碼實例

<div class="parent">
    <div class="child>DEMO</div>
</div>
.parent {
    position:flex;
    align-items:center;
}

(3)優缺點

  • 優勢:只設置parent
  • 缺點:兼容性存在必定問題

水平垂直居中

1)使用absolute+transform
(1)原理、用法

  • 原理:將水平居中時的absolute+transform和垂直居中時的absolute+transform相結合。詳見:水平居中的3)和垂直居中的2)。
  • 見水平居中的3)和垂直居中的2)。

(2)代碼實例

<div class="parent">
    <div class="child>DEMO</div>
</div>
.parent {
    position:relative;
}
.child {
    position:absolute;
    left:50%;
    top:50%;
    transform:tranplate(-50%,-50%);
}

(3)優缺點

  • 優勢:child元素不會對其餘元素產生影響
  • 缺點:兼容性存在必定問題

2)使用inline-block+text-align+table-cell+vertical-align
(1)原理、用法

  • 原理:使用inline-block+text-align水平居中,再用table-cell+vertical-align垂直居中,將兩者結合起來。詳見:水平居中的1)和垂直居中的1)。
  • 見水平居中的1)和垂直居中的1)。

(2)代碼實例

<div class="parent">
    <div class="child>DEMO</div>
</div>
.parent {
    text-align:center;
    display:table-cell;
    vertical-align:middle;
}
.child {
    display:inline-block;
}

(3)優缺點

  • 優勢:兼容性較好

3)使用flex+justify-content+align-items
(1)原理、用法

  • 原理:經過設置CSS3佈局利器flex中的justify-content和align-items,從而達到水平垂直居中。詳見:水平居中的4)和垂直居中的3)。
  • 見水平居中的4)和垂直居中的3)。

(2)代碼實例

<div class="parent">
    <div class="child>DEMO</div>
</div>
.parent {
    display:flex;
    justify-content:center;
    align-items:center;
}

(3)優缺點

  • 優勢:只設置了parent
  • 缺點:兼容性存在必定問題

多列布局

定寬+自適應

1)使用float+overflow
(1)原理、用法

  • 原理:經過將左邊框脫離文本流,設置右邊規定當內容溢出元素框時發生的事情以達到多列布局。
  • 用法:先將左框設置爲float:left、width、margin-left,再設置實際的右框overflow:hidden。

(2)代碼實例

<div class="parent">
    <div class="left">
        <p>left</p>
    </div>
    <div class="right">
        <p>right</p>
        <p>right</p>
    </div>
</div>
.left {
    float:left;
    width:100px;
    margin-right:20px;
}
.right {
    overflow:hidden;
}

(3)優缺點

  • 優勢:簡單
  • 缺點:不支持ie6

2)使用float+margin
(1)原理、用法

  • 原理:經過將左框脫離文本流,加上右框向右移動必定的距離,以達到視覺上的多列布局。
  • 用法:先將左框設置爲float:left、margin-left,再設置右框margin-left。

(2)代碼實例

<div class="parent">
    <div class="left">
        <p>left</p>
    </div>
    <div class="right">
        <p>right</p>
        <p>right</p>
    </div>
</div>
.left {
    float:left;
    width:100px;
}
.right {
    margin-left:120px;
}

(3)優缺點

  • 優勢:簡單,易理解
  • 缺點:兼容性存在必定問題,ie6下有3px的bug。right下的p清除浮動將產生bug

3)使用float+margin(改良版)
(1)原理、用法

  • 原理:在1)的基礎之上,經過向右框添加一個父框,再加上設置左、右父框屬性使之產生BFC以去除bug。
  • 用法:先將左框設置爲float:left、margin-left、position:relative,再設置右父框float:right、width:100%、margin-left,最後設置實際的右框margin-left。

(2)代碼實例

<div class="parent">
    <div class="left">
        <p>left</p>
    </div>
    <div class="rigth-fix">
        <div class="right">
            <p>right</p>
            <p>right</p>
        </div>
    </div>
</div>
.left {
    float:left;
    width:100px;
    position:relative;
}
.right-fix {
    float:right;
    width:100%;
    margin-left:-100px;
}
.right {
    margin-left:120px;
}

(3)優缺點

  • 優勢:簡單,易理解

4)使用table
(1)原理、用法

  • 原理:經過將父框設置爲表格,將左右邊框轉化爲相似於同一行的td,從而達到多列布局。
  • 用法:先將父框設置爲display:table、width:100%、table-layout:fixed,再設置左右框display:table-cell,最後設置左框width、padding-right。

(2)代碼實例

<div class="parent">
    <div class="left">
        <p>left</p>
    </div>
    <div class="right">
        <p>right</p>
        <p>right</p>
    </div>
</div>
.parent {
    display:table;
    width:100%;
    table-layout:fixed;
}
.left {
    width:100px;
    padding-right:20px;
}
.right,.left {
    display:table-cell;    
}

5)使用flex
(1)原理、用法

  • 原理:經過設置CSS3佈局利器flex中的flex屬性以達到多列布局。
  • 用法:先將父框設置爲display:flex,再設置左框flex:1,最後設置左框width、margin-right。

(2)代碼實例

<div class="parent">
    <div class="left">
        <p>left</p>
    </div>
    <div class="right">
        <p>right</p>
        <p>right</p>
    </div>
</div>
.parent {
    display:flex;
}
.left {
    width:100px;
    margin-right:20px;
}
.right {
    flex:1;
}

(3)優缺點

  • 優勢:flex很強大
  • 缺點:兼容性存在必定問題,性能存在必定問題

兩列定寬+一列自適應

(1)原理、用法

  • 原理:這種狀況與兩列定寬查很少。
  • 用法:先將左、中框設置爲float:left、width、margin-right,再設置右框overflow:hidden。

(2)代碼實例

<div class="parent">
    <div class="left">
        <p>left</p>
    </div>
    <div class="center">
        <p>center</p>
    </div>
    <div class="right">
        <p>right</p>
        <p>right</p>
    </div>
</div>
.left,.center {
    float:left;
    width:100px;
    margin-right:20px;
}
.right {
    overflow:hidden;
}

不定寬+自適應

1)使用float+overflow
(1)原理、用法

  • 原理:這種狀況與兩列定寬查很少。
  • 用法:先將左框設置爲float:left、margin-right,再設置右框overflow: hidden,最後設置左框中的內容width。

(2)代碼實例

<div class="parent">
    <div class="left">
        <p>left</p>
    </div>
    <div class="right">
        <p>right</p>
        <p>right</p>
    </div>
</div>
.left{
        float: left;
        margin-right: 20px;
    }
.right{
    overflow: hidden;
}
.left p{
    width: 200px;
}

(3)優缺點

  • 優勢:簡單
  • 缺點:ie6下兼容性存在必定問題

2)使用table
(1)原理、用法

  • 原理:經過將父框改變爲表格,將左右框轉換爲相似於同一行的td以達到多列布局,設置父框寬度100%,給左框子元素一個固定寬度從而達到自適應。
  • 用法:先將父框設置爲display: table、width: 100%,再設置左、右框display: table-cell,最後設置左框width: 0.1%、padding-right以及左框中的內容width。

(2)代碼實例

<div class="parent">
    <div class="left">
        <p>left</p>
    </div>
    <div class="right">
        <p>right</p>
        <p>right</p>
    </div>
</div>
.parent{
    display: table; width: 100%;
    }
.left,.right{
    display: table-cell;
}
.left{
    width: 0.1%;
    padding-right: 20px;
}
.left p{
    width:200px;
}

(3)優缺點

  • 缺點:ie6 ie7不支持

3)使用flex
(1)原理、用法

  • 原理:經過設置CSS3佈局利器flex中的flex屬性以達到多列布局,加上給左框中的內容定寬、給右框設置flex達到不定款+自適應。
  • 用法:先將父框設置爲display:flex,再設置右框flex:1,最後設置左框margin-right:20px、左框中的內容width。

(2)代碼實例

<div class="parent">
    <div class="left">
        <p>left</p>
    </div>
    <div class="right">
        <p>right</p>
        <p>right</p>
    </div>
</div>
.parent {
    display:flex;
}
.left {
    margin-right:20px;
}
.right {
    flex:1;
}
.left p{
    width: 200px;
}

(3)優缺點

  • 優勢:flex很強大
  • 缺點:兼容性存在必定問題,性能存在必定問題

兩列不定寬+一列自適應

(1)原理、用法

  • 原理:這個狀況與一列不定寬+一列自適應查很少。
  • 用法:先將左、中框設置爲float:left、margin-right,再設置右框overflow:hidden,最後給左中框中的內容設置width。

(2)代碼實例

<div class="parent">
    <div class="left">
        <p>left</p>
    </div>
    <div class="center">
        <p>center</p>
    </div>
    <div class="right">
        <p>right</p>
        <p>right</p>
    </div>
</div>
.left,.center{
    float: left;
    margin-right: 20px;
}
.right{
    overflow: hidden;
}
.left p,.center p{
    width: 100px;
}

等分佈局

圖片描述

公式轉化:
l = w * n + g * (n-1) -> l = w * n + g * n - g -> l + g = (w + g) * n

圖片描述

所以,咱們須要解決兩個問題:

  • 如何讓總寬度增長g(即:L+g)
  • 如何讓每一個寬包含g(即:w+g)

1)使用float
(1)原理、用法

  • 原理:增大父框的實際寬度後,使用CSS3屬性box-sizing進行佈局的輔助。
  • 用法:先將父框設置爲margin-left: -*px,再設置子框float: left、width: 25%、padding-left、box-sizing: border-box。

(2)代碼實例

<div class="parent">
    <div class="column"><p>1</p></div>
    <div class="column"><p>2</p></div>
    <div class="column"><p>3</p></div>
    <div class="column"><p>4</p></div>
</div>
.parent{
    margin-left: -20px;//l增長g
}
.column{
    float: left;
    width: 25%;
    padding-left: 20px;
    box-sizing: border-box;//包含padding區域 w+g
}

(3)優缺點

  • 優勢:兼容性較好
  • 缺點:ie6 ie7百分比兼容存在必定問題

2)使用table
(1)原理、用法

  • 原理:經過增長一個父框的修正框,增大其寬度,並將父框轉換爲table,將子框轉換爲tabel-cell進行佈局。
  • 用法:先將父框的修正框設置爲margin-left: -*px,再設置父框display: table、width:100%、table-layout: fixed,設置子框display: table-cell、padding-left。

(2)代碼實例

<div class="parent-fix">
    <div class="parent">
        <div class="column"><p>1</p></div>
        <div class="column"><p>2</p></div>
        <div class="column"><p>3</p></div>
        <div class="column"><p>4</p></div>
    </div>
</div>
.parent-fix{
    margin-left: -20px;//l+g
}
.parent{
    display: table;
    width:100%;
    table-layout: fixed;
}
.column{
    display: table-cell;
    padding-left: 20px;//w+g
}

(3)優缺點

  • 優勢:結構和塊數無關聯
  • 缺點:增長了一層

3)使用flex
(1)原理、用法

  • 原理:經過設置CSS3佈局利器flex中的flex屬性以達到等分佈局。
  • 用法:將父框設置爲display: flex,再設置子框flex: 1,最後設置子框與子框的間距margin-left。

(2)代碼實例

<div class="parent">
    <div class="column"><p>1</p></div>
    <div class="column"><p>2</p></div>
    <div class="column"><p>3</p></div>
    <div class="column"><p>4</p></div>
</div>
.parent{
    display: flex;
}
.column{
    flex: 1;
}
.column+.column{
    margin-left:20px;
}

(3)優缺點

  • 優勢:代碼量少,與塊數無關
  • 缺點:兼容性存在必定問題

定寬+自適應+兩塊高度同樣高

1)使用float
(1)原理、用法

  • 原理:經過過度加大左右子框的高度,輔助超出隱藏,以達到視覺上的等高。
  • 用法:將父框設置overflow: hidden,再設置左右子框padding-bottom: 9999px、margin-bottom: -9999px,最後設置左框float: left、width、margin-right,右框overflow: hidden。

(2)代碼實例

<div class="parent">
    <div class="left">
        <p>left</p>
    </div>
    <div class="right">
        <p>right</p>
        <p>right</p>
    </div>
</div>
p{
    background: none!important;
}
.left,.right{
    background: #444;
}
.parent{
    overflow: hidden;
}
.left,.right{
    padding-bottom: 9999px;
    margin-bottom: -9999px;
}
.left{
    float: left; 
    width: 100px;
    margin-right: 20px;
}
.right{
    overflow: hidden;
}

(3)優缺點

  • 優勢:兼容性好
  • 缺點:僞等高,不是真正意義上的等高

2)使用table
(1)原理、用法

  • 原理:將父框轉化爲tabel,將子框轉化爲tabel-cell佈局,以達到定寬+自適應+兩塊高度同樣高。
  • 用法:先將父框設置爲display:table、width:100%、table-layout:fixed,再設置左右框爲display:table-cell,最後設置左框width、padding-right。

(2)代碼實例

<div class="parent">
    <div class="left">
        <p>left</p>
    </div>
    <div class="right">
        <p>right</p>
        <p>right</p>
    </div>
</div>
.parent {
    display:table;
    width:100%;
    table-layout:fixed;
}
.left {
    width:100px;
    padding-right:20px;
}
.right,.left {
    display:table-cell;
}

3)使用flex
(1)原理、用法

  • 原理:經過設置CSS3佈局利器flex中的flex屬性以達到定寬+自適應+兩塊高度同樣高。
  • 用法:將父框設置爲display: flex,再設置左框width、margin-right,最後設置右框flex:1。

(2)代碼實例

<div class="parent">
    <div class="left">
        <p>left</p>
    </div>
    <div class="right">
        <p>right</p>
        <p>right</p>
    </div>
</div>
.parent {
    display:flex;
}
.left {
    width:100px;
    margin-right:20px;
}
.right {
    flex:1;
}

(3)優缺點

  • 優勢:代碼少,flex很強大
  • 缺點:兼容性存在必定問題

4)使用display
(1)原理、用法

  • 原理:經過設置display中的CSS3的-webkit-box屬性以達到定寬+自適應+兩塊高度同樣高。
  • 用法:將父框設置爲display: -webkit-box、width: 100%,再設置左框width、margin-right,最後設置右框-webkit-box-flex: 1。

(2)代碼實例

<div class="parent">
    <div class="left">left</div>
    <div class="right">right </div>
</div>
.parent {
    width: 100%;
    display: -webkit-box;
}
.left {
    width:100px;
    margin-right: 20px;
}
.right {
    -webkit-box-flex: 1;
}

(3)優缺點

  • 缺點:兼容性存在較大的問題

全屏佈局

全屏佈局的特色

  • 滾動條不是全局滾動條,而是出如今內容區域裏,每每是主內容區域
  • 瀏覽器變大時,撐滿窗口

全屏佈局的方法

圖片描述

1)使用position
(1)原理、用法

  • 原理:將上下部分固定,中間部分使用定寬+自適應+兩塊高度同樣高。
  • 用法:見實例。

(2)代碼實例

<div class="parent">
    <div class="top">top</div>
    <div class="left">left</div>
    <div class="right">
        <div class="inner">right</div>
    </div>
    <div class="bottom">bottom</div>
</div>
html,body,.parent{
    margin:0;
    height:100%;
    overflow:hidden;
}
body{
    color:white;
}
.top{
    position:absolute;
    top:0;
    left:0;
    right:0;
    height:100px;
    background:blue;
}
.left{
    position:absolute;
    left:0;
    top:100px;
    bottom:50px;
    width:200px;
    background:red;
}
.right{
    position:absolute;
    left:200px;
    top:100px;
    bottom:50px;
    right:0;
    background:pink;
    overflow: auto;
}
.right .inner{
    min-height: 1000px;
}
.bottom{
    position:absolute;
    left:0;
    right:0;
    bottom:0;
    height:50px;
    background: black;
}

(3)優缺點

  • 優勢:兼容性好,ie6下不支持

2)使用flex
(1)原理、用法

  • 原理:經過靈活使用CSS3佈局利器flex中的flex屬性和flex-direction屬性以達到全屏佈局。
  • 用法:見實例。

(2)代碼實例

<div class="parent">
    <div class="top">top</div>
    <div class="middle">
        <div class="left">left</div>
        <div class="right">
            <div class="inner">right</div>
        </div>
    </div>
    <div class="bottom">bottom</div>
</div>
html,body,.parent{
    margin:0;
    height:100%;
    overflow:hidden;
}
body{
    color: white;
} 
.parent{
    display: flex;
    flex-direction: column;
}
.top{
    height:100px;
    background: blue;
}
.bottom{
    height:50px;
    background: black;
}
.middle{
    flex:1;
    display:flex;
}
.left{
    width:200px;
    background: red;
}
.right{
    flex: 1;
    overflow: auto;
    background:pink;
}
.right .inner{
    min-height: 1000px;
}

(3)優缺點

  • 缺點:兼容性差,ie9及ie9如下不兼容

圖片描述

1)使用flex
(1)原理、用法

  • 原理:經過靈活使用CSS3佈局利器flex中的flex屬性和flex-direction屬性以達到全屏佈局。
  • 用法:見實例。

(2)代碼實例

<div class="parent">
    <div class="top">top</div>
    <div class="middle">
        <div class="left">left</div>
        <div class="right">
            <div class="inner">right</div>
        </div>
    </div>
    <div class="bottom">bottom</div>
</div>
html,body,.parent{
    margin:0;
    height:100%;
    overflow:hidden;
}
body{
    color:white;
} 
.parent{
    display:flex;
    flex-direction:column;
}
.top{
    background:blue;
}
.bottom{
    background:black;
}
.middle{
    flex:1;
    display:flex;
}
.left{
    background: red;
}
.right{
    flex:1;
    overflow:auto;
    background: pink;
}
.right .inner{
    min-height:1000px;
}

全屏佈局相關方案的兼容性、性能和自適應一覽表

方案 兼容性 性能 是否自適應
Position 部分自適應
Flex 較差 可自適應
Grid 較好 可自適應

固然,最最最最最後,若是您喜歡這片文章,能夠瘋狂點贊和收藏喔!!

相關文章
相關標籤/搜索