line-height
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since иÑÐ»Ñ 2015 г..
СвойÑÑво CSS line-height ÑÑÑÐ°Ð½Ð°Ð²Ð»Ð¸Ð²Ð°ÐµÑ Ð²ÐµÐ»Ð¸ÑÐ¸Ð½Ñ Ð¿ÑоÑÑÑанÑÑва Ð¼ÐµÐ¶Ð´Ñ ÑÑÑоками, напÑÐ¸Ð¼ÐµÑ Ð² ÑекÑÑе. РблоÑнÑÑ
ÑлеменÑаÑ
оно ÑказÑÐ²Ð°ÐµÑ Ð¼Ð¸Ð½Ð¸Ð¼Ð°Ð»ÑнÑÑ Ð²ÑÑоÑÑ Ð±Ð»Ð¾ÐºÐ¾Ð² ÑÑÑок внÑÑÑи ÑлеменÑа. РнезамеÑаемÑÑ
внÑÑÑиÑÑÑоÑнÑÑ
ÑлеменÑаÑ
âÑказÑÐ²Ð°ÐµÑ Ð²ÑÑоÑÑ, коÑоÑÐ°Ñ Ð¸ÑполÑзÑеÑÑÑ Ð´Ð»Ñ Ð²ÑÑиÑÐ»ÐµÐ½Ð¸Ñ Ð²ÑÑоÑÑ Ð±Ð»Ð¾ÐºÐ¾Ð² ÑÑÑок.
ÐнÑеÑакÑивнÑй пÑимеÑ
line-height: normal;
line-height: 2.5;
line-height: 3em;
line-height: 150%;
line-height: 32px;
<section id="default-example">
<div class="transition-all" id="example-element">
Far out in the uncharted backwaters of the unfashionable end of the western
spiral arm of the Galaxy lies a small unregarded yellow sun.
</div>
</section>
#example-element {
font-family: Georgia, sans-serif;
max-width: 200px;
}
СинÑакÑиÑ
/* 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 задаÑÑÑÑ Ñ Ð¿Ð¾Ð¼Ð¾ÑÑÑ:
<ÑиÑла><велиÑинÑ><пÑоÑенÑного ÑооÑноÑениÑ>- клÑÑевого Ñлова
normal.
ÐнаÑениÑ
normal-
ÐавиÑÐ¸Ñ Ð¾Ñ Ð¿Ð¾Ð»ÑзоваÑелÑÑкого бÑаÑзеÑа. ÐомпÑÑÑеÑнÑе бÑаÑзеÑÑ (вклÑÑÐ°Ñ Firefox) иÑполÑзÑÑÑ Ð·Ð½Ð°Ñение по ÑмолÑÐ°Ð½Ð¸Ñ Ð¿ÑиблизиÑелÑно
1.2, в завиÑимоÑÑи Ð¾Ñ ÑлеменÑовfont-family. <ÑиÑло> (без именованиÑ)-
ÐнаÑением
line-heightбÑÐ´ÐµÑ ÑезÑлÑÑÐ°Ñ ÑÐ¼Ð½Ð¾Ð¶ÐµÐ½Ð¸Ñ Ñказанного ÑиÑла (без именованиÑ) на ÑÐ°Ð·Ð¼ÐµÑ ÑÑиÑÑа ÑлеменÑов. Указанное ÑиÑло, по ÑÑÑи, множиÑелÑ. РболÑÑинÑÑве ÑлÑÑаев ÑÑо пÑедпоÑÑиÑелÑнÑй ÑпоÑоб ÑÐºÐ°Ð·Ð°Ð½Ð¸Ñ Ð·Ð½Ð°ÑениÑline-height, поÑÐ¾Ð¼Ñ ÑÑо позволÑÐµÑ Ð¸Ð·Ð±ÐµÐ¶Ð°ÑÑ Ð½ÐµÐ¿ÑедвиденнÑÑ ÑезÑлÑÑаÑов пÑи наÑледовании. <велиÑина>-
УказаннаÑ
<велиÑина>иÑполÑзÑеÑÑÑ Ð¿Ñи вÑÑиÑлении вÑÑоÑÑ Ð±Ð»Ð¾ÐºÐ° ÑÑÑоки. ÐнаÑение, заданное в единиÑÐ°Ñ em Ð¼Ð¾Ð¶ÐµÑ Ð¿ÑивеÑÑи к непÑедвидимÑм ÑезÑлÑÑаÑам (ÑмоÑÑи пÑÐ¸Ð¼ÐµÑ Ð½Ð¸Ð¶Ðµ). <пÑоÑенÑное ÑооÑноÑение>-
ÐÑноÑиÑелÑно ÑазмеÑа ÑÑиÑÑа Ñамого ÑлеменÑа.Relative to the font size of the element itself. The computed value is this
<percentage>multiplied by the element's computed font size. Percentage values may produce unexpected results (see the second example below).
ФоÑмалÑное опÑеделение
| ÐаÑалÑное знаÑение | normal |
|---|---|
| ÐÑименÑеÑÑÑ Ðº | вÑе ÑлеменÑÑ. ÐÑо Ñакже пÑименÑеÑÑÑ Ðº ::first-letter и ::first-line. |
| ÐаÑледÑеÑÑÑ | да |
| ÐÑоÑенÑÑ | оÑноÑÑÑÑÑ Ðº ÑазмеÑÑ ÑÑиÑÑа Ñамого ÑлеменÑа |
| ÐбÑабоÑка знаÑÐµÐ½Ð¸Ñ | Ð´Ð»Ñ Ð¿ÑоÑенÑов и знаÑений длин, абÑолÑÑной длинÑ, еÑли дÑÑгое не Ñказано |
| Animation type | ÑиÑло или длина |
ФоÑмалÑнÑй ÑинÑакÑиÑ
line-height =
normal |
<number [0,â]> |
<length-percentage [0,â]>
<length-percentage> =
<length> |
<percentage>
ÐÑимеÑÑ
>Basic example
/* All rules below have the same resultant line height */
div {
line-height: 1.2;
font-size: 10pt;
} /* number */
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.
Prefer unitless numbers for line-height values
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 ems.
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
<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 -->
Result
СпеÑиÑикаÑии
| Specification |
|---|
| CSS Inline Layout Module Level 3 > # line-height-property > |