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

層疊順序與堆疊上下文知多少

May 27, 2017 am 10:40 AM
ht

  層疊順序(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

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

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

熱門話題

Java教學
1664
14
CakePHP 教程
1423
52
Laravel 教程
1321
25
PHP教程
1269
29
C# 教程
1249
24
如何使用HTML,CSS和JavaScript創建動畫倒計時計時器 如何使用HTML,CSS和JavaScript創建動畫倒計時計時器 Apr 11, 2025 am 11:29 AM

您是否曾經在項目上需要一個倒計時計時器?對於這樣的東西,可以自然訪問插件,但實際上更多

HTML數據屬性指南 HTML數據屬性指南 Apr 11, 2025 am 11:50 AM

您想了解的有關HTML,CSS和JavaScript中數據屬性的所有信息。

當您看上去時,CSS梯度變得更好 當您看上去時,CSS梯度變得更好 Apr 11, 2025 am 09:16 AM

我關注的一件事是Lea Verou&#039; s conic-Gradient()Polyfill的功能列表是最後一項:

使Sass更快的概念證明 使Sass更快的概念證明 Apr 16, 2025 am 10:38 AM

在一個新項目開始時,Sass彙編發生在眼睛的眨眼中。感覺很棒,尤其是當它與browsersync配對時,它重新加載

靜態表單提供商的比較 靜態表單提供商的比較 Apr 16, 2025 am 11:20 AM

讓我們嘗試在這裡造成一個術語:“靜態表單提供商”。你帶上html

如何在WordPress主題中構建VUE組件 如何在WordPress主題中構建VUE組件 Apr 11, 2025 am 11:03 AM

內聯式模板指令使我們能夠將豐富的VUE組件構建為對現有WordPress標記的逐步增強。

php是A-OK用於模板 php是A-OK用於模板 Apr 11, 2025 am 11:04 AM

PHP模板通常會因促進Subpar代碼而變得不良說唱,但這並不是這樣的情況。讓我們看一下PHP項目如何執行基本的

三種代碼 三種代碼 Apr 11, 2025 pm 12:02 PM

每次啟動一個新項目時,我都會將我正在查看的代碼分為三種類型,或者如果您願意的話。我認為這些類型可以應用於

See all articles