line-height
CSS
property sets the height of a line box. It's commonly used to set the distance between lines of text.
On block-level elements, it specifies the minimum height of line boxes within the element. On non-
replaced
inline elements, it specifies the height that is used to calculate line box height.
/* Keyword value */ line-height: normal; /* Unitless values: use this number multiplied by the element's font size */ line-height: 3.5; /* <length> values */ line-height: 3em; /* <percentage> values */ line-height: 34%; /* Global values */ line-height: inherit; line-height: initial; line-height: unset;
line-height
property is specified as any one of the following:
<number>
<length>
<percentage>
normal
.
normal
1.2
, depending on the element's
font-family
.
<number>
(unitless)
<number>
multiplied by the element's own font size. The computed value is the same as the specified
<number>
. In most cases,
this is the preferred way
to set
line-height
and avoid unexpected results due to inheritance.
<length>
<length>
is used in the calculation of the line box height. Values given in
em
units may produce unexpected results (see example below).
<percentage>
<percentage>
multiplied by the element's computed font size.
Percentage
values may produce unexpected results (see the second example below).
-moz-block-height
Sets the line height to the content height of the current block.
Use a minimum value of
1.5
for
line-height
for main paragraph content. This will help people experiencing low vision conditions, as well as people with cognitive concerns such as Dyslexia. If the page is zoomed to increase the text size, using a unitless value ensures that the line height will scale proportionately.
| 初始值 |
normal
|
|---|---|
| 适用于 |
所有元素。它还适用于
::first-letter
and
::first-line
.
|
| 继承 | yes |
| 百分比 | refer to the font size of the element itself |
| 计算值 | for percentage and length values, the absolute length, otherwise as specified |
| 动画类型 | either number or length |
normal | <number> | <length> | <percentage>
/* All rules below have the same resultant line height */
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 shorthand */
It is often more convenient to set
line-height
by using the
font
shorthand as shown above, but this requires the
font-family
property to be specified as well.
This example shows why it is better to use
<number>
values instead of
<length>
values. We will use two
<div>
elements. The first, with the green border, uses a unitless
line-height
value. The second, with the red border, uses a
line-height
value defined in
em
s.
<div class="box green"> <h1>Avoid unexpected results by using unitless line-height.</h1> length and percentage line-heights have poor inheritance behavior ... </div> <div class="box red"> <h1>Avoid unexpected results by using unitless line-height.</h1> length and percentage line-heights have poor inheritance behavior ... </div> <!-- The first <h1> line-height is calculated from its own font-size (30px × 1.1) = 33px --> <!-- The second <h1> line-height results from the red div's font-size (15px × 1.1) = 16.5px, probably not what you want -->
.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;
}
| 规范 | 状态 | 注释 |
|---|---|---|
|
CSS Level 2 (Revision 1)
The definition of 'line-height' in that specification. |
推荐 | 无变化。 |
|
CSS Level 1
The definition of 'line-height' in that specification. |
推荐 | 初始定义。 |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
line-height
|
Chrome 完整支持 1 | Edge 完整支持 12 | Firefox 完整支持 1 | IE 完整支持 4 | Opera 完整支持 7 | Safari 完整支持 1 | WebView Android 完整支持 1 | Chrome Android 完整支持 18 | Firefox Android 完整支持 4 | Opera Android 完整支持 10.1 | Safari iOS 完整支持 1 | Samsung Internet Android 完整支持 1.0 |
-moz-block-height
非标
|
Chrome 不支持 No | Edge 不支持 No | Firefox 完整支持 3.6 | IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No | Firefox Android 完整支持 4 | Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
完整支持
不支持
非标。预期跨浏览器支持较差。
font
font-family
font-feature-settings
font-kerning
font-language-override
font-optical-sizing
font-size
font-size-adjust
font-smooth
font-stretch
font-style
font-synthesis
font-variant
font-variant-alternates
font-variant-caps
font-variant-east-asian
font-variant-ligatures
font-variant-numeric
font-variant-position
font-variation-settings
font-weight
line-height
line-height-step