display: flex;
justify-content: center;//控制水平方向的居中(可省略,默认主轴是水平)
align-items: center;//控制垂直方向的居中
position: absolute;
top: 50%;
left: 50%;
margin-top: -height的一半
margin-left: -width的一半
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);//浏览器厂商前缀必不可少
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
父元素:
position: relative;
子元素:
position: absolute;
margin: auto;
top: 0;
left: 0;
right: 0;
bottom: 0;