CSS页面布局解决方案大全
前端布局非常重要的一環就是頁面框架的搭建,也是最基礎的一環。在頁面框架的搭建之中,又有居中布局、多列布局以及全局布局,今天我們就來總結總結前端干貨中的CSS布局。
居中布局
水平居中
1)使用inline-block+text-align
(1)原理、用法
- 原理:先將子框由塊級元素改變為行內塊元素,再通過設置行內塊元素居中以達到水平居中。
- 用法:對子框設置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)優缺點
- 優點:兼容性好,甚至可以兼容ie6、ie7
- 缺點:child里的文字也會水平居中,可以在.child添加text-align:left;還原
2)使用table+margin
(1)原理、用法
- 原理:先將子框設置為塊級表格來顯示(類似 <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以上都支持
- 缺點:不支持ie6、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)優缺點
- 優點:居中元素不會對其他的產生影響
- 缺點: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; } .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 | 差 | 較好 | 可自適應 |
當然,最最最最最后,如果您喜歡這片文章,可以瘋狂點贊!!
總結
以上是生活随笔為你收集整理的CSS页面布局解决方案大全的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MVVM原理还你
- 下一篇: AES加密算法的学习笔记