vertical-align 用于控制 行内元素(inline) 或 表格单元格(table-cell) 的 垂直对齐方式。
不适用于块级元素(除非通过 display 转换为行内块或表格元素)。
值 | 描述 |
---|---|
baseline | 默认值,元素基线与父元素基线对齐 |
top | 元素顶部与行框顶部对齐 |
middle | 元素中线与父元素基线加半 x-height 对齐(近似垂直居中) |
bottom | 元素底部与行框底部对齐 |
text-top | 元素顶部与父元素文本顶部对齐 |
text-bottom | 元素底部与父元素文本底部对齐 |
1 2 |
<span class="icon">▲</span> <span class="text">Top</span> |
1 2 3 |
.icon { vertical-align: middle; /* 图标与文本垂直居中 */ } |
1 |
<img src="logo.png" class="logo"> CSS Logo |
1 2 3 |
.logo { vertical-align: middle; /* 图片与文字垂直居中 */ } |
1 2 3 4 5 |
<table> <tr> <td class="cell">Content</td> </tr> </table> |
1 2 3 |
.cell { vertical-align: middle; /* 单元格内容垂直居中 */ } |
1 2 3 4 |
img { vertical-align: bottom; /* 或 middle/top */ /* 或 display: block; */ } |
1 2 3 4 |
.block { display: inline-block; /* 或 table-cell */ vertical-align: middle; } |
1 2 3 4 5 6 |
.container { line-height: 1.5; /* 确保行高一致 */ } .item { vertical-align: middle; } |
line-height 控制行高,影响 vertical-align 的基线位置。
示例:单行文本垂直居中:
1 2 3 4 |
.button { height: 40px; line-height: 40px; /* 行高 = 容器高度 */ } |
实现块级元素的垂直居中:
1 2 3 4 |
.parent { display: table-cell; vertical-align: middle; /* 垂直居中 */ } |
关键点 | 说明 |
---|---|
适用元素 | 行内元素、行内块元素、表格单元格 |
默认值 | baseline(基线对齐) |
常见用途 | 图片与文字对齐、表格内容垂直居中、行内元素对齐 |
典型问题 | 图片底部间隙、块级元素无效、多行对齐混乱 |
协作属性 | line-height、display |
line-height 用于控制 行框(line box)的高度,直接影响以下布局效果:
line-height 支持多种单位或数值形式:
类型 | 示例 | 计算方式 | 推荐场景 |
---|---|---|---|
无单位数值 | line-height: 1.5 | 基于当前元素的 font-size 计算(1.5 * font-size) | 通用(继承友好) |
百分比 | line-height: 150% | 基于当前元素的 font-size 计算(1.5 * font-size) | 特定比例需求 |
长度单位 | line-height: 24px | 直接指定固定值(24px) | 精确控制行高 |
全局关键字 | line-height: normal | 浏览器默认值(通常约 1.2,取决于字体和浏览器) | 重置自定义行高 |
单位对比示例
1 2 3 4 5 6 7 8 |
.parent { font-size: 16px; line-height: 1.5; /* 子元素继承 1.5,计算为 1.5 * 子元素自身的 font-size */ } .parent-percent { font-size: 16px; line-height: 150%; /* 子元素继承计算后的 24px(1.5 * 16px) */ } |
继承差异示例
1 2 3 |
<div class="parent"> <div class="child">Text</div> </div> |
1 2 3 4 5 6 7 |
.parent { font-size: 16px; line-height: 1.5; /* 继承的是 1.5 */ } .child { font-size: 24px; /* 实际行高:1.5 * 24px = 36px */ } |
1 2 3 4 5 6 7 |
.parent-percent { font-size: 16px; line-height: 150%; /* 计算为 24px */ } .child-percent { font-size: 24px; /* 实际行高:24px(继承父元素计算后的值) */ } |
什么情况下 line-height 能实现垂直居中?
line-height 仅在以下场景有效:
正确示例:单行文本居中
1 2 3 4 5 6 7 |
<div class="text">单行文本</div> <style> .text { height: 100px; line-height: 100px; /* 有效:文本垂直居中 */ } </style> |
1 2 3 4 5 6 |
.goods .bd .text { height: 100px; display: flex; flex-direction: column; justify-content: center; /* 垂直居中 */ } |
1 2 3 4 5 6 7 8 9 10 |
.goods .bd .text { height: 100px; line-height: 100px; /* 父容器行高 */ } .goods .bd .text h4, .goods .bd .text p { display: inline-block; /* 转为行内块 */ vertical-align: middle; /* 垂直对齐 */ line-height: normal; /* 重置行高 */ } |
1 2 3 4 5 6 7 |
.goods .bd .text { height: 100px; } .goods .bd .text h4, .goods .bd .text p { line-height: 50px; /* 总高度 100px / 2 行 = 50px */ } |
通过设置容器高度等于 line-height 实现居中:
1 2 3 4 |
.button { height: 40px; line-height: 40px; /* 文本垂直居中 */ } |
1 2 3 |
.article { line-height: 1.6; /* 增加行间距提升可读性 */ } |
调整行内元素(如图标、文字)的垂直对齐:
1 2 3 |
.icon { vertical-align: middle; /* 基于行高居中 */ } |
1 2 3 |
body { line-height: 1.5; /* 覆盖浏览器默认值(通常 1.2) */ } |