垂直水平居中 的几种实现方式
1. 最简单
// margin:0 auto;
// line-height等于height
.outer {
width:200px;padding:200px;
background-color:pink;
}
.inner {
width:100px;height:100px;
margin:0 auto;line-height:100px;
text-align:center;
background-color: skyblue;
}
2. 绝对定位的几种
.outer { position:relative;}
.inner {
position:absolute;
left:50%;
top:50%;
margin-left:-50px;
margin-top:-50px;
}
.inner {
position:absolute;
left:50%;
top:50%;
transform:translate(-50%,-50%);
}
.inner {
position:absolute;
left: calc(50% - 50px);
top: calc(50% - 50px);
}
.inner {
position:absolute;
left:0;
right:0;
top:0;
bottom:0;
margin:auto;
}
3. flex 布局的几种
.outer {
display: -webkit-flex;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-box;
display: flex;
-webkit-box-align: center;
-moz-box-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
-webkit-box-pack: center;
-moz-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;
display:flex;
}
.outer { display: flex; }
.inner { margin: auto; }
4. grid 布局的几种
.outer { display:grid; }
.inner {
justify-self: center;
align-self: center;
}
.outer { display: grid; }
.inner { margin: auto; }