/* 使用 Flexbox 实现水平垂直居中 */
.container {
display: flex;
justify-content: center; /* 水平居中 */
align-items: center; /* 垂直居中 */
height: 100vh; /* 设置容器高度为视口高度 */
}
/* 使用 Grid 实现水平垂直居中 */
.container {
display: grid;
place-items: center; /* 同时实现水平和垂直居中 */
height: 100vh; /* 设置容器高度为视口高度 */
}
/* 使用绝对定位和 transform 实现水平垂直居中 */
.item {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%); /* 调整元素自身位置 */
}
display: flex 将容器设置为弹性盒子布局,使用 justify-content: center 和 align-items: center 分别控制子元素在主轴(水平)和交叉轴(垂直)上的居中对齐。display: grid 和 place-items: center 可以更简洁地实现水平和垂直居中。position: absolute 将元素相对于最近的已定位祖先元素进行定位,再结合 transform: translate(-50%, -50%) 来调整元素自身的偏移量,从而实现居中效果。上一篇:css的三种引入方式
下一篇:css文字颜色渐变
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站