CSS > CSS3 中的层叠上下文解密

移动开发
现在该笔者上场翻译了!在解释上面术语之前,需要阐明两个术语:“定位”指的是 position 为 relative 、absolute 、fixed 的元素,“非定位”则相反。

1 CSS2.1 中规定的层叠上下文

 

  1. Background and borders — of the element forming the stacking context. The lowest level in the stack. 
  2.  
  3. Negative Z-Index — the stacking contexts of descendants elements with negative z-index. 
  4.  
  5. Block Level Boxes — in-flow non-inline-level non-positioned descendants. 
  6.  
  7. Floated Boxes — non-positioned floats 
  8.  
  9. Inline Boxes — in-flow inline-level non-positioned descendants. 
  10.  
  11. Z-index: 0 — positioned elements. These form new stacking contexts. 
  12.  
  13. Positive Z-index — positioned elements. The highest level in the stack.

现在该笔者上场翻译了!在解释上面术语之前,需要阐明两个术语:“定位”指的是 position 为 relative 、absolute 、fixed 的元素,“非定位”则相反。

  • 背景和边框:建立层叠上下文元素的背景和边框。层叠中的***级
  • 负 Z-index:z-index 为负的后代元素建立的层叠上下文
  • 块级盒:文档流内非行内级非定位后代元素
  • 浮动盒:非定位浮动元素(笔者注:即排除了 position: relative 的浮动盒)
  • 行内盒:文档流内行内级非定位后代元素
  • Z-index: 0:定位元素。这些元素建立了新层叠上下文(笔者注:不一定,详见后文)
  • 正 Z-index:(z-index 为正的)定位元素。层叠的***等级

引文如上所表。但笔者提醒各位读者一点,“Z-index: 0”级的定位元素不一定就会建立新的层叠上下文。因为:

  1. CSS2.1:(z-index: auto)The stack level of the generated box in the current stacking context is 0. The box does not establish a new stacking context unless it is the root element. 

当定位元素 z-index: auto,生成盒在当前层叠上下文中的层级为 0。但该盒不建立新的层叠上下文,除非是根元素。

规范是这样,但 IE6-7 有个 BUG,定位元素即便 z-index: auto 照样创建层叠上下文。

以上是基于 CSS2.1 的层叠上下文介绍。下面要阐述的是在 CSS3 新环境下,层叠上下文的新变化。

2 CSS3 带来的变化

总的来说变化可以归为两点,我们之后一一探讨:

CSS3 中许多属性会创建局部层叠上下文

tranform 属性改变绝对定位子元素的包含块

2.1 产生新层叠上下文的情况

以下情况会产生新的层叠上下文:

  • 根元素(HTML)
  • 绝对或相对定位且 z-index 值不为 auto
  • 一个伸缩项目 Flex Item,且 z-index 值不为 auto,即父元素 display: flex|inline-flex
  • 元素的 opacity 属性值小于 1
  • 元素的 transform 属性值不为 none
  • 元素的 mix-blend-mode 属性值不为 normal
  • 元素的 filter 属性值不为 normal
  • 元素的 isolation 属性值为 isolate
  • position: fixed
  • will-change 中指定了上述任意属性,即便你没有直接定义这些属性
  • 元素的 -webkit-overflow-scrolling 属性值为 touch

以上列表译自:

https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context,提醒广大读者,别看中文版,因为中文版并非实时跟进更新的,且翻译不太准确

2.2 提升层叠上下文中的层级

以上元素建立新层叠上下文的同时,也会提升元素自身所在层叠上下文中的层级。

我们以 opacity 为例。来看下 CSS3 规范中的话:

  1. If an element with opacity less than 1 is not positioned, implementations must paint the layer it creates, within its parent stacking context, at the same stacking order that would be used if it were a positioned element with ‘z-index: 0’ and ‘opacity: 1’. If an element with opacity less than 1 is positioned, the ‘z-index’ property applies as described in [CSS21], except that ‘auto’ is treated as ‘0’ since a new stacking context is always created. 

如果元素 opacity 小于 1 且未定位,则必须在其父层叠上下文中,按其在定位了的、z-index: 0 且 opacity: 1 的情况中的层叠顺序绘制。如果 opacity 小于 1 且已定位,z-index 属性按 CSS2.1 应用,但 auto 要视为 0,因为新的层叠上下文总是创建了的。

如下案例:

  1. div { 
  2. width: 100px; 
  3. height: 100px; 
  4. #box1 { 
  5. position: absolute; 
  6. background: red; 
  7. top: 40px; 
  8. left: 40px; 
  9. #box2 { 
  10. background: blue; 
  11.  
  12. <body> 
  13. <div id="box1"></div> 
  14. <div id="box2"></div> 
  15. <body> 

以上 CSS 和 HTML 片段中,由于 box1 是绝对定位(层级为“Z-index: 0”级),而 box2 是文档流内块级盒(层级为“块级盒”级),因此 box1 会层叠在 box2 之上。下面添加如下 CSS 规则:

  1. #box2 { 
  2. opacity: .5

这时候, box2 则会层叠在 box1 之上了。因为 box2 的 opacity 为 0.5(小于 1),故视其为“Z-index: 0”级,也就和 box1 同级了。同级情况下,按照二者在源代码中的顺序,居后的 box2 又重新占领高地了。

读者可以取下面规则之任意一条实验,都能达到同样效果:

  1. #box2 { 
  2. transform: scale(1); 
  3. mix-blend-mode: difference; 
  4. isolation: isolate; 
  5. -webkit-filter: blur(5px); 

2.3 transform 改变绝对定位子元素包含块

transform 除了建立新的局部层叠上下文外,还会干一件事:改变绝对定位子元素的包含块。须注意的是,固定定位也是绝对定位的一种。

什么是包含块?有时候一些盒子根据矩形盒计算自身定位和大小,此矩形盒即包含块。更多详情请阅读视觉格式化模型详述。

固定定位元素

固定定位元素的包含块由视口创建(如果读者了解视觉格式化模型详述的信息,也就知道这一点:在计算其“静态位置”的时候,则以初始化包含块作为其计算包含块)。现在我们看以下源代码:

  1. div { 
  2. width: 100px; 
  3. height: 100px; 
  4. #fixed { 
  5. position: fixed; 
  6. width: 100%; 
  7. height: 100%; 
  8. top: 0
  9. left: 0
  10. background: blue; 
  11. #transform { 
  12. background: red; 
  13. padding: 20px; 
  14.  
  15. <body> 
  16. <div id="transform"
  17. <div id="fixed"></div> 
  18. </div> 
  19. </body> 

这个时候,以视口为包含块进行定位和大小计算, fixed 将会铺满整个屏幕。

但现在,我们加上如下规则:

  1. #transform { 
  2. transform: scale(1); 

此时,fixed 的包含块不再是视口,而是 transform 的内边距盒的边缘盒了。故此时 fixed 的宽高均为 140px。

绝对定位元素

我们举一个例子:

  1. #relative { 
  2. position: relative; 
  3. width: 100px; 
  4. height: 100px; 
  5. background: green; 
  6. #absolute { 
  7. position: absolute; 
  8. width: 100%; 
  9. height: 100%; 
  10. top: 0
  11. left: 0
  12. background: blue; 
  13. #transform { 
  14. background: red; 
  15. width: 50px; 
  16. height: 50px; 
  17.  
  18. <div id="relative"
  19. <div id="transform"
  20. <div id="absolute"></div> 
  21. </div> 
  22. </div> 

此时 absolute 的包含块为 relative 的内边距盒的边缘盒。由此 absolute 的宽高均为 100px。然后我们添加如下规则:

  1. #transform { 
  2. transform: scale(1); 

由于 transform 创建了局部层叠上下文,absolute 的包含块不再是 relative 而是 transform 了,根据这一新的包含块,得新宽和高为 50px。

责任编辑:chenqingxiang 来源: HaoyCn的博客
相关推荐

2017-05-11 14:00:02

Flask请求上下文应用上下文

2012-12-31 10:01:34

SELinuxSELinux安全

2021-01-26 05:19:56

语言Go Context

2012-07-18 11:39:18

ibmdw

2022-09-14 13:13:51

JavaScript上下文

2021-09-07 09:53:42

JavaScript变量提升

2012-09-13 09:24:31

CSSJSjQ

2023-07-11 10:02:23

2022-09-15 08:01:14

继承基础设施基础服务

2010-09-02 13:59:17

background-background-CSS3

2023-07-14 07:52:37

CSS优先级Design

2013-01-30 15:59:29

adobeCSS3HTML5

2022-04-24 15:37:26

LinuxCPU

2024-03-14 08:11:45

模型RoPELlama

2017-12-17 17:01:23

限界上下文系统模型

2022-10-28 16:24:33

Context上下文鸿蒙

2011-11-25 13:18:40

HTML 5

2010-08-27 09:19:32

CSS层叠继承

2021-07-26 07:47:36

Cpu上下文进程

2020-07-24 10:00:00

JavaScript执行上下文前端
点赞
收藏

51CTO技术栈公众号