/* 使用 Flexbox 实现水平和垂直居中 */
.container {
display: flex;
justify-content: center; /* 水平居中 */
align-items: center; /* 垂直居中 */
height: 100vh; /* 设置容器高度为视口高度 */
}
/* 使用 Grid 实现水平和垂直居中 */
.container {
display: grid;
place-items: center; /* 同时实现水平和垂直居中 */
height: 100vh; /* 设置容器高度为视口高度 */
}
/* 使用绝对定位和 transform 实现水平和垂直居中 */
.centered {
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 中文站