/* 使用 Flexbox 实现垂直居中 */
.container {
display: flex;
justify-content: center; /* 水平居中 */
align-items: center; /* 垂直居中 */
height: 100vh; /* 设置容器高度为视口高度 */
}
/* 使用 Grid 实现垂直居中 */
.container {
display: grid;
place-items: center; /* 同时实现水平和垂直居中 */
height: 100vh; /* 设置容器高度为视口高度 */
}
/* 使用绝对定位和 transform 实现垂直居中 */
.container {
position: relative;
height: 100vh; /* 设置容器高度为视口高度 */
}
.item {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%); /* 通过 transform 实现精确居中 */
}
display: flex 和 align-items: center 可以轻松实现子元素的垂直居中,同时使用 justify-content: center 实现水平居中。display: grid 和 place-items: center 是一种简洁的方式,可以同时实现水平和垂直居中。top: 50% 和 left: 50%,再使用 transform: translate(-50%, -50%) 来调整位置,从而实现精确的居中效果。上一篇:css隐藏滚动条
下一篇:css border
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站