CSS字体对齐通过text-align控制水平对齐,适用于块级元素内行内内容;垂直对齐需结合vertical-align(行内元素)、line-height(单行文本)、Flexbox或Grid布局实现,各方法适用不同场景。<p>
<p>CSS字体对齐方式的设置主要通过text-align
vertical-align
line-height
text-align
text-align: left;
text-align: right;
text-align: center;
text-align: justify;
/* 示例:水平居中 */
.container {
text-align: center; /* 容器内的文本和行内元素都会水平居中 */
}text-align
vertical-align
vertical-align: baseline;
vertical-align: top;
vertical-align: middle;
vertical-align: bottom;
5px
20%
/* 示例:图片与文字垂直居中对齐 */
.icon {
vertical-align: middle;
margin-right: 5px;
}
.text {
vertical-align: middle;
}line-height
height
line-height
/* 示例:按钮文本垂直居中 */
.button {
height: 40px;
line-height: 40px; /* 与height相等,单行文本垂直居中 */
text-align: center; /* 水平居中 */
}/* 示例:Flexbox实现内容垂直居中 */
.flex-container {
display: flex;
align-items: center; /* 垂直居中 */
justify-content: center; /* 水平居中 */
height: 200px; /* 需要有固定高度 */
border: 1px solid #ccc;
}
.flex-item {
/* 内部内容 */
}/* 示例:Grid实现内容垂直居中 */
.grid-container {
display: grid;
place-items: center; /* 同时实现水平和垂直居中 */
height: 200px;
border: 1px solid #ccc;
}
.grid-item {
/* 内部内容 */
}text-align
text-align
text-align
left
<p>
p {
text-align: left; /* 文本靠左 */
}right
.price-display {
text-align: right; /* 价格数字靠右 */
}center
h1 {
text-align: center; /* 标题居中 */
}
.button-text {
text-align: center; /* 按钮文字居中 */
}justify
text-align-last
.article-body {
text-align: justify; /* 文章正文两端对齐 */
}left
justify
center
@@##@@
text-align: center;
text-align
text-align: center;
div
text-align
margin: 0 auto;
line-height
height
line-height
.single-line-text {
height: 50px;
line-height: 50px; /* 关键:与height相等 */
background-color: #f0f0f0;
text-align: center; /* 如果还需要水平居中 */
}vertical-align
vertical-align
.icon {
display: inline-block; /* 让它成为行内块 */
vertical-align: middle; /* 垂直居中对齐 */
width: 24px;
height: 24px;
background-color: lightblue;
}
.text-beside-icon {
display: inline-block;
vertical-align: middle;
}.flex-parent {
display: flex; /* 开启Flex布局 */
align-items: center; /* 子元素在交叉轴(默认垂直)上居中 */
justify-content: center; /* 如果也需要水平居中 */
height: 300px; /* 父容器需要有高度 */
border: 1px solid #ccc;
}
.flex-child {
padding: 20px;
background-color: lightgreen;
}.grid-parent {
display: grid; /* 开启Grid布局 */
place-items: center; /* 这是一个简写属性,同时设置align-items和justify-items为center */
height: 300px; /* 父容器需要有高度 */
border: 1px solid #ccc;
}
.grid-child {
padding: 20px;
background-color: lightcoral;
}place-items
transform
transform: translate(-50%, -50%);
.parent-relative {
position: relative; /* 父元素需要有相对定位 */
height: 300px;
border: 1px solid #ccc;
}
.child-absolute {
position: absolute;
top: 50%; /* 距离顶部50% */
left: 50%; /* 距离左边50% */
transform: translate(-50%, -50%); /* 自身向左上移动50% */
padding: 20px;
background-color: lightgoldenrodyellow;
}position: relative;
line-height
transform
vertical-align
line-height
vertical-align
line-height
vertical-align
vertical-align
display: table-cell;
div
vertical-align
baseline
baseline
top
middle
bottom
text-top
text-bottom
vertical-align: middle;
<p>
@@##@@ 这是一段文字。
.icon {
vertical-align: middle; /* 让图标与文字垂直居中 */
}vertical-align
img
line-height
line-height
line-height
font-size
height
line-height
line-height
.button {
height: 40px;
line-height: 40px; /* 行高撑满元素高度 */
text-align: center;
background-color: #eee;
}line-height
vertical-align
line-height
line-height
vertical-align
以上就是CSS字体对齐方式怎么设置_CSS字体对齐方式设置指南的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号