实现水平居中的方法

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;

发布了26 篇原创文章 · 获赞 1 · 访问量 2216

猜你喜欢

转载自blog.csdn.net/qq_38396070/article/details/104983273