搜尋
首頁web前端css教學層疊順序與堆疊上下文知多少

  層疊順序(stacking level)與堆疊上下文(stacking context)知多少?

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

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

<p class="container">
    <p class="inline-block">#pA display:inline-block</p>
    <p class="float"> #pB float:left</p>
</p>

  它們的CSS 定義如下:

.container{

    position:relative;

    background:#ddd;

}

.container > p{

    width:200px;

    height:200px;

}

.float{

    float:left;

    background-color:deeppink;

}

.inline-block{

    display:inline-block;

    background-color:yellowgreen;

    margin-left:-100px;

}

  大概描述起來,意思就是擁有共同父容器的兩個p重疊在一起,是display:inline-block 疊在上面,還是float:left 疊在上面?

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

<p class="container">
    <p class="float"> #pB float:left</p>
    <p class="inline-block">#pA display:inline-block</p>
</p>

  會發現,無論順序如何,總是 display:inline-block 的 p 疊在上方。

  Demo戳我。

<p class="container">
    <p class="inline-block">#pA inline-block</p>
    <p class="float"> #pB float:left</p>
</p>

<p class="container">
    <p class="float"> #pB float:left</p>
    <p class="inline-block">#pA inline-block</p>
</p>

  這裡其實是涉及了所謂的層疊水平(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: 的子堆疊上下文元素(正的越低越堆疊層級越低)

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

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

.container{

    position:relative;

    background:#ddd;

}

.container > p{

    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;

}

  Demo戳我。

  會看到,inline-block 的 p 不再一定疊在 float 的 p 之上,而是和 HTML 程式碼中 DOM 的堆疊順序有關,後來添加的 p 會 疊在先加入的 p 之上。

The key point here is that the added opacity:0.9 allows both p to generate a stacking context. the concept of. At this time, to stack the two, you need z-index. The higher the z-index, the higher the stacking level.

A stacking context is a three-dimensional concept of HTML elements that extend along an imaginary z-axis relative to the user-facing window (computer screen) or web page. HTML Elements occupy space in the stacking context in order of priority based on their own attributes.

So, how to trigger an element to form a stacking context? The method is as follows, excerpted from MDN:

  • Root element (HTML ),

  • Absolute/relative positioning where z-index value is not "auto",

  • A flex item with a z-index value other than "auto", that is, the parent element display: flex|inline-flex,

  • ## Elements whose opacity attribute value is less than 1 (refer to the specification for opacity),

  •  Transform attribute value is not "none" elements,

  •  mix-blend-mode attribute value is not "normal" elements ,

  • The element whose filter value is not "none",

  • ## The perspective value is not Elements whose isolation attribute is set to "isolate",

  • # Position: fixed

  • Specify any CSS properties in will-change, even if you do not directly specify the values ​​​​of these properties

  •  -webkit-overflow-scrolling The element whose attribute is set to "touch"

  • So, above we give The purpose of adding the opacity attribute to the two p is to form a stacking context. That is to say add opacity Replacing the properties listed above can achieve the same effect.

  • In a cascading context, its child elements are also cascaded according to the rules explained above. It is particularly worth mentioning that the z-index of its child elements The value is only meaningful in the context of the parent cascade. This means that if the z-index of the parent element is lower than another sibling element of the parent element, it will be useless no matter how high the z-index of the child element is.

Understanding the above stacking-level and stacking-context is the key to understanding the stacking order of CSS.

以上是層疊順序與堆疊上下文知多少的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
有點提醒您偽元素是孩子,有點。有點提醒您偽元素是孩子,有點。Apr 19, 2025 am 11:39 AM

這裡有一個帶子元素的容器:

菜單具有'動態命中區域”菜單具有'動態命中區域”Apr 19, 2025 am 11:37 AM

飛翔的菜單!您需要在第二個菜單中實現懸停事件以顯示更多菜單項的菜單,即您在棘手的領域中。一方面,他們應該

通過Webvtt改善視頻可訪問性通過Webvtt改善視頻可訪問性Apr 19, 2025 am 11:27 AM

“網絡的力量在於其普遍性。每個人的訪問無論殘疾是一個重要方面。” - 蒂姆·伯納斯 - 李

每周平台新聞:CSS :: Marker偽元素,預先渲染的Web組件,向您的網站添加Webmention每周平台新聞:CSS :: Marker偽元素,預先渲染的Web組件,向您的網站添加WebmentionApr 19, 2025 am 11:25 AM

在本週的綜述中:datepickers正在讓鍵盤用戶頭痛,一個新的Web組件編譯器,有助於與Fouc進行戰鬥,我們終於獲得了造型列表項目標記,以及在您的網站上獲得網絡攻擊的四個步驟。

使寬度和靈活的物品一起玩得很好使寬度和靈活的物品一起玩得很好Apr 19, 2025 am 11:23 AM

簡短的答案:您可能要尋找的是彈性折射和彈性基礎。

位置粘性和桌子標頭位置粘性和桌子標頭Apr 19, 2025 am 11:21 AM

您可以位置:粘性;一個

每周平台新聞:HTML在搜索控制台,全局腳本範圍中的HTML檢查,Babel Envs添加默認查詢查詢每周平台新聞:HTML在搜索控制台,全局腳本範圍中的HTML檢查,Babel Envs添加默認查詢查詢Apr 19, 2025 am 11:18 AM

在本週的Web平台新聞世界中,Google搜索控制台可以更輕鬆地查看爬行的標記,我們了解到自定義屬性

Indieweb和網絡企業Indieweb和網絡企業Apr 19, 2025 am 11:16 AM

Indieweb是一回事!他們將舉行會議和一切。紐約客甚至在寫這件事:

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中