/* 使用 Flexbox 实现上下左右居中 */
.container {
display: flex;
justify-content: center; /* 水平居中 */
align-items: center; /* 垂直居中 */
height: 100vh; /* 设置容器高度为视口高度 */
}
.item {
width: 100px;
height: 100px;
background-color: lightblue;
}
.container
是父级容器,使用 display: flex
将其转换为弹性盒子布局。justify-content: center
使子元素在水平方向上居中对齐。align-items: center
使子元素在垂直方向上居中对齐。height: 100vh
设置容器的高度为视口的 100%,确保容器占据整个屏幕高度。通过这种方式,.item
元素将被完美地居中显示在页面的中心位置。
上一篇:css居右
下一篇:css左右居中
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站