成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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請求上下文應用上下文

2024-04-28 08:31:47

CSS3Clamp()函數響應式設計工具

2024-04-26 08:27:15

JavaScriptCSSHTML元素

2024-05-31 00:00:01

2025-04-07 01:02:00

GoAPI語言

2012-09-13 09:24:31

CSSJSjQ

2022-09-14 13:13:51

JavaScript上下文

2021-09-07 09:53:42

JavaScript變量提升

2012-12-31 10:01:34

SELinuxSELinux安全

2013-01-30 15:59:29

adobeCSS3HTML5

2012-07-18 11:39:18

ibmdw

2010-09-02 13:59:17

background-background-CSS3

2021-01-26 05:19:56

語言Go Context

2023-07-14 07:52:37

CSS優先級Design

2022-09-15 08:01:14

繼承基礎設施基礎服務

2011-11-25 13:18:40

HTML 5

2010-08-27 09:19:32

CSS層疊繼承

2025-05-07 08:35:11

2023-07-11 10:02:23

2022-10-11 23:26:54

css3attr函數
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产精品18hdxxxⅹ在线 | 日韩一级免费看 | 国产精品免费福利 | 最新中文字幕在线 | 欧美精品一区在线发布 | 91精品免费视频 | 99这里只有精品视频 | 99福利视频 | 久久久久久免费免费 | 91在线网站| 黄免费观看视频 | 91文字幕巨乱亚洲香蕉 | 一区二区三区回区在观看免费视频 | 免费久久网 | 成人在线免费网站 | 国产精品久久久久久久免费观看 | 黑色丝袜三级在线播放 | 中文字幕av中文字幕 | 国产美女视频 | 中文字幕中文字幕 | 国产一区欧美 | 视频一区二区在线 | 久久久久成人精品免费播放动漫 | 亚洲人va欧美va人人爽 | 中文av网站 | 日本国产精品视频 | 丁香六月激情 | 欧美一区在线视频 | 中文字幕视频在线 | 久久伦理电影 | 美女视频一区 | 精品在线看 | 久久亚洲一区二区三区四区 | 精品一区二区三区在线观看 | 亚洲一页 | 午夜小视频在线观看 | 懂色中文一区二区在线播放 | 欧美美女爱爱视频 | 日韩成人在线免费观看 | 欧美精品一区二区三区在线四季 | 精品国产视频 |