圣杯布局
文章目录
1. float
浮动会脱离文档流,处理不好会出现布局问题,但兼容性好
div {
height: 300px;
}
.left {
background: red;
width: 300px;
float: left;
}
.right {
background: green;
width: 300px;
float: right;
}
.center {
background: yellow;
}
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
2. flex
相比较浮动与定位比较完美
div {
height: 300px;
}
.left {
background: red;
width: 300px;
}
.right {
background: green;
width: 300px;
}
.center {
background: yellow;
flex: 1;
}
.out {
display: flex;
}
<div class="out">
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
</div>
3. position
快捷方便,但定位脱离文档流之后,会影响以后的布局,与浮动的问题类似
div {
height: 300px;
}
.left {
background: red;
width: 300px;
position: absolute;
left: 0;
}
.right {
background: green;
width: 300px;
position: absolute;
right: 0;
}
.center {
background: yellow;
position: absolute;
left: 300px;
right: 300px;
}
.out {
position: relative;
}
<div class="out">
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
</div>
4. table
兼容性好
.left {
background: red;
width: 300px;
}
.right {
background: green;
width: 300px;
}
.center {
background: yellow;
}
.out {
width: 100%;
height: 300px;
display: table;
}
.out div {
display: table-cell;
}
<div class="out">
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
</div>
5. Grid
新特性,兼容性较差
.left {
background: red;
}
.right {
background: green;
}
.center {
background: yellow;
}
.out {
display: grid;
grid-template-rows: 300px;
grid-template-columns: 300px auto 300px;
width: 100%;
}
<div class="out">
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
</div>
6. inline-block + calc
注意inline-block产生的空隙
div {
height: 300px;
}
.left {
background: red;
width: 300px;
}
.right {
background: green;
width: 300px;
}
.center {
background: yellow;
width: calc(100% - 600px);
}
.out {
font-size: 0;
}
.out div {
display: inline-block;
}
<div class="out">
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
</div>
7. inline-block + margin + padding
div {
height: 300px;
}
.left {
background: red;
margin-left: -300px;
width: 300px;
}
.right {
background: green;
margin-right: -300px;
width: 300px;
}
.center {
background: yellow;
width: 100%;
}
.out {
padding: 0 300px;
font-size: 0;
}
.out div {
display: inline-block;
}
<div class="out">
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
</div>

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论