CSS包含块

1,写在前面的话

CSS目前还不被认可是一门真正的编程语言,虽然现在已经有像less、saas、stylus这样的预处理器或者叫预处理框架,使CSS更具动态性,拥有诸如变量、继承、运算、函数等编程特性,但是我们不可忽视CSS在前端开发中的地位,特别是在互联网(移动互联网)时代,前端作为直面用户的“门户”,对视觉与交互要求越来越高,CSS就如是“门户”的装潢有者不可替代的地位,在发挥着越来越重要的作用。

正是因为CSS的声明式特点与编程上的特殊性,CSS上手很容易,但是精深却很难,我们需要系统性地学习并应用它。我们从最基础,最不容易懂而且很容易被忽视的地方开始,从规范或者权威解读里出发,一步一步深入CSS的世界。

2,什么是包含块

首先,我们来看下W3C CSS规范中对包含块的定义:

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)“有时”:这意味者并不是所有元素都有包含块。

理清了定义后,我们来看看包含块具体的规则与详细解释

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.

根元素所在的包含块是一个称为初始包含块的矩形。对于连续媒体,它具有视口的尺寸,并固定在画布原点;而对于分页媒体,则是一个页面的区域。初始包含块的“ 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 )的内容区域决定的。

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.

如果元素具有“位置:固定”,则在连续媒体的情况下由视口建立包含块,在分页媒体的情况下由页面区域建立包含块。

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.

如果元素具有“位置:绝对”,则包含块由最接近的祖先( 位置属性为“绝对”或“相对”或“固定”)建立,具体细则如下: 在祖先是内联元素的情况下,包含块是为该元素生成的第一个和最后一个内联框的填充框周围的 内边距区域框。在CSS 2.1中,如果将inline元素分成多行,则包含块未定义。

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

否则,包含块由祖先的填充内边距区域形成。

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

如果没有这样的祖先,则包含块为初始包含块。

上面都是文字描述,有点不够形象,我们引用个导图来总结下:

3,包含块的作用和意义

包含块我们平时“看不到”、“摸不着”,那理解它有什么意义呢?

意义大着呢,因为山就在那里!

包含块(Containing Block)是视觉格式化模型(该文集里有相应的解读)的一个重要概念,它与框模型(盒模型)类似,也可以理解为一个矩形,而这个矩形的作用是为它里面包含的元素提供一个参考,元素的尺寸和位置的计算往往是由该元素所在的包含块决定的。

如果深入了解包含块等重要基础知识,则在日常的开发比如页面布局,还有问题解决时,就更有底气,更能够找到问题点,对开发的价值也是有重大意义的。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,012评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,589评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,819评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,652评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,954评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,381评论 1 210
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,687评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,404评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,082评论 1 238
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,355评论 2 241
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,880评论 1 255
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,249评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,864评论 3 232
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,007评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,760评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,394评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,281评论 2 259

推荐阅读更多精彩内容

  • 定义 css中,定位一个元素需要计算其和另外一个矩形区域的相对位置,这个矩形区域就是该元素的包含块。一般来讲,一个...
    狐尼克朱迪阅读 1,646评论 0 1
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,391评论 0 13
  • 根元素:初始包含块 static/relative 元素:由它最近的 block、table cell(类似 ...
    bigggge阅读 882评论 0 0
  • 亲爱的陈皮: 你好。 爸爸明早回太原,大概下午来接咱。 今天给你拍照,顾着玩东西不配合做表情了。坐着拍...
    猫悟空阅读 110评论 0 0
  • 11月13日 最近几天气温回升了,白天的温度都在零上了,但我一个人还是懒得出去,就喜欢一个人宅在家里做些家务,喝一...
    有意無意阅读 138评论 1 3