深入理解 line-height 和 vertical-align
幾個(gè)概念
- line box:包裹 inline box 的一個(gè)盒子,一個(gè)或多個(gè) line box 堆疊撐起一個(gè) HTML 元素。
- inline(-level) box:可以是一個(gè)由行內(nèi)元素包裹的盒子,也可以是一個(gè)純文字的匿名盒子。
- content area:對(duì)于非替換元素來(lái)說(shuō),content area 的范圍由 font-size 以及字體本身決定;對(duì)于替換元素來(lái)說(shuō),由元素自有寬高決定。
- baseline:一個(gè)元素基線的位置由該元素內(nèi)字母 x 底部所在的位置決定,當(dāng)然字體不同基線所在的位置也就不同。
通過(guò)一段代碼可以理解一下:
div {
background-color: #ccc;
font-size: 20px;
color: #fff;
}
span {
color: red;
}
<div>文字1<span>文字2</span>文字3</div>

白色的文字就是一個(gè)匿名 inline box,紅色的文字是一個(gè)由 span 包裹的 inline box。這三個(gè) inline box 組成一個(gè) line box,可以理解為灰色的區(qū)域,因?yàn)樵谶@個(gè)例子里就是由一個(gè) line box 撐開(kāi)了 div。如果有多行的文字,那就有多個(gè) line box。
關(guān)于 content area,W3C 有一段這樣的解釋:
CSS 2.1 does not define what the content area of an inline box is (see 10.6.1 above) and thus different UAs may draw the backgrounds and borders in different places.
這篇文章對(duì)非替換元素 content area 的定義就是自有寬高加上 margin,padding 以及 border。我認(rèn)為應(yīng)該將 content area 理解為 content box。
line box 高度
瀏覽器會(huì)計(jì)算 line box 中每一個(gè) inline box 的高度,對(duì)于不同的 inline box 計(jì)算方式有所不同:
如果是一個(gè)替換元素(比如 img,input),inline-* 元素或者是 flexbox 中的子元素,高度由其 margin box 決定;
inline-block 元素:
div {
background-color: #ccc;
color: #fff;
}
span {
display: inline-block;
height: 30px;
margin: 10px;
background: #fff;
color: red;
}
<div>xxx<span>xxx</span>xxx</div>

這里 span inline box 的高度就是 height + margin 2。如果 height 的值是 auto,高度就是等于 line-height + margin 2。
如果是一個(gè)非替換元素,高度由它的 line-height 決定,而不是 content area,雖然有時(shí)候看起來(lái)像 content area 撐開(kāi)了 line box 的高度。
div {
background-color: #ccc;
font-size: 20px;
color: #fff;
font-family: Sana;
}
span {
background: #fff;
color: red;
}
<div>xxx<span>xxx</span>xxx</div>

這張圖片可以明顯地看出撐開(kāi) line box 的是 line-height,而不是 content area。
這篇文章用了 virtual-area height 來(lái)表示 line-height 撐開(kāi)的高度,而我的理解其實(shí)就是 inline box 的高度。
line box 中所有 inline box 的最高點(diǎn)以及最低點(diǎn)決定了它的高度(該計(jì)算包括了 strut 的高度,后文會(huì)提到 strut)。
非替換元素的的 margin,padding 以及 border 并不會(huì)影響 line box 高度的計(jì)算。當(dāng)一個(gè) inline-level box 的 line-height 小于 content area 的時(shí)候,line box 的高度就會(huì)小于 content area,此時(shí)元素的 background 以及 padding 等就會(huì)溢出到 line box 之外。
以下代碼可以說(shuō)明這個(gè)問(wèn)題:
div {
background: #eee;
border: 1px solid #000;
box-sizing: border-box;
font-size: 50px;
line-height: 10px;
}
span {
background: red;
margin: 10px;
padding: 10px;
}
<div><span>xxx</span></div>

leading:
content area 的高度與 inline box 的高度差就是 leading,這個(gè) leading 會(huì)等分被添加到 content area 的頂部與底部,所以說(shuō) content area 永遠(yuǎn)位于 inline box 的中間(垂直居中)。
strut:
瀏覽器認(rèn)為每一個(gè) line box 的起始位置都存在一個(gè)寬度為 0,沒(méi)有任何字符的 匿名 inline box,稱為 strut,這個(gè) strut 是會(huì)從父元素繼承 line-height 的,因此它的高度會(huì)影響整個(gè) line box 高度的計(jì)算。
一個(gè)例子

div { background: #eee; border: 1px solid #000; box-sizing: border-box; }
<div><img src="./image.png" alt=""></div>
在圖片中可以看到 img 與外層的 div 存在一個(gè)間隙,這就是上文提到的 strut 造成的。
在這個(gè)例子中,默認(rèn)情況下 img 的底邊與父元素的基線對(duì)齊(img { vertical-align: baseline }),而這個(gè)基線實(shí)際上就是 strut 基線所在的位置。如下圖所示:

strut 其實(shí)就相當(dāng)于一個(gè)不可見(jiàn)的字母 x,上文已經(jīng)提到 strut 本身是具有 line-height 的,所以就導(dǎo)致圖片底部多了一段間隙。
總結(jié)一下存在間隙原因:
- strut 存在 line-height
- vertical-align 默認(rèn)值為 baseline
對(duì)應(yīng)的解決方案:
- 修改 strut 的 line-height,因?yàn)?strut 的 line-height 不是能夠直接設(shè)置的,所以需要設(shè)置父元素的 line-height,然后讓 strut 繼承,或者修改 font-size
- 將 vertical-align 設(shè)置為其他值line-height
W3C 中對(duì)于 line-height 的解釋是這樣的:
On a block container element whose content is composed of inline-level elements, 'line-height' specifies the minimal height of line boxes within the element. The minimum height consists of a minimum height above the baseline and a minimum depth below it, exactly as if each line box starts with a zero-width inline box with the element's font and line height properties. We call that imaginary box a "strut."
我的簡(jiǎn)單理解是,對(duì)于由行內(nèi)元素組成的塊級(jí)元素而言,line-height 決定了 line box 的最小高度,瀏覽器會(huì)假定每一個(gè) line box 以一個(gè)寬度為 0 的 inline box (strut)開(kāi)始,而這個(gè) strut 從父元素繼承到 font 以及 line-height。
- normal 是 line-height 的默認(rèn)值,W3C 對(duì)它并沒(méi)有一個(gè)明確的定義。normal 會(huì)將 content area 作為一個(gè)計(jì)算因素。
- line-height 并不是兩條 baseline 之間的距離。
line-height的值推薦使用數(shù)值,而不是使用 em 單位,因?yàn)?em 單位會(huì)根據(jù)從父元素繼承到的font-size來(lái)計(jì)算行高。
vertical-align
W3C 對(duì) baseline 以及 middle 的定義如下:
baseline: Align the baseline of the box with the baseline of the parent box. If the box does not have a baseline, align the bottom margin edge with the parent's baseline.
元素基線與父元素基線對(duì)齊,如果元素沒(méi)有基線,比如 img,則使用 margin 底邊與父元素基線對(duì)齊。
middle: Align the vertical midpoint of the box with the baseline of the parent box plus half the x-height of the parent.
元素的垂直中點(diǎn)位置與父元素的基線加上一半 x-height 的位置對(duì)齊。
參考
Deep dive CSS: font metrics, line-height and vertical-align
https://meyerweb.com/eric/css/inline-format.html
https://www.zhangxinxu.com/wordpress/2015/08/css-deep-understand-vertical-align-and-line-height/
https://www.w3.org/TR/CSS2/visudet.html#inline-box-height
相關(guān)文章

CSS3打造的現(xiàn)代交互式登錄界面詳細(xì)實(shí)現(xiàn)過(guò)程
本文介紹CSS3和jQuery在登錄界面設(shè)計(jì)中的應(yīng)用,涵蓋動(dòng)畫、選擇器、自定義字體及盒模型技術(shù),提升界面美觀與交互性,同時(shí)優(yōu)化性能和可訪問(wèn)性,感興趣的朋友跟隨小編一起看看吧2025-06-20CSS普通流、浮動(dòng)與定位網(wǎng)頁(yè)布局三大機(jī)制及最佳實(shí)踐
本文給大家講解CSS 的三種核心布局機(jī)制——普通流(Normal Flow)、浮動(dòng)(Float)和定位(Positioning)對(duì)于創(chuàng)建靈活、響應(yīng)式的網(wǎng)頁(yè)至關(guān)重要,本文將深入探討這三種機(jī)制的工作原2025-06-19
css實(shí)現(xiàn)角標(biāo)效果并帶有文章或圖標(biāo)效果(完整代碼)
文章介紹如何用CSS實(shí)現(xiàn)角標(biāo)效果,通過(guò).active類結(jié)合::after和::before偽元素,利用定位、邊框和旋轉(zhuǎn)創(chuàng)建紅色邊框與白色三角形的提示標(biāo)志,適用于按鈕或卡片元素的視覺(jué)增強(qiáng)設(shè)計(jì)2025-06-19CSS Anchor Positioning重新定義錨點(diǎn)定位的時(shí)代來(lái)臨(最新推薦)
CSS Anchor Positioning是一項(xiàng)仍在草案中的新特性,由 Chrome 125 開(kāi)始提供原生支持需啟用實(shí)驗(yàn) flag,它允許你在 CSS 中通過(guò)錨點(diǎn)(anchor)來(lái)相對(duì)于任意 DOM 元素定位,本文2025-06-17CSS中的Static、Relative、Absolute、Fixed、Sticky的應(yīng)用與詳細(xì)對(duì)比
CSS 中的 position 屬性用于控制元素的定位方式,不同的定位方式會(huì)影響元素在頁(yè)面中的布局和層疊關(guān)系,以下是 static、relative、absolute、fixed、sticky 的詳細(xì)對(duì)比和應(yīng)用2025-06-17CSS place-items: center解析與用法詳解
place-items: center; 是一個(gè)強(qiáng)大的 CSS 簡(jiǎn)寫屬性,用于同時(shí)控制 網(wǎng)格(Grid) 和 彈性盒(Flexbox) 布局中的對(duì)齊方式,本文給大家介紹CSS place-items: center; 詳解與用法2025-06-17
CSS實(shí)現(xiàn)元素?fù)螡M剩余空間的五種方法
在日常開(kāi)發(fā)中,我們經(jīng)常需要讓某個(gè)元素占據(jù)容器的剩余空間,本文將介紹5種不同的方法來(lái)實(shí)現(xiàn)這個(gè)需求,并分析各種方法的優(yōu)缺點(diǎn),感興趣的朋友一起看看吧2025-06-17CSS中前端單位 px、vw、vh 等的區(qū)別與使用建議
CSS單位區(qū)別與使用場(chǎng)景總結(jié):px絕對(duì)、vw/vh響應(yīng)式,%繼承父尺寸,em/rem文字縮放,vmin/vmax適應(yīng)寬高變化,固定布局用px或%,響應(yīng)式布局用vw/vh/rem,文字用em或rem,本文給大家2025-06-16CSS 樣式表的四種應(yīng)用方式及css注釋的應(yīng)用小結(jié)
這篇文章主要介紹了CSS 樣式表的四種應(yīng)用方式及css注釋的應(yīng)用小結(jié),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2025-05-21- 在CSS布局中,padding屬性是控制元素內(nèi)容與其邊框之間距離的關(guān)鍵工具,本文介紹CSS基礎(chǔ)中padding,通過(guò)本文的介紹,我們深入了解了padding的基本概念、簡(jiǎn)寫方法以及它對(duì)元2025-05-16




