下面介绍几种常用的垂直居中方法
<div class="outer">
<div class="content"></div>
</div>
html,body{
width: 100%;
height: 100%;
}
第一种:absolute + margin 负值
.outer {
position: relative;
width: 100%;
height: 100%;
}
.content {
width: 200px;
height: 200px;
position: absolute;
left: 50%;
top: 50%;
margin-left: -100px;
margin-top: -100px;
background-color: #a00;
}
第二种:absolute + margin auto
.outer {
position: relative;
width: 100%;
height: 100%;
}
.content {
width: 200px;
height: 200px;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
background-color: #a00;
}
第三种:absolute + translate
.outer {
position: relative;
width: 100%;
height: 100%;
}
.content {
width: 200px;
height: 200px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background-color: #a00;
}
第四种:flex布局
.outer {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.content {
width: 200px;
height: 200px;
background-color: #a00;
}