如何垂直居中元素
-
如何垂直和水平居中
实例
.container {
height: 200px;
position: relative;
border: 3px solid green;
}
.center {
margin: 0;
position: absolute;
top: 50%;
left: 50%;
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
I am vertically and horizontally centered.
亲自试一试
您还可以使用 Flexbox 将元素居中:
实例
.center {
display: flex;
justify-content: center;
align-items: center;
height: 200px;
border: 3px solid green;
}
亲自试一试