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å¹´7æâ©.
line-height CSS 屿§ç¨äºè®¾ç½®å¤è¡å
ç´ ç空é´éï¼å¦å¤è¡ææ¬çé´è·ã对äºå级å
ç´ ï¼å®æå®å
ç´ è¡çï¼line boxesï¼çæå°é«åº¦ã对äºé坿¿æ¢ç inline å
ç´ ï¼å®ç¨äºè®¡ç®è¡çï¼line boxï¼çé«åº¦ã
å°è¯ä¸ä¸
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 屿§è¢«æå®ä¸ºä»¥ä¸ä»»ä½ä¸ä¸ªï¼
- ä¸ä¸ª
<number>å¼ - ä¸ä¸ª
<length>å¼ - ä¸ä¸ª
<percentage>å¼ - å
³é®å
normalã
åå¼
normal-
åå³äºç¨æ·ä»£çãæ¡é¢æµè§å¨ï¼å æ¬ Firefoxï¼ä½¿ç¨é»è®¤å¼ï¼çº¦ä¸º
1.2ï¼è¿åå³äºå ç´ çfont-familyã <number>ï¼æ åä½ï¼-
è¯¥å±æ§çåºç¨å¼æ¯è¿ä¸ªæ åä½
<æ°å>ä¹ä»¥è¯¥å ç´ çåä½å¤§å°ã计ç®å¼ä¸æå®ç<number>å¼ç¸åã大夿°æ åµä¸ï¼è¿æ¯è®¾ç½®line-heightçæ¨èæ¹æ³ï¼ä¸ä¼å¨ç»§æ¿æ¶äº§çä¸ç¡®å®çç»æã <length>-
æå®ç¨äºè®¡ç®è¡åçé«åº¦ç
<é¿åº¦>å¼ã以 em 为åä½çå¼å¯è½ä¼äº§çä¸ç¡®å®çç»æï¼è§ä¸é¢ç示ä¾ï¼ã <percentage>-
ä¸å ç´ èªèº«çåä½å¤§å°æå ³ã计ç®å¼æ¯ç»å®çç¾åæ¯å¼ä¹ä»¥å ç´ è®¡ç®åºçåä½å¤§å°ã
<percentage>å¼å¯è½ä¼å¸¦æ¥ä¸ç¡®å®çç»æï¼è§ä¸é¢ç¬¬äºä¸ªç¤ºä¾ï¼ã
å½¢å¼åè¯æ³
line-height =
normal |
<number [0,â]> |
<length-percentage [0,â]>
<length-percentage> =
<length> |
<percentage>
示ä¾
>åºæ¬ç¤ºä¾
/* ç论ä¸ï¼ä»¥ä¸ææè§åæ¥æç¸åçè¡é« */
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 èµå¼æ¶ä½¿ç¨ <æ°å> 弿¯ä½¿ç¨ <é¿åº¦> æ´å¥½ãæä»¬ä¼å°ç¨ä¸¤ä¸ª <div> å
ç´ ã第ä¸ä¸ª div 为绿è²è¾¹æ¡ï¼ä½¿ç¨æ åä½ç line-heightå¼ã第äºä¸ª div 带红è²è¾¹æ¡ï¼ä½¿ç¨ em å®ä¹ line-height çå¼ã
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 -->
ç»æ
æ éç¢æ¹é¢
主段è½å
容ç line-height è³å°åºä¸º 1.5ãè¿å°æå©äºæ¹åä½å¯è§æ¡ä»¶ä¸çä½éªï¼ä¹å¯¹è®¤ç¥é»ç¢è
ï¼å¦é
读å°é¾è
ï¼æå¸®å©ã妿æåç大å°è¦é页é¢ç缩æ¾èååï¼è¯·ä½¿ç¨æ åä½çå¼ï¼ä»¥ç¡®ä¿è¡é«ä¹ä¼çæ¯ä¾ç¼©æ¾ã
è§è
| Specification |
|---|
| CSS Inline Layout Module Level 3 > # line-height-property > |
| åå§å¼ | normal |
|---|---|
| éç¨å ç´ | ææå
ç´ . It also applies to ::first-letter and ::first-line. |
| æ¯å¦æ¯ç»§æ¿å±æ§ | æ¯ |
| Percentages | refer to the font size of the element itself |
| 计ç®å¼ | 对äºç¾åæ¯åé¿åº¦å¼ï¼å ¶ä¸ºç»å¯¹é¿åº¦ï¼å¦å为æå®å¼ |
| å¨ç»ç±»å | æ°åæé¿åº¦åå¯ |