1. 程式人生 > >疊層順序(stacking level)和堆疊上下文(stacking context)

疊層順序(stacking level)和堆疊上下文(stacking context)

z-index 看上去其實很簡單,根據 z-index 的高低決定層疊的優先順序,實則深入進去,會發現內有乾坤。

看看下面這題,定義兩個 div A 和 B,被包括在同一個父 div 標籤下。HTML結構如下:

<div class="container">
    <div class="inline-block">#divA display:inline-block</div>
    <div class="float"> #divB float:left</div>
</div>

它們的 CSS 定義如下:

.container
{ position:relative; background:#ddd; } .container > div{ width:200px; height:200px; } .float{ float:left; background-color:deeppink; } .inline-block{ display:inline-block; background-color:yellowgreen; margin-left:-100px; }

大概描述起來,意思就是擁有共同父容器的兩個 DIV 重疊在一起,是 display:inline-block

 疊在上面,還是 float:left 疊在上面?

注意這裡 DOM 的順序,是先生成 display:inline-block ,再生成 float:left 。當然也可以把兩個的 DOM 順序調轉如下:

<div class="container">
    <div class="float"> #divB float:left</div>
    <div class="inline-block">#divA display:inline-block</div>
</div>

會發現,無論順序如何,始終是 display:inline-block

 的 div 疊在上方。

這裡其實是涉及了所謂的層疊水平(stacking level),有一張圖可以很好的詮釋:

stacking level

運用上圖的邏輯,上面的題目就迎刃而解,inline-blcok 的 stacking level 比之 float 要高,所以無論 DOM 的先後順序都堆疊在上面。

不過上面圖示的說法有一些不準確,按照 W3官方 的說法,準確的 7 層為:

1、the background and borders of the element forming the stacking context.

2、the child stacking contexts with negative stack levels (most negative first).

3、the in-flow, non-inline-level, non-positioned descendants.

4、the non-positioned floats.

5、the in-flow, inline-level, non-positioned descendants, including inline tables and inline blocks.

6、the child stacking contexts with stack level 0 and the positioned descendants with stack level 0.

7、the child stacking contexts with positive stack levels (least positive first).

稍微翻譯一下:

1、形成堆疊上下文環境的元素的背景與邊框

2、擁有負 z-index 的子堆疊上下文元素 (負的越高越堆疊層級越低)

3、正常流式佈局,非 inline-block,無 position 定位(static除外)的子元素

4、無 position 定位(static除外)的 float 浮動元素

5、正常流式佈局, inline-block元素,無 position 定位(static除外)的子元素(包括 display:table 和 display:inline )

6、擁有 z-index:0 的子堆疊上下文元素

7、擁有正 z-index: 的子堆疊上下文元素(正的越低越堆疊層級越低)

所以我們的兩個 div 的比較是基於上面所列出來的 4 和 5 。5 的 stacking level 更高,所以疊得更高。

不過!不過!不過!重點來了,請注意,上面的比較是基於兩個 div 都沒有形成 堆疊上下文 這個為基礎的。下面我們修改一下題目,給兩個 div ,增加一個 opacity:

.container{
    position:relative;
    background:#ddd;
}
.container > div{
    width:200px;
    height:200px;
    opacity:0.9; // 注意這裡,增加一個 opacity
}
.float{
    float:left;
    background-color:deeppink;
}
.inline-block{
    display:inline-block;
    background-color:yellowgreen;
    margin-left:-100px;
}

會看到,inline-block 的 div 不再一定疊在 float 的 div 之上,而是和 HTML 程式碼中 DOM 的堆放順序有關,後新增的 div 會 疊在先新增的 div 之上。

這裡的關鍵點在於,新增的 opacity:0.9 這個讓兩個 div 都生成了 stacking context(堆疊上下文) 的概念。此時,要對兩者進行層疊排列,就需要 z-index ,z-index 越高的層疊層級越高。

堆疊上下文是HTML元素的三維概念,這些HTML元素在一條假想的相對於面向(電腦螢幕的)視窗或者網頁的使用者的 z 軸上延伸,HTML 元素依據其自身屬性按照優先順序順序佔用層疊上下文的空間。

那麼,如何觸發一個元素形成 堆疊上下文 ?方法如下,摘自 MDN

  • 根元素 (HTML),
  • z-index 值不為 "auto"的 絕對/相對定位,
  • 一個 z-index 值不為 "auto"的 flex 專案 (flex item),即:父元素 display: flex|inline-flex,
  • opacity 屬性值小於 1 的元素(參考 the specification for opacity),
  • transform 屬性值不為 "none"的元素,
  • mix-blend-mode 屬性值不為 "normal"的元素,
  • filter值不為“none”的元素,
  • perspective值不為“none”的元素,
  • isolation 屬性被設定為 "isolate"的元素,
  • position: fixed
  • 在 will-change 中指定了任意 CSS 屬性,即便你沒有直接指定這些屬性的值
  • -webkit-overflow-scrolling 屬性被設定 "touch"的元素

所以,上面我們給兩個 div 新增 opacity 屬性的目的就是為了形成 stacking context。也就是說新增 opacity 替換成上面列出來這些屬性都是可以達到同樣的效果。

在層疊上下文中,其子元素同樣也按照上面解釋的規則進行層疊。 特別值得一提的是,其子元素的 z-index 值只在父級層疊上下文中有意義。意思就是父元素的 z-index 低於父元素另一個同級元素,子元素 z-index 再高也沒用。

理解上面的 stacking-level 與 stacking-context 是理解 CSS 的層疊順序的關鍵。