/* 使用 Flexbox 实现水平和垂直居中 */
.center {
display: flex;
justify-content: center; /* 水平居中 */
align-items: center; /* 垂直居中 */
height: 100vh; /* 设置高度为视口高度的100% */
}
/* 使用绝对定位和 transform 实现居中 */
.center-alt {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%); /* 向左和向上移动自身宽度和高度的一半 */
}
Flexbox 方法:
display: flex;:将容器设置为弹性盒子布局。justify-content: center;:使子元素在主轴(通常是水平方向)上居中对齐。align-items: center;:使子元素在交叉轴(通常是垂直方向)上居中对齐。height: 100vh;:确保容器的高度为视口高度的100%,以便垂直居中有效。绝对定位 + transform 方法:
position: absolute;:将元素相对于最近的已定位祖先元素进行定位。top: 50%; left: 50%;:将元素的左上角移动到父容器的中心点。transform: translate(-50%, -50%);:通过负值平移,使元素自身居中。上一篇:css上下左右居中
下一篇:style.css
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站