一、三栏布局:
1. 高度已知,左右两栏固定,中间自适应
(1) float实现
<section class="float">
<article>
<div class="left"></div>
<div class="right"></div>
<div class="center"></div>
</article>
</section>
<style>
.float .left {
width: 300px;
float: left;
background: green;
}
.float .center {
margin-left: 300px;
margin-right: 300px;
background: yellow;
}
.float .right {
width: 300px;
float: right;
background: red;
}
</style>
(2)absolute实现
<section class="absolute">
<article>
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
</article>
</section>
<style>
.absolute article > div {
position: absolute;
}
.absolute .left {
width: 300px;
left: 0;
background: green;
}
.absolute .center {
left: 300px;
right: 300px;
background: yellow;
}
.absolute .right {
width: 300px;
right: 0;
background: red;
}
</style>
(3)flex实现
<section class="flex">
<arctile class="left-center-right">
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
</arctile>
</section>
<style>
.flex .left-center-right {
display: flex;
}
.flex .left {
width: 300px;
background: green;
}
.flex .center {
flex:1;
background: yellow;
}
.flex .right {
width: 300px;
background: red;
}
</style>
(4)table实现
<section class="table">
<arctile class="left-center-right">
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
</arctile>
</section>
<style>
.table .left-center-right {
width: 100%;
height: 100px;
display: table;
}
.table .left-center-right div {
display: table-cell;
}
.table .left {
width: 300px;
background: green;
}
.table .center {
background: yellow;
}
.table .right {
width: 300px;
background: red;
}
</style>
(5)gird实现
<section class="grid">
<arctile class="left-center-right">
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
</arctile>
</section>
<style>
.grid .left-center-right {
display: grid;
width: 100%;
grid-template-rows : 100px;
grid-template-columns : 300px auto 300px;
}
.grid .left {
background: green;
}
.grid .center {
background: yellow;
}
.grid .right {
background: red;
}
</style>
2. 高度未知,左右两栏固定,中间自适应
(1)flex:可用
(2)table:可用
3. 上下固定,中自适应
<body>
<article class="top"></article>
<article class="center"></article>
<article class="bottom"></article>
</body>
<style>
article {
width: 100%;
}
.top{
height: 200px;
background: red;
position: absolute;
top: 0;
left: 0;
}
.bottom {
height: 200px;
background: blue;
position: absolute;
bottom: 0;
left: 0;
}
.center {
background: yellow;
position: absolute;
height: auto;
top: 200px;
bottom: 200px;
}
</style>
二、垂直居中:
(1)弹性盒方法——flex布局
display: flex;
justify-content: center;
align-items: center;
(2)定位+transform
position: relative //父元素
position: absolute; //子元素
top: 50%;
left: 50%;
transform: translate(-50%,-50%)
(3)纯absolute
position: relative //父元素
position: absolute; //子元素
top: 50%;
left: 50%;
margin-left:-1/2子元素宽度;
margin-top: -1/2子元素高度;
(4)margin:auto+absolute
position: relative //父元素
position: absolute; //子元素
top: 0;
left: 0;
right:0;
bottom:0;
margin:auto
(5)table-cell
display table //父元素上一层
display: table-cell; //父元素
vertical-align: middle;
text-align: center;
display: inline-block; //子元素