尝试一下
line-height: normal;
line-height: 2.5;
line-height: 3em;
line-height: 150%;
line-height: 32px;
Far out in the uncharted backwaters of the unfashionable end of the western
spiral arm of the Galaxy lies a small unregarded yellow sun.
#example-element {
font-family: Georgia, sans-serif;
max-width: 200px;
}
语法
css/* Keyword value */
line-height: normal;
/* Unitless values: use this number multiplied
by the element's font size */
line-height: 3.5;
/*
line-height: 3em;
/*
line-height: 34%;
/* Global values */
line-height: inherit;
line-height: initial;
line-height: unset;
line-height 属性被指定为以下任何一个:
一个
一个
一个
关键字 normal。
取值
normal
取决于用户代理。桌面浏览器(包括 Firefox)使用默认值,约为 1.2,这取决于元素的 font-family。
该属性的应用值是这个无单位<数字>乘以该元素的字体大小。计算值与指定的
指定用于计算行向盒高度的<长度>值。以 em 为单位的值可能会产生不确定的结果(见下面的示例)。
与元素自身的字体大小有关。计算值是给定的百分比值乘以元素计算出的字体大小。
形式化语法
line-height = normal |
此语法反映了基于 CSS Inline Layout Module Level 3, CSS Values and Units Module Level 4 的最新标准。并非所有浏览器都已实现全部内容。有关支持信息,请参阅浏览器兼容性。
示例
基本示例
css/* 理论上,以下所有规则拥有相同的行高 */
div {
line-height: 1.2;
font-size: 10pt;
} /* 无单位数值 number/unitless */
div {
line-height: 1.2em;
font-size: 10pt;
} /* 长度 length */
div {
line-height: 120%;
font-size: 10pt;
} /* 百分比 percentage */
div {
font:
10pt/1.2 Georgia,
"Bitstream Charter",
serif;
} /* font 简写属性 font shorthand */
为了简便,可以通过 font 简写来设置 line-height,但这要求在使用该简写属性时同时设置 font-family 属性。
推荐在设置 line-height 时使用无单位数值
这个示例说明了为什么给 line-height 赋值时使用 <数字> 值比使用 <长度> 更好。我们会到用两个
CSS
css.green {
line-height: 1.1;
border: solid limegreen;
}
.red {
line-height: 1.1em;
border: solid red;
}
h1 {
font-size: 30px;
}
.box {
width: 18em;
display: inline-block;
vertical-align: top;
font-size: 15px;
}
HTML
html
Avoid unexpected results by using unitless line-height.
length and percentage line-heights have poor inheritance behavior ...
Avoid unexpected results by using unitless line-height.
length and percentage line-heights have poor inheritance behavior ...
结果
无障碍方面
主段落内容的 line-height 至少应为 1.5。这将有助于改善低可视条件下的体验,也对认知阻碍者,如阅读困难者,有帮助。如果文字的大小要随页面的缩放而变化,请使用无单位的值,以确保行高也会等比例缩放。
W3C Understanding WCAG 2.1
规范
规范
CSS Inline Layout Module Level 3# line-height-property
初始值normal适用元素所有元素. It also applies to ::first-letter and ::first-line.是否是继承属性是Percentagesrefer to the font size of the element itself计算值对于百分比和长度值,其为绝对长度,否则为指定值动画类型数字或长度均可
浏览器兼容性
参见
font
font-size
帮助改进 MDN
此页面对您有帮助吗?
是
否
了解如何参与贡献
此页面最后更新于 2025年11月10日,由 MDN 贡献者更新。
在 GitHub 上查看此页面 • 报告此内容的问题