使用Flexbox可通过justify-content和align-items实现多列文本居中:1. justify-content: center使列在主轴水平居中;2. align-items: center在交叉轴垂直居中;3. 配合text-align: center使文本自身居中,需设置容器高度与gap间距,适用于多列布局且整体对齐效果直观灵活。
要实现CSS多列文本的居中显示,使用Flexbox是最简单且高效的方式。通过 justify-content 和 align-items 两个属性,可以轻松控制主轴和交叉轴上的对齐方式。
display: flex
justify-content: center 实现水平居中display: flex
align-items: center 实现垂直居中
.container {
display: flex;
justify-content: center; /* 水平居中 */
align-items: center; /* 垂直居中 */
height: 300px; /* 设定高度以便看到垂直居中效果 */
gap: 20px; /* 列间距 */
}
.column {
text-align: center; / 文本自身居中 /
width: 100px;
}
HTML结构:第一列
第二列
第三列
tent 控制整体列的位置,align-items 控制列内内容的垂直对齐,但还需注意:align-items: flex-start 配合其他方式调整text-align: center
基本上就这些,Flexbox让多列居中变得直观又灵活。