WebVincent 2019-07-01
html结构:
<div class="parent">
<div class="item"></div>
</div>.item {
position: absolute;
top: 50%;
left: 50%;
margin-top: -75px; /* 设置margin-left / margin-top 为自身高度的一半 */
margin-left: -75px;
}.item {
position: absolute;
margin:auto;
left:0;
top:0;
right:0;
bottom:0;
}
/* 这个方法可以实现自适应 */.item {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%); /* 使用css3的transform来实现 */
}.parent {
display: flex;
justify-content:center;
align-items: center;
background: #AAA;
height: 300px; /* 注意这里需要设置高度来查看垂直居中效果 */
}