/* 使用 Flexbox 实现上下左右居中 */
.container {
display: flex;
justify-content: center; /* 水平居中 */
align-items: center; /* 垂直居中 */
height: 100vh; /* 设置容器高度为视口高度 */
}
.item {
width: 100px;
height: 100px;
background-color: lightblue;
}
<div class="container">
<div class="item"></div>
</div>
.container 是父级容器,使用 display: flex 将其设置为弹性盒子布局。justify-content: center 使子元素在水平方向上居中对齐。align-items: center 使子元素在垂直方向上居中对齐。height: 100vh 设置容器的高度为视口的 100%,确保整个页面的高度都被利用。.item 是需要居中的子元素,设置了宽度和高度,并添加了背景颜色以便观察效果。上一篇:css @property
下一篇:css文字超出换行
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站