時(shí)間:2023-09-19 08:00:01 | 來(lái)源:網(wǎng)站運(yùn)營(yíng)
時(shí)間:2023-09-19 08:00:01 來(lái)源:網(wǎng)站運(yùn)營(yíng)
CSS實(shí)現(xiàn)常見布局:從我看到的,知道的,全面的總結(jié)一下。CSS常見布局。<div class="main"></div>? .main { width: 90%; height: 2000px; background-color: aqua; margin: 0 auto; }
border-box
<div class="main"> <div class="left"></div> <div class="right"></div> </div>??? .main { width: 90%; margin: 0 auto; overflow: hidden;/*觸發(fā)BFC,清除浮動(dòng)*/ } .left { width: 1000px;/*也可以使用百分比*/ height: 1500px; float: left; background-color: aqua; }? .right { width: 1000px;/*也可以使用百分比*/ height: 1500px; float: right; background-color: aqua; }
div
之間相距20px,左側(cè)div
寬120px<div class="main"> <div class="left"></div> <div class="right"></div> </div>?
雙inline-block
.main { padding: 15px 20px; border: 1px dashed #ff6c60; font-size: 0; } .left { margin-right: 20px; display: inline-block; width: 120px; height: 500px; font-size: 14px; border: 1px solid #ddd; vertical-align: top;/*上端對(duì)齊*/ box-sizing: border-box; } .right { display: inline-block; width: calc(100% - 140px);/*注意:當(dāng)父容器存在padding時(shí)100% 不算padding*/ height: 1000px; font-size: 14px; border: 1px solid #ddd; vertical-align: top;/*上端對(duì)齊*/ box-sizing: border-box; }
border-box
模型font-size : 0
float
.main { overflow: hidden;/*清除浮動(dòng)帶來(lái)的影響*/ padding: 15px 20px; border: 1px dashed #ff6c60; } .left { width: 120px; height: 500px; box-sizing: border-box; border: 1px solid #ddd; float: left; margin-right: 20px; } .right { width: calc(100% - 140px); height: 1000px; box-sizing: border-box; border: 1px solid #ddd; float: left; }
float+ margin-left
margin-left
.main { overflow: hidden;/*清除浮動(dòng)帶來(lái)的影響*/ padding: 15px 20px; border: 1px dashed #ff6c60; } .left { width: 120px; height: 500px; box-sizing: border-box; border: 1px solid #ddd; float: left; } .right { width: calc(100% - 140px); height: 1000px; box-sizing: border-box; border: 1px solid #ddd; margin-left: 140px; }
absolute+margin-left
.main { padding: 15px 20px; border: 1px dashed #ff6c60; position: relative; } .left { width: 120px; height: 500px; box-sizing: border-box; border: 1px solid #ddd; position: absolute; } .right { width: calc(100% - 140px);/*不設(shè)置寬度也可以,由里面自由撐開*/ height: 1000px; box-sizing: border-box; border: 1px solid #ddd; margin-left: 140px; }
注意:沒(méi)有清除絕對(duì)定位的方法,若左側(cè)盒子高于右側(cè)盒子,就會(huì)超出父容器的高度。因此只能通過(guò)設(shè)置父容器的min-height
來(lái)防止這種情況。float+BFC
BFC
。flex
方案 .main { padding: 15px 20px; border: 1px dashed #ff6c60; display: flex; align-items: flex-start; } .left { width: 120px; height: 500px; border: 1px solid #ddd; flex: 0 0 auto; margin-right: 20px; } .right { height: 1000px; border: 1px solid #ddd; flex: 1 1 auto; }
flex
容器的一個(gè)默認(rèn)屬性值:align-items: stretch;
。這個(gè)屬性導(dǎo)致了列等高的效果。 為了讓兩個(gè)盒子高度自動(dòng),需要設(shè)置: align-items: flex-start;
grid
方案margin
調(diào)整間距,也可以全部左浮動(dòng)。<div class="main"> <div class="left"></div> <div class="middle"></div> <div class="right"></div> </div> .main { width: 90%; margin: 0 auto; overflow: hidden;/*觸發(fā)BFC,清除浮動(dòng)*/ } .left { width: 200px;/*也可以使用百分比*/ height: 1500px; float: left; background-color: aqua; margin: 0 10px; }? .right { width: 200px;/*也可以使用百分比*/ height: 1500px; float: right; background-color: aqua; margin: 0 10px; } .middle { width: calc(100% - 460px); height: 1500px; float: left; background-color: aqua; margin: 0 10px; }
<div class="main"> <div class="left"></div> <div class="middle"></div> <div class="right"></div> </div> .main { width: 90%; margin: 0 auto; position: relative; } .left { width: 200px;/*也可以使用百分比*/ height: 1500px; position: absolute; left: 10px; background-color: aqua; }? .right { width: 200px;/*也可以使用百分比*/ height: 1500px; position: absolute; right: 10px; background-color: aqua; } .middle { width: calc(100% - 460px); height: 1500px; position: absolute; left: 230px; background-color: aqua; }?
padding+負(fù)margin
(圣杯布局)overflow: hidden;
<div id = "container"> <div id="center" class="column"></div> <div id="left" class="column"></div> <div id="right" class="column"></div> </div>
基本的css樣式如下(為了更好的顯示,所以設(shè)置了邊框,將box-sizing
設(shè)置為border-box
模型): #container { padding-left: 200px; padding-right: 150px; overflow: hidden; border: 1px dashed #ff6c60; } #container .column { border: 1px solid #ff6c60; box-sizing: border-box; } #center { width: 100%; height: 1000px; }? #left { width: 200px; height: 100px;? }? #right { width: 150px; height: 100px; }
將左側(cè)弄到上面去#left { position: relative; margin-left: -100%; right: 200px; }#right { margin-right: -150px;}?
就OK了!<div id="header"></div> <div id = "container"> <div id="center" class="column"> <div id="inner-center"></div> </div> <div id="left" class="column"></div> <div id="right" class="column"></div> </div><div id="footer"></div>
和圣杯布局一樣,只不過(guò)需要將,padding 用 margin來(lái)進(jìn)行實(shí)現(xiàn)。 #header { width: 100%; height: 50px; background-color: aliceblue; }? #container { overflow: hidden;/*觸發(fā)BFC,清除浮動(dòng)影響*/ border: 1px dashed #ff6c60; } #footer { width: 100%; height: 50px; background-color: aliceblue; }? .column { float: left; } #center { width: 100%; } #inner-center { height: 1000px; margin-left: 200px; margin-right: 150px; background-color: aqua; }? #left { width: 200px; height: 100px; margin-left: -100%; background-color: aqua; }? #right { width: 150px; height: 100px; margin-left: -150px; background-color: aqua; }?
<div class="father"> <div class="son"></div> <div class="son"></div> <div class="son"></div> <div class="son"></div></div>
margin-right: 10px
,寬度則為calc(25% - 10px)
margin-left
清除。 .father { overflow: hidden; margin-right: -10px; } .son { width: calc(25% - 10px); height: 500px; float: left; margin-right: 10px; background-color: aqua; }
.father { display: flex; margin-right: -10px; } .son { flex: 1 1 auto; align-items: flex-start; height: 500px; border: 1px solid #ff6c60; margin-right: 10px; }
關(guān)鍵詞:布局,實(shí)現(xiàn)
客戶&案例
營(yíng)銷資訊
關(guān)于我們
客戶&案例
營(yíng)銷資訊
關(guān)于我們
微信公眾號(hào)
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。