摘要:下的屬性值詳解以下內(nèi)容均在中測(cè)試默認(rèn)對(duì)齊方式這里作為參考系,而它的就是所要對(duì)齊的了。沒有任何變化。那改變又如何呢為了讓的清晰可見,特意添加一個(gè)的包裹著。
前言
一直聽說line-height是指兩行文本的基線間的距離,然后又說行高等于行距,最近還聽說有個(gè)叫行間距的家伙,@張?chǎng)涡襁€說line-height和vertical-align基情四射,貴圈真亂啊。。。。。。于是通過本篇來一探究竟:)
line-height到底有多height? 行距、行間距傻傻分不清?首先看看“有道詞典”的解析!
Leading = Line Space + Font Size(即是 行距 = 行間距 + 字體大小)
Leading: 指相鄰文本行間上一個(gè)文本行基線和下一文本行基線間的距離。
Line Space: 指相鄰文本行間上一個(gè)文本行下行線(ascent)和下一文本行上行線(descent)間的距離。
而在一些面向普通消費(fèi)者的軟件中,Leading往往是指Line Space。Leading
?在CSS當(dāng)中,Leading就是指Line Space。而CSS屬性line-height則是用于設(shè)置真實(shí)的Leading。參考
Still for each glyph, determine the leading L to add, where L = "line-height" - AD
AD是指字形ascent和descent間的距離,即是font-size。
?這里為更清晰地?cái)⒄f,我將以廣義Leading指代行間距,而狹義Leading則指代行距。
從W3C Rec中看出,line-height就是狹義Leading,而line-height的字面意思即為“行高”,推導(dǎo)結(jié)果CSS中行高即是行距。
這里我們了解到行高,行距和行間距的區(qū)別了。那接下來要介紹line-height的一個(gè)重要特性——垂直居中性。
?通過L = "line-height" - AD我們知道line-height=行間距+字形大小,字形大小我們可以通過font-size來設(shè)置,而line-height就更不用說了,而家問題是行間距所占的空間是怎樣分配的呢?
方案1:不是說行間距就是上一行的descent到下一行的ascent間的距離嗎?那直接分配到A位置就好了。
方案2:如果方案1的分配方案合理,那么分配到B位置就也是OK的。
方案3:一邊倒的分配方案太不美觀了吧!不如將行間距對(duì)半開,然后分別分配到上下兩端不就OK了嗎!
CSS采用的就是方案3。這是引用了Half-leading的概念,Half-leading = Leading/2.
Half the leading is called the half-leading. User agents center glyphs vertically in an inline box, which adds half-leading on the top and bottom. For example, if a piece of text is "12pt" high and the line-height value is "14pt", 2pt of extra space should be added: 1pt above and 1pt below the text (this applies to empty boxes as well, as if the empty box contained zero-height text).參考
在深入垂直居中性之前,我們先看一個(gè)容易引起誤解的示例。(其實(shí)是我自己誤解而已:()
center glyphs vertically in an inline box.
不是說好了會(huì)垂直居中嗎?你看字母x明明就在div#container中線的下方呢!
我們用空格符代替文字就可以看清楚了。
“垂直居中”是指字形所在的盒子的垂直中線與line-height所占據(jù)的盒子的垂直中線重合,不是指字形的mean line和line-height所占據(jù)的盒子的垂直中線重合。
從L = "line-height" - AD可以知道行間距可能會(huì)負(fù)數(shù),那么垂直居中性還有效嗎?
答案是肯定的,L為負(fù)數(shù)時(shí),Half-leading自然也是負(fù)數(shù),只是上下兩端從增加空間變?yōu)闇p少等量空間而已。不信你看
line-height屬性x
"line-height"
Value: normal || | | inherit
Initial: normal
Applies to: all elements
Inherited: yes
Percentages: refer to the font size of the element itself
Media: visual
Computed value: forand the absolute value; otherwise as specified normal
Tells user agents to set the used value to a "reasonable" value based on the font of the element. The value has the same meaning as. We recommend a used value for "normal" between 1.0 to 1.2. The computed value is "normal".
?normal其實(shí)就是一個(gè)
line-height-x-Lline-height-x-Lline-height-x-L
Chrome43的結(jié)果
14/10 = 1.4 45/40 = 1.125 92/80 = 1.15 average: 1.225 約為1.2
Firefox44.0.2
13/10 = 1.3 46/40 = 1.15 92/80 = 1.15 average: 1.2
IE9
11/10 = 1.1 46/40 = 1.15 92/80 = 1.15 average: 1.13333 約為1.1
通過小數(shù)據(jù)統(tǒng)計(jì)得出normal值的規(guī)律:
不同瀏覽器的normal值不相同;
同一個(gè)瀏覽器下,font-size值不同,normal值也會(huì)有變化;
同一瀏覽器下,font-size值相同,font-family值不同,normal值也會(huì)有變化;
normal的平均值確實(shí)是在1.0~1.2之間(含兩端),但具體到特定瀏覽器、font-family和font-size時(shí),normal的實(shí)際值可能會(huì)大于1.2。
The specified length is used in the calculation of the line box height. Negative values are illegal.
?設(shè)置固定值,單位可以是px、pt。好處是簡(jiǎn)單——設(shè)置是什么,line-height的實(shí)際高度就是什么。壞處是子元素默認(rèn)情況下會(huì)繼承父容器的line-height屬性,若子元素的font-size大于父容器的font-size屬性值,那么子元素的文本行會(huì)十分密集,降低可閱讀性。所以我們一般采用相對(duì)font-size實(shí)際大小來設(shè)置line-height值的方式,如默認(rèn)normal方式。
The computed value of the property is this percentage multiplied by the element"s computed font size. Negative values are illegal.
?既然采用
子元素繼承的是父容器實(shí)際的line-height值。也就是說父容器設(shè)置為font-size:20px;line-height:200%;,那么子元素繼承來的line-height值為40px,而不是200%。因此又回到
The used value of the property is this number multiplied by the element"s font size. Negative values are illegal. The computed value is the same as the specified value.
?
?其實(shí)line-height:1.2em;和line-height:1.2;是等價(jià)的。若想將參考系改為根元素的font-size,則需要采用CSS3新增的line-height:1.2rem單位了。
?根據(jù)WCAG2.0(萬維網(wǎng)內(nèi)容可存取性指南)規(guī)定“段落中的行距至少要1.5倍”,那么是否在body那設(shè)置一個(gè)
CSS魔法堂:深入理解line-height和vertical-align
In my dual profession as an educator and health care provider, I have worked with numerous children infected with the virus that causes AIDS. The relationships that I have had with these special kids have been gifts in my life. They have taught me so many things, but I have especially learned that great courage can be found in the smallest of packages. Let me tell you about Tyler.
看對(duì)于h1標(biāo)題欄而言,行距太多了。于是得出如下配置:
body{line-height:1.5;} h1,h2,h3,h4,h5,h6{line-height:1.2;}vertical-align到底如何對(duì)齊呢?
?下面我們稍微將line-height垂直居中特性中Leading為負(fù)數(shù)的示例代碼修改一下,將font-size:90px;line-height:10px;遷移到子元素中.
x
不是說垂直居中嗎?這里就涉及到一個(gè)相對(duì)復(fù)雜的CSS垂直對(duì)齊規(guī)則——vertical-align。
注意:前方高能,需要IFC、line box作為前提知識(shí)。(可參考CSS魔法堂:重新認(rèn)識(shí)Box Model、IFC、BFC和Collapsing margins)
"vertical-align"
Value: baseline | sub | super | top | text-top | middle | bottom | text-bottom || | inherit
Initial: baseline
Applies to: inline-level and "table-cell" elements
Inherited: no
Percentages: refer to the "line-height" of the element itself
Media: visual
Computed value: forand the absolute length, otherwise as specified
baseline:默認(rèn)值。元素的基線與父元素的基線對(duì)齊;
top:把元素line box上邊框?qū)R父元素的line box上邊框;
text-top:把元素line box上邊框?qū)R父元素的ascent(即content top edge);
super:升高元素的基線到父元素合適的上標(biāo)位置;
middle:把元素line box中垂點(diǎn)與父元素基線 + x-height/2的高度對(duì)齊;
sub:降低元素的基線到父元素合適的下標(biāo)位置;
text-bottom:把元素line box下邊框?qū)R父元素的descent(即content bottom edge);
bottom:把元素line box下邊框?qū)R父元素的line box下邊框;
inherit:繼承父元素的對(duì)齊方式。
?怎么這么多規(guī)則要記啊?我記性不好難道到時(shí)還要挨個(gè)查嗎?其實(shí)歸納一下就OK了!
對(duì)齊操作必定涉及操作元素和參考系元素,而vertical-align的值全是指參考系元素的位置,而操作元素則以baseline或linebox上中下作對(duì)齊;
默認(rèn)對(duì)齊方式為baseline,數(shù)量值均是相對(duì)于baseline而言。
vertical-align僅對(duì)inline-level和table-cell元素有效注意:vertical-align僅對(duì)inline-level和table-cell元素有效,下面示例無效是正常不過的。
IE9+下的vertical-align屬性值詳解(以下內(nèi)容均在Chrome43中測(cè)試)I"m formerI"m latter
1.默認(rèn)對(duì)齊方式——baseline
line-height x vertical-align x for reference frame
這里x for reference frame作為參考系,而它的baseline就是span#obj所要對(duì)齊的baseline了。
那么在baseline的基礎(chǔ)上的設(shè)置
line-height x vertical-align x for reference frameline-height x vertical-align x for reference frameline-height x vertical-align x for reference frameline-height x vertical-align x for reference frame
2.top——把元素line box上邊框?qū)R父元素的line box上邊框
?我們將上面的示例稍微改一下
line-height x vertical-align
x for reference frame
確實(shí)不同了,但這無法證明是元素的line box上邊框?qū)R父元素的line box上邊框哦。那么我們改改代碼看看
line-height x vertical-align x for reference frame
通過line-height:1使line box與content box/area的高度一致,雖然span#parent和span#obj的上邊框?qū)R,但還不能說明什么。
line-height x vertical-align x for reference frame
沒有任何變化。那改變line-height又如何呢?
line-height x vertical-align x for reference frame
為了讓span#obj的Half-leading清晰可見,特意添加一個(gè)display:inline-block的inline box包裹著span#obj。而span#parent也增大了Half-leading的高度?,F(xiàn)在可以我們清晰看到確實(shí)是span#obj的line box的上邊框?qū)R父元素的line box上邊框。(同理證明了vertical-align:bottom是把元素line box下邊框?qū)R父元素的line box下邊框;)
注意:chrome下若外層div不添加font-size:14px;line-height:1;屬性,將導(dǎo)致span#parent上有條空白間隙
原因十分簡(jiǎn)單,那是因?yàn)?b>span#parent的對(duì)齊方式是baseline,參考系是div的baseline,而div的line-height為normal,有空白間隙就是當(dāng)然的事了。通過JS就可以看清楚了。
var div = document.getElementsByTagName("div")[0] console.log(div.childNodes[0].nodeType) // 顯示3,就是TextNode
其實(shí)除了在div上設(shè)置line-height:1之外,我們還可以在span#parent上設(shè)置vertical-align:top來解決。
line-height x vertical-align x for reference frame
3.text-top——把元素的line box上邊框?qū)R父元素的ascent(即content top edge)
* x for reference frame
******* x for reference frame
4.middle——把元素line box中垂點(diǎn)與父元素基線 + x-height/2的高度對(duì)齊
*******x for reference frame
注意
當(dāng)元素的display:inline-block/inline-table等對(duì)應(yīng)的是atomic inline-level box時(shí),其line box高度為margin box的高度。若元素對(duì)應(yīng)的是inline box,則其最小高度為line-height,最大則由子盒子決定。
?由于我工作中沒有適配IE8等歷史瀏覽器的需求,因此詳細(xì)內(nèi)容請(qǐng)參考@張?chǎng)涡竦腃SS vertical-align的深入理解(二)之text-top篇
?簡(jiǎn)單來說IE5.5~IE8下vertical-align:text-top是把元素的ascent對(duì)齊父元素的ascent(即content top edge)
?到這里理論部分已經(jīng)介紹完了,是時(shí)候通過示例來驗(yàn)證自己了!
單行文字的垂直居中對(duì)齊行數(shù)不固定的多行文字的垂直居中對(duì)齊字體大小14px
* 字體大小為14px字體大小為30px
大小不固定的圖片的垂直居中對(duì)齊第一行文本
the second one.
總結(jié)
尊重原創(chuàng),轉(zhuǎn)載請(qǐng)注明來自:http://www.cnblogs.com/fsjohnhuang/p/5307706.html^_^肥子John
感謝深入了解css的行高Line Height屬性
我對(duì)CSS vertical-align的一些理解與認(rèn)識(shí)(一)
CSS vertical-align的深入理解(二)之text-top篇
CSS深入理解vertical-align和line-height的基友關(guān)系
css行高line-height的一些深入理解及應(yīng)用
大小不固定的圖片、多行文字的水平垂直居中
深入理解 CSS 中的行高與基線
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/115125.html
摘要:深入本屆集團(tuán)公司黨委由公司黨委由本屆集團(tuán)公司黨委由公司黨委由組均是,而組則是。下英文泰文等的默認(rèn)對(duì)齊方式,下的默認(rèn)對(duì)齊方式等同于,采用增加減少象形文字間的距離。 前言 也許提及text-align你會(huì)想起水平居中,但除了這個(gè)你對(duì)它還有多少了解呢?本篇打算和大家一起來跟text-align來一次負(fù)距離的交往,你準(zhǔn)備好了嗎? text-align屬性詳解 The text-align C...
摘要:前言繼上篇魔法堂稍稍深入偽類選擇器記錄完偽類后,我自然而然要向偽元素伸出魔掌的啦。和的注意事項(xiàng)默認(rèn)必須設(shè)置屬性,否則一切都是無用功默認(rèn),就是和的內(nèi)容無法被用戶選中的偽元素和偽類結(jié)合使用形如。 前言 ?繼上篇《CSS魔法堂:稍稍深入偽類選擇器》記錄完偽類后,我自然而然要向偽元素伸出魔掌的啦^_^。本文講講述偽元素以及功能強(qiáng)大的Contet屬性,讓我們可以通過偽元素更好地實(shí)現(xiàn)更多的可能! ...
摘要:更多關(guān)于的信息可參考魔法堂不得不說的因此的實(shí)際值則是相對(duì)于而言,我們可以通過來獲取和的實(shí)際值。對(duì)于由于自身有固有的,因此當(dāng)設(shè)置時(shí),其實(shí)際值就是元素固有的。結(jié)果就是除均不為,而為時(shí),會(huì)自動(dòng)計(jì)算以滿足等式。兩條不滿足外,其他情況均一致。 前言 當(dāng)我們以position:absolute之名讓元素脫離Normal flow的控制后,以為通過left和top屬性值即可讓元素得以無限的自由時(shí),卻...
摘要:前言是否曾經(jīng)被業(yè)務(wù)提出能改改這個(gè)單選框的顏色吧讓它和主題顏色搭配一下吧,然后苦于原生不支持換顏色,最后被迫自己手?jǐn)]一個(gè)湊合使用。設(shè)置為的樣式行為特征單選框的行為特征,明顯就是選中與否,及選中狀態(tài)的改變事件,因此我們必須保持對(duì)外提供事件。 前言 ?是否曾經(jīng)被業(yè)務(wù)提出能改改這個(gè)單選框的顏色吧!讓它和主題顏色搭配一下吧!,然后苦于原生不支持換顏色,最后被迫自己手?jǐn)]一個(gè)湊合使用。若拋開inpu...
閱讀 3495·2023-04-25 18:14
閱讀 1602·2021-11-24 09:38
閱讀 3330·2021-09-22 14:59
閱讀 3125·2021-08-09 13:43
閱讀 2644·2019-08-30 15:54
閱讀 619·2019-08-30 13:06
閱讀 1626·2019-08-30 12:52
閱讀 2776·2019-08-30 11:13