CSS Stacking Context 里那些不为人知的坑

一般咱们在学习/了解CSS的时候,并不会直接接触/了解到stacking context的规则,甚至在初学的时,仅仅接触到z-index、知道能够经过z-index控制元素显示的先后顺序,却经常由此碰到各式各样匪夷所思的bug,这两天我也趟了一次z-index浑水,搞明白了z-index扯出的这一系列stacking context坑。css

什么是z-index

在W3C document里对z-index的解释是:html

The z-index attribute lets you adjust the order of the layering of objects when rendering content.
It means that CSS style rules allow you to position boxes on layers in addition to the normal rendering layer (layer 0). The Z position of each layer is expressed as an integer representing the stacking order for rendering. Greater numbers mean closer to the observer.css3

简言之咱们经过z-index在layer 0上控制「已定位」元素的前后顺序,其值越大元素越靠近用户。#这里layer 0指代root节点即html元素web

什么是Stacking Context

显然仅仅知道z-index的做用并不能解释现实中诸多怪异的CSS z-index定位表现,如同上一节中提到指代root节点的layer 0,在CSS中也可构造出与其类似的结构,这种提供z-index栈空间特性并影响子元素渲染顺序的结构,咱们称之为stacking context。chrome

怎样的CSS属性可使element造成一个Stacking Context

知足下面规则的元素将会构造出一个 Stacking Context 结构:express

  • root元素(html)
  • 「已定位」元素(position: absolute or relative)且 指定z-index值非auto的元素
  • flex item且指定z-index值非auto的元素
  • opacity小于1的元素
  • 指定transform值非none的元素
  • 指定mix-blend-mode值非normal的元素
  • 指定filter值非none的元素
  • 指定isolation值为isolate的元素
  • ==特例 mobile webkit & chrome 22+, 指定position: fixed的元素==
  • 在will-change属性上指定值为上述列表任意属性的元素
  • 指定-webkit-overflow-scrolling值为touch的元素

*注意除了前两条以外有如此多知足建立stacking context的条件,这也是形成诸多bug的源泉,好比opacity<1

浏览器

Stacking Context有什么特性

  1. stacking context能够嵌套
  2. 每一个stacking context相对于兄弟元素是彻底独立的,其内部规则不会影响到外部
  3. 每一个stacking context元素都会被父stacking context当作一个元素施加stacking规则

对于一个stacking context内部的元素,若是这个元素没有造成stacking context,其z-index值是auto(但其实若是这个元素没有造成stacking context,z-index属性对这个元素的表现根本没有意义,咱们能够理解为这个元素和其parent stacking context是一体的)。less

咱们经过给已定位元素(position: absolute or relative)指定z-index值以改变元素在其parent stacking context中Z轴的「相对偏移」量。这里的「相对偏移」指的是以parent stacking context为基准,相对于其它兄弟元素距离用户远近的顺序。async

因为造成stacking context的元素其z-index属性并不对内部元素产生影响,所以其子元素以其(parent stacking context)为z-index相对基准点即z-index: auto,这些子元素的stacking context兄弟元素按照下面的远近顺序展现在屏幕:ide

远 ---------> 近
parent stacking context >> z-index < 0 >> 非stacking context元素(z-index: auto) >> z-index >= 0

*注意在stacking context中的元素不会远于parent stacking context

非「定位」元素Stacking Context的特殊规则

先上一段极其容易产生困惑的代码(不支持embedded scripts略卵疼):

See the Pen yNJRKX by abruzzi (@abruzzi) on CodePen.

<script async src="//assets.codepen.io/assets/embed/ei.js"></script>
http://codepen.io/abruzzi/pen/yNJRKX

如上文说起,这里box3和box5经过opacity: .99建立了一个stacking context,但box3却覆盖在了box4之上,翻查W3C文档并无这样一条规则定义stacking context元素默认在非stacking context元素之上;咱们修改了box5的z-index属性为负值,box5依然在box6之上;咱们又经过position: relative & z-index: -1建立了box7的stacking context,其表现倒是正常的(话说回来,z-index: auto自己也没法造成stacking context,所以根本没有存在相似「stacking context元素默认在非stacking context元素之上」定义的意义)

在W3C文档的某个角落,咱们能够究其原委:

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’.

简言之,若是一个元素不是经过「定位」(position: absolute or relative)实现了stacking context,它将会以z-index: 0(高于auto)被看待,所以不管如何更改非「定位」元素的z-index都是无效的。

虽然文档中只提到opacity less 1构成的stacking context被看作z-index: 0,但经过测试,能够发现其余非「定位」方式建立的stacking context拥有与opacity less 1一致的表现。

以上即是我对CSS stacking context中形成诸多匪夷所思现象缘由的总结。


*注:下文内容与题目无关,仅为做者思路延展

什么是GraphicLayer

浏览器在解析渲染document时,内部的处理并未直接暴露给咱们,其中很重要的结构就是Layers。在这些未暴露的步骤中,RenderLayers负责渲染整个DOM子树结构,GraphicLayers则负责渲染RenderLayers中的子树,GraphicLayer把所负责的子树做为textures(能够理解为内存间移动的位图)upload到GPU,以实现高速绘制,也于是能够大幅优化这些属性带来的动画效果。

包括opacity在内,这些可产生stacking context的属性和建立特殊GraphicLayers的属性(opacity, filter, transfrom3d等)都很类似,但某些属性却又没法吻合(例如2d transfrom),冥冥之中总以为二者原理上有类似之处,能力有限,只好留一坑。

Fixed定位脱离Viewport的bug

在定位中有一个跟stacking content无关但又较为相近的危险Bug,注意下面代码中.inner的定位:

See the Pen VLKeZP by abruzzi (@abruzzi) on CodePen.

<script async src="//assets.codepen.io/assets/embed/ei.js"></script>
http://codepen.io/abruzzi/pen/VLKeZP

对于声明transfrom值非none元素,其子元素中若存在position: fixed将以声明transform的最近祖先做为基准而定位,这是由于transfrom值非none的元素定义了一个局部坐标系统,致使postion: fixed以此坐标系统计算布局。
目前这个bug仍未被解决,官方建议避免在transform元素下作fixed定位。


by Abruzzi's blog

相关文章
相关标签/搜索