摘要:我認(rèn)為應(yīng)該將理解為。如果的值是,高度就是等于。中所有的最高點(diǎn)以及最低點(diǎn)決定了它的高度該計(jì)算包括了的高度,后文會(huì)提到。非替換元素的的,以及并不會(huì)影響高度的計(jì)算。并不是兩條之間的距離。元素的垂直中點(diǎn)位置與父元素的基線加上一半的位置對(duì)齊。
文章 GitHub 地址:https://github.com/afishhhhh/blog/issues/4
文章如有錯(cuò)誤,請(qǐng)各位能夠指出。
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; }文字1文字2文字3
白色的文字就是一個(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; }xxxxxxxxx
這里 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; }xxxxxxxxx
這張圖片可以明顯地看出撐開(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; }leading:xxx
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; }
在圖片中可以看到 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-heightW3C 中對(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-alignW3C 對(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
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/117128.html
摘要:但實(shí)質(zhì)上,只要是內(nèi)聯(lián)元素,這兩個(gè)元素都會(huì)同時(shí)在起作用。而解決方案可以有以下幾種元素不使用基線對(duì)齊,可以改為對(duì)齊元素塊狀化設(shè)置為設(shè)置為總結(jié)講解了的各類屬性值及其效果起作用的前提是內(nèi)聯(lián)元素與都是同時(shí)作用在內(nèi)聯(lián)元素上的 前言 vertical-align用來(lái)指定行內(nèi)元素(inline)或表格單元格(table-cell)元素的垂直對(duì)齊方式。也就是說(shuō),對(duì)于塊級(jí)元素,vertical-align...
摘要:下的屬性值詳解以下內(nèi)容均在中測(cè)試默認(rèn)對(duì)齊方式這里作為參考系,而它的就是所要對(duì)齊的了。沒(méi)有任何變化。那改變又如何呢為了讓的清晰可見(jiàn),特意添加一個(gè)的包裹著。 前言 一直聽(tīng)說(shuō)line-height是指兩行文本的基線間的距離,然后又說(shuō)行高等于行距,最近還聽(tīng)說(shuō)有個(gè)叫行間距的家伙,@張?chǎng)涡襁€說(shuō)line-height和vertical-align基情四射,貴圈真亂啊。。。。。。于是通過(guò)本篇來(lái)一探究竟...
摘要:上例中,左邊盒子的基線為其底邊緣,右邊盒子的基線為的基線將右邊盒子的行高設(shè)置為,即這個(gè)的高度為,位置處于中間。 vertical-align的值 線類:baseline(默認(rèn)), top, bottom, middle 文字類:text-top, text-bottom 上標(biāo)下標(biāo)類:sub, super 數(shù)值:1px, 1em - 在baseline對(duì)齊的基礎(chǔ)上上下偏移一定數(shù)值 百分...
摘要:關(guān)于的條參考線以及。關(guān)于和之間的關(guān)系。目標(biāo)元素的的垂直平分線,與父元素內(nèi)匿名的對(duì)齊。垂直平分線,這個(gè)容易理解。要比這些字母的頂端再高一些。 前言 關(guān)于 vertical-align,我們很容易想到,這不就是告訴我們?cè)卦诳v向上和什么對(duì)齊的屬性嗎?而事實(shí)上,正是這種寬泛的說(shuō)法導(dǎo)致了我們對(duì)其的理解存在許多不確定性。事實(shí)上,對(duì)齊這兩個(gè)字其實(shí)牽涉到4個(gè)對(duì)象:即哪個(gè)對(duì)象的哪條線,與哪個(gè)對(duì)象的哪條...
摘要:接下來(lái)說(shuō)句聽(tīng)起來(lái)很奇怪的話一個(gè)內(nèi)聯(lián)元素有兩個(gè)高度高度和實(shí)際區(qū)域高度是我自己發(fā)明的單詞,它表示對(duì)人類有效的高度,你在其他地方是看不到這個(gè)單詞的。你沒(méi)看錯(cuò),是計(jì)算的一些細(xì)節(jié)對(duì)于內(nèi)聯(lián)元素,和會(huì)增大區(qū)域,但是不會(huì)增大不是的高度。 本文為饑人谷講師方方原創(chuàng)文章,首發(fā)于 前端學(xué)習(xí)指南。 這是一篇譯文,對(duì) inline 和 inline-block 的元素剖析非常給力。 原文:Deep dive C...
閱讀 2745·2023-04-25 14:21
閱讀 1184·2021-11-23 09:51
閱讀 4032·2021-09-22 15:43
閱讀 617·2019-08-30 15:55
閱讀 1566·2019-08-29 11:28
閱讀 2451·2019-08-26 11:44
閱讀 1690·2019-08-23 18:15
閱讀 2887·2019-08-23 16:42