問題背景:
昨晚在某個群病附,看到一個問題: 大概是CSS動畫,color屬性能夠生效,transform屬性卻沒能生效脐瑰。
作者寫了個demo,重現(xiàn)了一下問題,** 更多請戳demo**
.tow{
width: 200px;
height: 200px;
border-radius: 50%;
background-color: red;
/*display: inline-block;*/
animation: test 5s 1s infinite;
text-align: center;
line-height:200px;}
@keyframes test {
0%{
transform: translateX(0px);
}
100%{
background-color: darkturquoise;
transform: translateX(500px);
}}
<span class="tow">tow</span>
上面這段代碼只能看到背景顏色的轉(zhuǎn)變,卻看不到transform的變化
最后這位同學(xué)在樣式中設(shè)置了display:inline-block就搞定了
當(dāng)時我正在復(fù)習(xí)概率論省撑。今天查閱了些資料,大致弄明白其中的道理。
transformable element
A transformable element is an element in one of these categories:
an element whose layout is governed by the CSS box model which is either a block-level or atomic inline-level element,or whose display property computes to table-row, table-row-group, table-header-group, table-footer-group, table-cell, or table-caption [CSS21]
an element in the SVG namespace and not governed by the CSS box model which has the attributes transform, ‘patternTransform‘ or gradientTransform [SVG11].
注意到下劃線部分: which is either a block-level or atomic inline-level element
transform適用的范圍包括但不限于a block-level or atomic inline-level element
block-level本文并不討論悯周。這里我想談?wù)処nline-level elements , inline boxes ,Inline-level boxes
依舊先看看規(guī)范
Inline-level elements and inline boxes
Inline-level elements are those elements of the source document that do not form new blocks of content; the content is distributed in lines (e.g., emphasized pieces of text within a paragraph, inline images, etc.). The following values of the ‘display’ property make an element inline-level: ‘inline’, ‘inline-table’, and ‘inline-block’. Inline-level elements generate inline-level boxes, which are boxes that participate in an inline formatting context.
An inline box is one that is both inline-level and whose contents participate in its containing inline formatting context. A non-replaced element with a ‘display’ value of ‘inline’ generates an inline box. Inline-level boxes that are not inline boxes (such as replaced inline-level elements, inline-block elements, and inline-table elements) are called atomic inline-level boxes because they participate in their inline formatting context as a single opaque box.
上面這兩段我反復(fù)讀了數(shù)十次,一直翻譯不好。
最后在MDN上看到一段翻譯,如下
行內(nèi)級元素生成行內(nèi)級盒(inline-level boxes)陪竿,參與行內(nèi)格式化上下文(inline formatting context)禽翼。同時參與生成行內(nèi)格式化上下文的行內(nèi)級盒稱為行內(nèi)盒(Inline boxes)。所有display:inline 的非替換元素生成的盒是行內(nèi)盒族跛。而不參與生成行內(nèi)格式化上下文的行內(nèi)級盒稱為原子行內(nèi)級盒(atomic inline-level boxes)闰挡。這些盒由可替換行內(nèi)元素,或 display 值為 inline-block 或 inline-table 的元素生成礁哄,不能拆分成多個盒
或許你會有些疑惑,為什么和你的翻譯不一樣长酗。文末會進(jìn)行解答
看看下圖加深理解
當(dāng)一個元素的display的值為inline,inline-table,inline-block時,這個元素是行內(nèi)級元素/內(nèi)聯(lián)級元素(Inline-level elements)。這些行內(nèi)級元素會生成一個行內(nèi)級盒,即inline-level boxes桐绒。倘若這個盒子還同時參與生成行內(nèi)格式化上下文(IFC),該行內(nèi)級盒稱為行內(nèi)盒(Inline boxes)夺脾。而用inline-level boxes集合減去Inline boxes集合便是atomic inline-level boxes(原子行內(nèi)級盒)之拨。即是不參與生成行內(nèi)格式化上下文的行內(nèi)級盒。** 這些盒由可替換行內(nèi)元素咧叭,或 display 值為 inline-block 或 inline-table 的元素生成蚀乔,不能拆分成多個盒。**
至于什么是可替換元素,什么是不可替換元素,戳這兒
回到最初,這位同學(xué)之所以在樣式中設(shè)置了display:inline-block就解決了問題菲茬。
是因為transform作用于transformable element(作者在這里譯為可變元素), 有以下兩種
1.an element whose layout is governed by the CSS box model which is either a block-level or atomic inline-level element, or whose display property computes to table-row, table-row-group, table-header-group, table-footer-group, table-cell, or table-caption [CSS21]
2.an element in the SVG namespace and not governed by the CSS box model which has the attributes transform, ‘patternTransform‘ or gradientTransform [SVG11].
span會形成inline boxes,而不會形成atomic inline-level boxes吉挣。span本身并不是可變元素,在設(shè)置display:inline-block就是atomic inline-level boxes。
在查閱文檔過程中遇到幾個問題
1.規(guī)范在對atomic inline-level boxes出現(xiàn)了 a single opaque box生均。single比較好理解,單個的,不可拆分的听想。而opaque是不透明的意思,MDN上的翻譯,直接忽略了這個詞腥刹。規(guī)范中確乎有一處進(jìn)行了描述,戳這兒马胧。這里作者的理解就是從字面意思理解,不透明的。如果你有更好的理解,可以告訴我衔峰。
2.第二個問題,是規(guī)范中有處錯誤,導(dǎo)致我的走了一些彎路佩脊。這里指出
Inline-level boxes that are not inline boxes (such as replaced inline-level elements, inline-block elements, and inline-table elements) are called atomic inline-level boxes because they participate in their inline formatting context as a single opaque box.
在上面這段中,有兩處下滑線和一對括號。** 括號是應(yīng)該緊放在需要進(jìn)行形容,具體說明的詞的后方垫卤。 **按照這段文字的表達(dá),括號中的內(nèi)容是對inline boxes進(jìn)行說明的威彰。而實際上括號中的內(nèi)容是對inline-boxes的具體詮釋。
作者便是在這兒疑惑不已,耽誤了幾個小時穴肘。
本文已經(jīng)更新在我的個人博客 ,歡迎訪問