时间:2023-09-19 08:00:01 | 来源:网站运营
时间:2023-09-19 08:00:01 来源:网站运营
CSS实现常见布局:从我看到的,知道的,全面的总结一下。CSS常见布局。margin: auto
进行水平居中<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;/*触发BFC,清除浮动*/ } .left { width: 1000px;/*也可以使用百分比*/ height: 1500px; float: left; background-color: aqua; } .right { width: 1000px;/*也可以使用百分比*/ height: 1500px; float: right; background-color: aqua; }
div
之间相距20px,左侧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;/*上端对齐*/ box-sizing: border-box; } .right { display: inline-block; width: calc(100% - 140px);/*注意:当父容器存在padding时100% 不算padding*/ height: 1000px; font-size: 14px; border: 1px solid #ddd; vertical-align: top;/*上端对齐*/ box-sizing: border-box; }
border-box
模型font-size : 0
float
.main { overflow: hidden;/*清除浮动带来的影响*/ 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;/*清除浮动带来的影响*/ 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);/*不设置宽度也可以,由里面自由撑开*/ height: 1000px; box-sizing: border-box; border: 1px solid #ddd; margin-left: 140px; }
注意:没有清除绝对定位的方法,若左侧盒子高于右侧盒子,就会超出父容器的高度。因此只能通过设置父容器的min-height
来防止这种情况。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
容器的一个默认属性值:align-items: stretch;
。这个属性导致了列等高的效果。 为了让两个盒子高度自动,需要设置: align-items: flex-start;
grid
方案margin
调整间距,也可以全部左浮动。<div class="main"> <div class="left"></div> <div class="middle"></div> <div class="right"></div> </div> .main { width: 90%; margin: 0 auto; overflow: hidden;/*触发BFC,清除浮动*/ } .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+负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样式如下(为了更好的显示,所以设置了边框,将box-sizing
设置为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; }
将左侧弄到上面去#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>
和圣杯布局一样,只不过需要将,padding 用 margin来进行实现。 #header { width: 100%; height: 50px; background-color: aliceblue; } #container { overflow: hidden;/*触发BFC,清除浮动影响*/ 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; }
关键词:布局,实现