/* 使用 Flexbox 实现水平和垂直居中对齐 */
.container {
display: flex;
justify-content: center; /* 水平居中 */
align-items: center; /* 垂直居中 */
height: 100vh; /* 设置容器高度为视口高度 */
}
/* 使用绝对定位和 transform 实现水平和垂直居中对齐 */
.centered-element {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%); /* 向左和向上移动自身宽度和高度的一半 */
}
/* 使用文本居中对齐 */
.text-center {
text-align: center; /* 文本水平居中 */
}
display: flex
,结合 justify-content: center
和 align-items: center
,可以轻松实现子元素在父容器中的水平和垂直居中。position: absolute
和 transform: translate(-50%, -50%)
,可以将元素精确地居中对齐。text-align: center
,可以使文本内容在行内水平居中。上一篇:css文字垂直居中
下一篇:css压缩
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站