摘要:今天總結(jié)一下布局,之前校招面試的時(shí)候貌似很喜歡考布局,例如兩欄自適應(yīng)布局三欄自適應(yīng)布局等等,實(shí)現(xiàn)的方法多種多樣,總結(jié)一下以后就不亂了。脫離文檔流觸發(fā)使用脫離文檔流之后,我們?cè)倮脜^(qū)域不會(huì)與浮動(dòng)元素重疊的特性來(lái)使剩下的元素自適應(yīng)。
今天總結(jié)一下布局,之前校招面試的時(shí)候貌似很喜歡考布局,例如兩欄自適應(yīng)布局、三欄自適應(yīng)布局等等,實(shí)現(xiàn)的方法多種多樣,總結(jié)一下以后就不亂了。
兩欄布局這里我們講的兩欄布局一般是左定寬右自適應(yīng)的
左float+右margin-left
html, body { height: 80%; } .wrapper { height: 100%; } .common { height: 100%; } .aside { width: 200px; background: green; float: left; } .main { background: red; margin-left: 200px; }側(cè)欄
主欄
雙float+右calc
html, body { height: 80%; } .wrapper { height: 100%; } .common { height: 100%; float: left; } .aside { width: 200px; background: green; } .main { background: red; width: calc(100% - 200px); }側(cè)欄
主欄
左absolute+右margin-left
html, body { height: 80%; } .wrapper { height: 100%; position: relative; } .common { height: 100%; } .aside { position: absolute; left: 0px; width: 200px; background: green; } .main { background: red; margin-left: 200px; }側(cè)欄
主欄
雙absolute
.wrapper { height: 100%; position: relative; } .common { height: 100%; } .aside { background: green; position: absolute; left: 0px; width: 200px; } .main { background: red; position: absolute; left: 200px; right: 0px; }側(cè)欄
主欄
flex
html, body { height: 80%; } .wrapper { height: 100%; display: flex; } .common { height: 100%; } .aside { flex: 0 1 200px; background: green; } .main { flex-grow: 1; background: red; }三欄布局側(cè)欄
主欄
這里說(shuō)的三欄布局是左右定寬,中間自適應(yīng)
float left + margin-left/margin-right + float right
html, body { height: 100%; } .wrapper { height: 100%; } .left { height: 100%; width: 200px; float: left; background: green; } .right { height: 100%; width: 200px; float: right; background: blue; } .main { height: 100%; margin: 0px 200px; background: red; }
BFC特性的三欄布局(后面總結(jié)BFC)
.left { float: left; height: 200px; width: 100px; background-color: red; } .right { width: 200px; height: 200px; float: right; background-color: blue; } .main { height: 200px; overflow: hidden; background-color: green; }Left
Right
Content
圣杯布局
html, body { height: 100%; } .wrapper { height: 80%; padding: 0px 200px; } .common { position: relative; float: left; height: 100%; color: white; } .content { background: red; width: 100%; } .left { background: blue; width: 200px; margin-left: -100%; left: -200px; } .right { background: green; width: 200px; margin-left: -200px; right: -200px; }Content
Left
Right
中間內(nèi)容區(qū)content在最前面,后面依次是left和right。
首先Content、Left、Right都設(shè)為float:left,然后Content寬度設(shè)為100%,此時(shí)Left和Right被擠到了下面一行;
將Left的margin-left設(shè)為-100%,Left被拉到了Content的最左邊,且遮擋了Content的左邊部分;將Right的負(fù)外左邊距設(shè)為它的寬度,Right被拉到了Content的最右邊,且遮住了Content的右邊部分
此時(shí)再設(shè)置wapper的左右padding分別為L(zhǎng)eft和Right的寬度,此時(shí)Content的寬度被壓縮到了合適的位置,但是Left和Right仍沒(méi)有到正確的位置
最后通過(guò)相對(duì)定位,設(shè)置Left和Right都為relative,且Left的left設(shè)為其負(fù)寬度,Right的right設(shè)為其負(fù)寬度
雙飛翼布局
html, body { height: 100%; } .common { height: 100%; float: left; color: #fff; } .content { background: red; width: 100%; } .content-in { margin: 0px 200px; } .left { background: blue; width: 200px; margin-left: -100%; } .right { background: green; width: 200px; margin-left: -200px; }Content
Left
Right
首先Content、Left、Right都設(shè)置float:left;
將Left的margin-left設(shè)為-100%,Left被拉到了Content的最左邊,且遮擋了Content的左邊部分;將Right的負(fù)外左邊距設(shè)為它的寬度,Right被拉到了Content的最右邊,且遮住了Content的右邊部分
Content-in設(shè)置左右margin分別為L(zhǎng)eft寬度和Right寬度即可
絕對(duì)定位
.wrapper { height: 80%; position: relative; } .common { height: 100%; color: white; } .left { position: absolute; top: 0px; left: 0px; width: 200px; background: green; } .right { position: absolute; top: 0px; right: 0px; width: 200px; background: blue; } .content { background: red; margin: 0 200px; }Content
Left
Right
flex
html, body { height: 100%; } .wrapper { height: 80%; display: flex; } .common { height: 100%; color: white; } .content { flex-grow: 1; background: red; } .left { flex: 0 1 200px; order: -1; background: blue; } .right { flex: 0 1 200px; background: green; }總結(jié)Content
Left
Right
總結(jié)發(fā)現(xiàn)實(shí)現(xiàn)兩欄或者三欄布局的方法大概有這么幾種
脫離文檔流+margin
在上面的實(shí)現(xiàn)方式中使用float和position:absolute來(lái)脫離文檔流,然后再讓剩下的元素調(diào)整外邊距margin來(lái)做成自適應(yīng)。
脫離文檔流+觸發(fā)BFC
使用float脫離文檔流之后,我們?cè)倮肂FC區(qū)域不會(huì)與浮動(dòng)元素重疊的特性來(lái)使剩下的元素自適應(yīng)。
純絕對(duì)定位
所有的欄都設(shè)置position:absolute,然后定寬元素設(shè)置寬度、left和Right位置,自適應(yīng)元素只設(shè)置left和right位置
flex
使用flex的flex-grow和flex-shrink可以來(lái)實(shí)現(xiàn)自適應(yīng)布局
其他
類似雙飛翼和圣杯布局其實(shí)也是部分利用了浮動(dòng)和定位的知識(shí),以及負(fù)margin相關(guān)的操作,總體的知識(shí)并不變化
參考資料
http://zh.learnlayout.com/dis...
http://www.cnblogs.com/imwtr/...
https://zhuanlan.zhihu.com/p/...
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/115883.html
摘要:如何使用首先梳理一下,將屬性分為兩類作用在容器元素上的有個(gè),其實(shí)容器元素上還有一個(gè)設(shè)置的屬性作用在子元素上的有個(gè)當(dāng)時(shí)設(shè)置布局之后,子元素的的屬性將會(huì)失效。 why——為什么使用flex 長(zhǎng)久以來(lái),網(wǎng)頁(yè)布局是一個(gè)比較難搞的地方,各種前端前輩嘔心瀝血總結(jié)了各種奇淫巧技,比如利用float和position來(lái)實(shí)現(xiàn)居中、兩欄、三欄等等布局。然而css并不存在一個(gè)官方的布局方案,終于一種新的布局...
摘要:特意對(duì)前端學(xué)習(xí)資源做一個(gè)匯總,方便自己學(xué)習(xí)查閱參考,和好友們共同進(jìn)步。 特意對(duì)前端學(xué)習(xí)資源做一個(gè)匯總,方便自己學(xué)習(xí)查閱參考,和好友們共同進(jìn)步。 本以為自己收藏的站點(diǎn)多,可以很快搞定,沒(méi)想到一入?yún)R總深似海。還有很多不足&遺漏的地方,歡迎補(bǔ)充。有錯(cuò)誤的地方,還請(qǐng)斧正... 托管: welcome to git,歡迎交流,感謝star 有好友反應(yīng)和斧正,會(huì)及時(shí)更新,平時(shí)業(yè)務(wù)工作時(shí)也會(huì)不定期更...
摘要:和都是相對(duì)于內(nèi)邊距邊界的。和即為向上滾動(dòng)時(shí),元素內(nèi)容區(qū)被遮住的那一部分。同理參考中的各種寬高屬性 引子 曾經(jīng)校招面試的時(shí)候,學(xué)習(xí)了三個(gè)月前端的我去某廠面試,面試官循循善誘考察了一個(gè)開(kāi)發(fā)中的實(shí)際場(chǎng)景,其中有需要用到某元素的高度,面試官問(wèn)我clientHeight和offsetHeight的區(qū)別是什么,我當(dāng)時(shí)一臉懵逼,這個(gè)問(wèn)題對(duì)于當(dāng)時(shí)的我來(lái)說(shuō)已經(jīng)完全超綱了...面試結(jié)果自然是面試官感謝我來(lái)...
摘要:和都是相對(duì)于內(nèi)邊距邊界的。和即為向上滾動(dòng)時(shí),元素內(nèi)容區(qū)被遮住的那一部分。同理參考中的各種寬高屬性 引子 曾經(jīng)校招面試的時(shí)候,學(xué)習(xí)了三個(gè)月前端的我去某廠面試,面試官循循善誘考察了一個(gè)開(kāi)發(fā)中的實(shí)際場(chǎng)景,其中有需要用到某元素的高度,面試官問(wèn)我clientHeight和offsetHeight的區(qū)別是什么,我當(dāng)時(shí)一臉懵逼,這個(gè)問(wèn)題對(duì)于當(dāng)時(shí)的我來(lái)說(shuō)已經(jīng)完全超綱了...面試結(jié)果自然是面試官感謝我來(lái)...
摘要:重繪元素做了一些不影響排版的改變,比如背景色下劃線等等,只需要重新繪制的過(guò)程,叫做重繪。顯然回流帶來(lái)的代價(jià)大于重繪,因?yàn)橹乩L僅僅是重新畫(huà)一遍元素而已,但是重繪是重新計(jì)算重新畫(huà)。不然這會(huì)導(dǎo)致大量地讀寫(xiě)這個(gè)結(jié)點(diǎn)的屬性。 瀏覽器的大概工作流程 以普通的HTML頁(yè)面為例: 解析HTML文檔,生成dom樹(shù) 解析css產(chǎn)生css規(guī)則樹(shù) 解析JavaScript,通過(guò)DOM-API來(lái)操作dom樹(shù)和...
閱讀 4076·2021-11-22 13:53
閱讀 1774·2021-09-23 11:52
閱讀 2534·2021-09-06 15:02
閱讀 1092·2019-08-30 15:54
閱讀 953·2019-08-30 14:15
閱讀 2438·2019-08-29 18:39
閱讀 760·2019-08-29 16:07
閱讀 553·2019-08-29 13:13