CSS包含塊

1资厉,寫在前面的話

CSS目前還不被認可是一門真正的編程語言,雖然現(xiàn)在已經(jīng)有像less蔬顾、saas宴偿、stylus這樣的預處理器或者叫預處理框架,使CSS更具動態(tài)性诀豁,擁有諸如變量窄刘、繼承、運算舷胜、函數(shù)等編程特性娩践,但是我們不可忽視CSS在前端開發(fā)中的地位,特別是在互聯(lián)網(wǎng)(移動互聯(lián)網(wǎng))時代烹骨,前端作為直面用戶的“門戶”翻伺,對視覺與交互要求越來越高,CSS就如是“門戶”的裝潢有者不可替代的地位沮焕,在發(fā)揮著越來越重要的作用吨岭。

正是因為CSS的聲明式特點與編程上的特殊性,CSS上手很容易峦树,但是精深卻很難辣辫,我們需要系統(tǒng)性地學習并應用它。我們從最基礎魁巩,最不容易懂而且很容易被忽視的地方開始急灭,從規(guī)范或者權威解讀里出發(fā),一步一步深入CSS的世界歪赢。

2化戳,什么是包含塊

首先,我們來看下W3C CSS規(guī)范中對包含塊的定義:

The position and size of an element's box(es) are sometimes calculated relative to a certain rectangle, called the containing block of the element. The containing block of an element is defined as follows:

The containing block in which the root element lives is a rectangle called the initial containing block. For continuous media, it has the dimensions of the viewport and is anchored at the canvas origin; it is the page area for paged media. The 'direction' property of the initial containing block is the same as for the root element.

For other elements, if the element's position is 'relative' or 'static', the containing block is formed by the content edge of the nearest block container ancestor box.

If the element has 'position: fixed', the containing block is established by the viewport in the case of continuous media or the page area in the case of paged media.

If the element has 'position: absolute', the containing block is established by the nearest ancestor with a 'position' of 'absolute', 'relative' or 'fixed', in the following way:

In the case that the ancestor is an inline element, the containing block is the bounding box around the padding boxes of the first and the last inline boxes generated for that element. In CSS 2.1, if the inline element is split across multiple lines, the containing block is undefined.

Otherwise, the containing block is formed by the padding edge of the ancestor.

If there is no such ancestor, the containing block is the initial containing block.

一大串英文,看起來容易頭暈点楼,沒關系我們來翻譯并拆解下:

The position and size of an element's box(es) are sometimes calculated relative to a certain rectangle, called the containing block of the element.

一個元素框(盒子扫尖,有可能有多個,比如行盒)的大小和位置的計算有時是和一個特定的矩形框相關的掠廓,這個矩形框就叫該元素的包含塊

上面這句話就給包含塊下了個定義换怖,其中有幾個點我們需要關注下

(1)“特定的矩形框”:包含塊的具象特征

(2)“有時”:這意味者并不是所有元素都有包含塊。

理清了定義后蟀瞧,我們來看看包含塊具體的規(guī)則與詳細解釋

The containing block of an element is defined as follows:

元素的包含塊具體定義如下:

The containing block in which theroot elementlives is a rectangle called theinitial containing block. For continuous media, it has the dimensions of theviewportand is anchored at the canvas origin; it is thepage areafor paged media. The 'direction' property of the initial containing block is the same as for the root element.

根元素所在的包含塊是一個稱為初始包含塊的矩形沉颂。對于連續(xù)媒體,它具有視口的尺寸悦污,并固定在畫布原點铸屉;而對于分頁媒體,則是一個頁面的區(qū)域切端。初始包含塊的“ direction”(排布方向:ltr or rtl)屬性與根元素相同彻坛。 --- 至此我們引出了初始包含塊。

For other elements, if the element's position is 'relative' or 'static', the containing block is formed by the content edge of the nearestblock containerancestor box.

對于其他元素踏枣,如果位置(定位)屬性值為static/relative昌屉,則它的 包含塊是由它最近的祖先元素的塊狀盒子(display為 block、table cell(類似 <td> 和 <th>) 或者 inline-block )的內(nèi)容區(qū)域決定的茵瀑。

If the element has 'position: fixed', the containing block is established by theviewportin the case of continuous media or the page area in the case of paged media.

如果元素具有“位置:固定”间驮,則在連續(xù)媒體的情況下由視口建立包含塊,在分頁媒體的情況下由頁面區(qū)域建立包含塊马昨。

If the element has 'position: absolute', the containing block is established by the nearest ancestor with a'position'of 'absolute', 'relative' or 'fixed', in the following way:

In the case that the ancestor is an inline element, the containing block is the bounding box around the padding boxes of the first and the last inline boxes generated for that element. In CSS 2.1, if the inline element is split across multiple lines, the containing block is undefined.

如果元素具有“位置:絕對”竞帽,則包含塊由最接近的祖先( 位置屬性為“絕對”或“相對”或“固定”)建立,具體細則如下: 在祖先是內(nèi)聯(lián)元素的情況下偏陪,包含塊是為該元素生成的第一個和最后一個內(nèi)聯(lián)框的填充框周圍的 內(nèi)邊距區(qū)域框抢呆。在CSS 2.1中煮嫌,如果將inline元素分成多行笛谦,則包含塊未定義。

Otherwise, the containing block is formed by thepadding edgeof the ancestor.

否則昌阿,包含塊由祖先的填充內(nèi)邊距區(qū)域形成饥脑。

If there is no such ancestor, the containing block is the initial containing block.

如果沒有這樣的祖先,則包含塊為初始包含塊懦冰。

上面都是文字描述灶轰,有點不夠形象,我們引用個導圖來總結下:

3刷钢,包含塊的作用和意義

包含塊我們平時“看不到”笋颤、“摸不著”,那理解它有什么意義呢?

意義大著呢伴澄,因為山就在那里赋除!

包含塊(Containing Block)是視覺格式化模型(該文集里有相應的解讀)的一個重要概念,它與框模型(盒模型)類似非凌,也可以理解為一個矩形举农,而這個矩形的作用是為它里面包含的元素提供一個參考,元素的尺寸和位置的計算往往是由該元素所在的包含塊決定的敞嗡。

如果深入了解包含塊等重要基礎知識颁糟,則在日常的開發(fā)比如頁面布局,還有問題解決時喉悴,就更有底氣棱貌,更能夠找到問題點,對開發(fā)的價值也是有重大意義的箕肃。

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末键畴,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子突雪,更是在濱河造成了極大的恐慌起惕,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,576評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件咏删,死亡現(xiàn)場離奇詭異惹想,居然都是意外死亡,警方通過查閱死者的電腦和手機督函,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,515評論 3 399
  • 文/潘曉璐 我一進店門嘀粱,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人辰狡,你說我怎么就攤上這事锋叨。” “怎么了宛篇?”我有些...
    開封第一講書人閱讀 168,017評論 0 360
  • 文/不壞的土叔 我叫張陵娃磺,是天一觀的道長。 經(jīng)常有香客問我叫倍,道長偷卧,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,626評論 1 296
  • 正文 為了忘掉前任吆倦,我火速辦了婚禮听诸,結果婚禮上,老公的妹妹穿的比我還像新娘蚕泽。我一直安慰自己晌梨,他們只是感情好,可當我...
    茶點故事閱讀 68,625評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著仔蝌,像睡著了一般砸逊。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上掌逛,一...
    開封第一講書人閱讀 52,255評論 1 308
  • 那天师逸,我揣著相機與錄音,去河邊找鬼豆混。 笑死篓像,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的皿伺。 我是一名探鬼主播员辩,決...
    沈念sama閱讀 40,825評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼鸵鸥!你這毒婦竟也來了奠滑?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,729評論 0 276
  • 序言:老撾萬榮一對情侶失蹤妒穴,失蹤者是張志新(化名)和其女友劉穎宋税,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體讼油,經(jīng)...
    沈念sama閱讀 46,271評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡杰赛,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,363評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了矮台。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片乏屯。...
    茶點故事閱讀 40,498評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖瘦赫,靈堂內(nèi)的尸體忽然破棺而出辰晕,到底是詐尸還是另有隱情,我是刑警寧澤确虱,帶...
    沈念sama閱讀 36,183評論 5 350
  • 正文 年R本政府宣布含友,位于F島的核電站,受9級特大地震影響蝉娜,放射性物質(zhì)發(fā)生泄漏唱较。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,867評論 3 333
  • 文/蒙蒙 一召川、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧胸遇,春花似錦荧呐、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,338評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽概疆。三九已至,卻和暖如春峰搪,著一層夾襖步出監(jiān)牢的瞬間岔冀,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,458評論 1 272
  • 我被黑心中介騙來泰國打工概耻, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留使套,地道東北人。 一個月前我還...
    沈念sama閱讀 48,906評論 3 376
  • 正文 我出身青樓鞠柄,卻偏偏與公主長得像侦高,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子厌杜,可洞房花燭夜當晚...
    茶點故事閱讀 45,507評論 2 359

推薦閱讀更多精彩內(nèi)容

  • 定義 css中奉呛,定位一個元素需要計算其和另外一個矩形區(qū)域的相對位置,這個矩形區(qū)域就是該元素的包含塊夯尽。一般來講瞧壮,一個...
    狐尼克朱迪閱讀 1,700評論 0 1
  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,464評論 0 13
  • 根元素:初始包含塊 static/relative 元素:由它最近的 block、table cell(類似 ...
    bigggge閱讀 910評論 0 0
  • 親愛的陳皮: 你好匙握。 爸爸明早回太原馁痴,大概下午來接咱。 今天給你拍照肺孤,顧著玩東西不配合做表情了罗晕。坐著拍...
    貓悟空閱讀 113評論 0 0
  • 11月13日 最近幾天氣溫回升了,白天的溫度都在零上了赠堵,但我一個人還是懶得出去小渊,就喜歡一個人宅在家里做些家務,喝一...
    有意無意閱讀 142評論 1 3