/* 使用 flexbox 实现文本居中 */
.container {
display: flex;
justify-content: center; /* 水平居中 */
align-items: center; /* 垂直居中 */
height: 100vh; /* 设置容器高度为视口高度 */
}
.text {
text-align: center; /* 如果需要让文本本身也居中对齐 */
}
.container
:使用 flexbox
布局,通过 justify-content: center
和 align-items: center
实现水平和垂直居中。height: 100vh
:设置容器的高度为视口的 100%,确保内容可以在整个屏幕上居中。.text
:如果需要让文本本身也居中对齐,可以使用 text-align: center
。如果你只需要简单的单行文本居中,也可以使用以下方法:
/* 单行文本居中 */
.center-text {
width: 100%;
text-align: center; /* 水平居中 */
line-height: 100vh; /* 垂直居中(假设容器高度为 100vh) */
}
line-height: 100vh
:通过设置 line-height
等于容器高度,可以让单行文本垂直居中。上一篇:css最后一个元素
下一篇:css 文本两端对齐
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站