CSS布局解決方案(終結(jié)版)

CSS布局解決方案(終結(jié)版)

前端布局非常重要的一環(huán)就是頁面框架的搭建,也是最基礎(chǔ)的一環(huán)。在頁面框架的搭建之中,又有居中布局、多列布局以及全局布局,今天我們就來總結(jié)總結(jié)前端干貨中的CSS布局。

居中布局

水平居中

1)使用inline-block+text-align

(1)原理、用法

原理:先將子框由塊級元素改變?yōu)樾袃?nèi)塊元素,再通過設(shè)置行內(nèi)塊元素居中以達(dá)到水平居中。

用法:對子框設(shè)置display:inline-block,對父框設(shè)置text-align:center。

(2)代碼實例

<div class="parent">

? ? <div class="child>DEMO</div>

</div>

.child{

? ? display:inline-block;

}

.parent{

? ? text-align:center;

}

(3)優(yōu)缺點

優(yōu)點:兼容性好,甚至可以兼容ie6、ie7

缺點:child里的文字也會水平居中,可以在.child添加text-align:left;還原

2)使用table+margin

(1)原理、用法

原理:先將子框設(shè)置為塊級表格來顯示(類似 <table>),再設(shè)置子框居中以達(dá)到水平居中。

用法:對子框設(shè)置display:table,再設(shè)置margin:0 auto。

(2)代碼實例

<div class="parent">

? ? <div class="child>DEMO</div>

</div>

.child {

? ? display:table;

? ? margin:0 auto;

}

(3)優(yōu)缺點:

優(yōu)點:只設(shè)置了child,ie8以上都支持

缺點:不支持ie6、ie7,將div換成table

3)使用absolute+transform

(1)原理、用法

原理:將子框設(shè)置為絕對定位,移動子框,使子框左側(cè)距離相對框左側(cè)邊框的距離為相對框?qū)挾鹊囊话?,再通過向左移動子框的一半寬度以達(dá)到水平居中。當(dāng)然,在此之前,我們需要設(shè)置父框為相對定位,使父框成為子框的相對框。

用法:對父框設(shè)置position:relative,對子框設(shè)置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)優(yōu)缺點

優(yōu)點:居中元素不會對其他的產(chǎn)生影響

缺點:transform屬于css3內(nèi)容,兼容性存在一定問題,高版本瀏覽器需要添加一些前綴

4)使用flex+margin

(1)原理、用法

原理:通過CSS3中的布局利器flex將子框轉(zhuǎn)換為flex item,再設(shè)置子框居中以達(dá)到居中。

用法:先將父框設(shè)置為display:flex,再設(shè)置子框margin:0 auto。

(2)代碼實例

<div class="parent">

? ? <div class="child>DEMO</div>

</div>

.parent {

? ? display:flex;

}

.child {

? ? margin:0 auto;

}

(3)優(yōu)缺點

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

5)使用flex+justify-content

(1)原理、用法

原理:通過CSS3中的布局利器flex中的justify-content屬性來達(dá)到水平居中。

用法:先將父框設(shè)置為display:flex,再設(shè)置justify-content:center。

(2)代碼實例

<div class="parent">

? ? <div class="child>DEMO</div>

</div>

.parent {

? ? display:flex;

? ? justify-content:center;

}

(3)優(yōu)缺點

優(yōu)點:設(shè)置parent即可

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

垂直居中

1)使用table-cell+vertical-align

(1)原理、用法

原理:通過將父框轉(zhuǎn)化為一個表格單元格顯示(類似 <td> 和 <th>),再通過設(shè)置屬性,使表格單元格內(nèi)容垂直居中以達(dá)到垂直居中。

用法:先將父框設(shè)置為display:table-cell,再設(shè)置vertical-align:middle。

(2)代碼實例

<div class="parent">

? ? <div class="child>DEMO</div>

</div>

.parent {

? ? display:table-cell;

? ? vertical-align:middle;

}

(3)優(yōu)缺點

優(yōu)點:兼容性較好,ie8以上均支持

2)使用absolute+transform

(1)原理、用法

原理:類似于水平居中時的absolute+transform原理。將子框設(shè)置為絕對定位,移動子框,使子框上邊距離相對框上邊邊框的距離為相對框高度的一半,再通過向上移動子框的一半高度以達(dá)到垂直居中。當(dāng)然,在此之前,我們需要設(shè)置父框為相對定位,使父框成為子框的相對框。

用法:先將父框設(shè)置為position:relative,再設(shè)置子框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)優(yōu)缺點

優(yōu)點:居中元素不會對其他的產(chǎn)生影響

缺點:transform屬于css3內(nèi)容,兼容性存在一定問題,高版本瀏覽器需要添加一些前綴

3)使用flex+align-items

(1)原理、用法

原理:通過設(shè)置CSS3中的布局利器flex中的屬性align-times,使子框垂直居中。

用法:先將父框設(shè)置為position:flex,再設(shè)置align-items:center。

(1)代碼實例

<div class="parent">

? ? <div class="child>DEMO</div>

</div>

.parent {

? ? position:flex;

? ? align-items:center;

}

(3)優(yōu)缺點

優(yōu)點:只設(shè)置parent

缺點:兼容性存在一定問題

水平垂直居中

1)使用absolute+transform

(1)原理、用法

原理:將水平居中時的absolute+transform和垂直居中時的absolute+transform相結(jié)合。詳見:水平居中的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)優(yōu)缺點

優(yōu)點:child元素不會對其他元素產(chǎn)生影響

缺點:兼容性存在一定問題

2)使用inline-block+text-align+table-cell+vertical-align

(1)原理、用法

原理:使用inline-block+text-align水平居中,再用table-cell+vertical-align垂直居中,將二者結(jié)合起來。詳見:水平居中的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)優(yōu)缺點

優(yōu)點:兼容性較好

3)使用flex+justify-content+align-items

(1)原理、用法

原理:通過設(shè)置CSS3布局利器flex中的justify-content和align-items,從而達(dá)到水平垂直居中。詳見:水平居中的4)和垂直居中的3)。

見水平居中的4)和垂直居中的3)。

(2)代碼實例

<div class="parent">

? ? <div class="child>DEMO</div>

</div>

.parent {

? ? display:flex;

? ? justify-content:center;

? ? align-items:center;

}

(3)優(yōu)缺點

優(yōu)點:只設(shè)置了parent

缺點:兼容性存在一定問題

多列布局

定寬+自適應(yīng)

1)使用float+overflow

(1)原理、用法

原理:通過將左邊框脫離文本流,設(shè)置右邊規(guī)定當(dāng)內(nèi)容溢出元素框時發(fā)生的事情以達(dá)到多列布局。

用法:先將左框設(shè)置為float:left、width、margin-left,再設(shè)置實際的右框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)優(yōu)缺點

優(yōu)點:簡單

缺點:不支持ie6

2)使用float+margin

(1)原理、用法

原理:通過將左框脫離文本流,加上右框向右移動一定的距離,以達(dá)到視覺上的多列布局。

用法:先將左框設(shè)置為float:left、margin-left,再設(shè)置右框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)優(yōu)缺點

優(yōu)點:簡單,易理解

缺點:兼容性存在一定問題,ie6下有3px的bug。right下的p清除浮動將產(chǎn)生bug

3)使用float+margin(改良版)

(1)原理、用法

原理:在1)的基礎(chǔ)之上,通過向右框添加一個父框,再加上設(shè)置左、右父框?qū)傩允怪a(chǎn)生BFC以去除bug。

用法:先將左框設(shè)置為float:left、margin-left、position:relative,再設(shè)置右父框float:right、width:100%、margin-left,最后設(shè)置實際的右框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)優(yōu)缺點

優(yōu)點:簡單,易理解

4)使用table

(1)原理、用法

原理:通過將父框設(shè)置為表格,將左右邊框轉(zhuǎn)化為類似于同一行的td,從而達(dá)到多列布局。

用法:先將父框設(shè)置為display:table、width:100%、table-layout:fixed,再設(shè)置左右框display:table-cell,最后設(shè)置左框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)原理、用法

原理:通過設(shè)置CSS3布局利器flex中的flex屬性以達(dá)到多列布局。

用法:先將父框設(shè)置為display:flex,再設(shè)置左框flex:1,最后設(shè)置左框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)優(yōu)缺點

優(yōu)點:flex很強大

缺點:兼容性存在一定問題,性能存在一定問題

兩列定寬+一列自適應(yīng)

(1)原理、用法

原理:這種情況與兩列定寬查不多。

用法:先將左、中框設(shè)置為float:left、width、margin-right,再設(shè)置右框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;

}

不定寬+自適應(yīng)

1)使用float+overflow

(1)原理、用法

原理:這種情況與兩列定寬查不多。

用法:先將左框設(shè)置為float:left、margin-right,再設(shè)置右框overflow: hidden,最后設(shè)置左框中的內(nèi)容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)優(yōu)缺點

優(yōu)點:簡單

缺點:ie6下兼容性存在一定問題

2)使用table

(1)原理、用法

原理:通過將父框改變?yōu)楸砀?,將左右框轉(zhuǎn)換為類似于同一行的td以達(dá)到多列布局,設(shè)置父框?qū)挾?00%,給左框子元素一個固定寬度從而達(dá)到自適應(yīng)。

用法:先將父框設(shè)置為display: table、width: 100%,再設(shè)置左、右框display: table-cell,最后設(shè)置左框width: 0.1%、padding-right以及左框中的內(nèi)容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)優(yōu)缺點

缺點:ie6 ie7不支持

3)使用flex

(1)原理、用法

原理:通過設(shè)置CSS3布局利器flex中的flex屬性以達(dá)到多列布局,加上給左框中的內(nèi)容定寬、給右框設(shè)置flex達(dá)到不定款+自適應(yīng)。

用法:先將父框設(shè)置為display:flex,再設(shè)置右框flex:1,最后設(shè)置左框margin-right:20px、左框中的內(nèi)容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)優(yōu)缺點

優(yōu)點:flex很強大

缺點:兼容性存在一定問題,性能存在一定問題

兩列不定寬+一列自適應(yīng)

(1)原理、用法

原理:這個情況與一列不定寬+一列自適應(yīng)查不多。

用法:先將左、中框設(shè)置為float:left、margin-right,再設(shè)置右框overflow:hidden,最后給左中框中的內(nèi)容設(shè)置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;

}

等分布局

公式轉(zhuǎn)化:

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進行布局的輔助。

用法:先將父框設(shè)置為margin-left: -*px,再設(shè)置子框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區(qū)域 w+g

}

(3)優(yōu)缺點

優(yōu)點:兼容性較好

缺點:ie6 ie7百分比兼容存在一定問題

2)使用table

(1)原理、用法

原理:通過增加一個父框的修正框,增大其寬度,并將父框轉(zhuǎn)換為table,將子框轉(zhuǎn)換為tabel-cell進行布局。

用法:先將父框的修正框設(shè)置為margin-left: -*px,再設(shè)置父框display: table、width:100%、table-layout: fixed,設(shè)置子框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)優(yōu)缺點

優(yōu)點:結(jié)構(gòu)和塊數(shù)無關(guān)聯(lián)

缺點:增加了一層

3)使用flex

(1)原理、用法

原理:通過設(shè)置CSS3布局利器flex中的flex屬性以達(dá)到等分布局。

用法:將父框設(shè)置為display: flex,再設(shè)置子框flex: 1,最后設(shè)置子框與子框的間距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)優(yōu)缺點

優(yōu)點:代碼量少,與塊數(shù)無關(guān)

缺點:兼容性存在一定問題

定寬+自適應(yīng)+兩塊高度一樣高

1)使用float

(1)原理、用法

原理:通過過分加大左右子框的高度,輔助超出隱藏,以達(dá)到視覺上的等高。

用法:將父框設(shè)置overflow: hidden,再設(shè)置左右子框padding-bottom: 9999px、margin-bottom: -9999px,最后設(shè)置左框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)優(yōu)缺點

優(yōu)點:兼容性好

缺點:偽等高,不是真正意義上的等高

2)使用table

(1)原理、用法

原理:將父框轉(zhuǎn)化為tabel,將子框轉(zhuǎn)化為tabel-cell布局,以達(dá)到定寬+自適應(yīng)+兩塊高度一樣高。

用法:先將父框設(shè)置為display:table、width:100%、table-layout:fixed,再設(shè)置左右框為display:table-cell,最后設(shè)置左框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)原理、用法

原理:通過設(shè)置CSS3布局利器flex中的flex屬性以達(dá)到定寬+自適應(yīng)+兩塊高度一樣高。

用法:將父框設(shè)置為display: flex,再設(shè)置左框width、margin-right,最后設(shè)置右框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)優(yōu)缺點

優(yōu)點:代碼少,flex很強大

缺點:兼容性存在一定問題

4)使用display

(1)原理、用法

原理:通過設(shè)置display中的CSS3的-webkit-box屬性以達(dá)到定寬+自適應(yīng)+兩塊高度一樣高。

用法:將父框設(shè)置為display: -webkit-box、width: 100%,再設(shè)置左框width、margin-right,最后設(shè)置右框-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)優(yōu)缺點

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

全屏布局

全屏布局的特點

滾動條不是全局滾動條,而是出現(xiàn)在內(nèi)容區(qū)域里,往往是主內(nèi)容區(qū)域

瀏覽器變大時,撐滿窗口

全屏布局的方法

1)使用position

(1)原理、用法

原理:將上下部分固定,中間部分使用定寬+自適應(yīng)+兩塊高度一樣高。

用法:見實例。

(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)優(yōu)缺點

優(yōu)點:兼容性好,ie6下不支持

2)使用flex

(1)原理、用法

原理:通過靈活使用CSS3布局利器flex中的flex屬性和flex-direction屬性以達(dá)到全屏布局。

用法:見實例。

(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)優(yōu)缺點

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

1)使用flex

(1)原理、用法

原理:通過靈活使用CSS3布局利器flex中的flex屬性和flex-direction屬性以達(dá)到全屏布局。

用法:見實例。

(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;

}

全屏布局相關(guān)方案的兼容性、性能和自適應(yīng)一覽表

方案 兼容性 性能 是否自適應(yīng)

Position 好 好 部分自適應(yīng)

Flex 較差 差 可自適應(yīng)

Grid 差 較好 可自適應(yīng)

當(dāng)然,最最最最最后,如果您喜歡這片文章,可以瘋狂點贊和收

愛工作,愛學(xué)習(xí),愛生活,愛前端 追求卓越,成功也會再不經(jīng)意間追上你

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補...
    _Yfling閱讀 14,198評論 1 92
  • 前端布局非常重要的一環(huán)就是頁面框架的搭建,也是最基礎(chǔ)的一環(huán)。在頁面框架的搭建之中,又有居中布局、多列布局以及全局布...
    HelloJames閱讀 449評論 0 2
  • H5移動端知識點總結(jié) 閱讀目錄 移動開發(fā)基本知識點 calc基本用法 box-sizing的理解及使用 理解dis...
    Mx勇閱讀 4,834評論 0 26
  • 移動開發(fā)基本知識點 一.使用rem作為單位 html { font-size: 100px; } @media(m...
    橫沖直撞666閱讀 3,746評論 0 6
  • 思念像柳絮一樣飛 是誰……使它成了雪 又是誰把雪染成了血 我呆呆的望著,一字不差的看著柳絮 看著雪 ……看著血 告...
    m沐心x閱讀 348評論 0 4

友情鏈接更多精彩內(nèi)容