當fixed元素相互嵌套時chrome下父元素會影響子項目的層疊關係

來源:互聯網
上載者:User

      問題:fixed元素被另一個fixed元素包含的時候在chrome下fixed子項目的定位會受到父元素的影響。

demo(http://jsbin.com/qumah/1):

<!DOCTYPE html><html><head>  <meta charset="utf-8">  <title>JS Bin</title>  <style>       .fixed {         position: fixed;         left:0;         top:20px;         width:200px;         height:200px;         background:#fee;       }       .fixed-child {         width:150px;         height:150px;         background: #fcc;         top:40px;         right: 20px;         left: auto;         z-index: 100;       }    .absolute {      position: absolute;      right: 20px;      width:100px;      height:100px;      background:#f11;    }    </style></head><body>  <div class="fixed">fixed position<div class="fixed fixed-child">fixed child position</div></div>  <div class="absolute">absolute position</div>  </body></html>

 

IE(IE8-IE11效果一致,IE7沒有測試,IE6不支援fixed元素)下效果:

 

chrome下效果:

 

firefox下效果:

Safari下效果:

 

    解釋:層疊關係是受層疊上下文影響的。文檔中的層疊上下文由滿足以下任意一個條件的元素形成:

  • 根項目 (HTML),
  • 絕對(absolute)定位或相對(relative)定位且z-index值不為"auto",
  • 元素的opacity屬性值小於1. (參考 the specification for opacity),
  • 在mobile WebKit 和 Chrome 22+核心的瀏覽器中, position: fixed 總是建立一個新的層疊上下文, 即使z-index的值是"auto" (See this post)

參考來源:The stacking context - MDN

 

每個層疊上下文都有如下的層疊層級組成(顯示順序從後到前):

參考來源:RM8015: IE6 IE7 IE8(Q) 中定位元素 'z-index' 為預設值在某些情況下會產生新的層疊上下文

 

 

      結論:在chrome22+的瀏覽器中,position為fixed總是會建立新的重疊上下文,所以子fixed元素在此時會以父fixed元素為層疊上下文,子項目的層疊關係會受到父元素的影響。而在非chrome瀏覽器下子fixed元素並不會建立新的層疊上下文,fixed元素的層疊上下文為最近的層疊上下文。

     因此,我們應該盡量避免出現fixed元素相互嵌套的問題。如果必須有嵌套的情況,建議修改fixed父元素的z-index值來修正在chrome下fixed子項目的層疊問題。

 

題外話:

   對於opacity小於1的元素也會產生層疊內容相關的問題,可能很多人都不知道。但是規範裡面是明文規定的:

Since an element with opacity less than 1 is composited from a single offscreen image, content outside of it cannot be layered in z-order between pieces of content inside of it. For the same reason, implementations must create a new stacking context for any element with opacity less than 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. See section 9.9 andAppendix E of [CSS21] for more information on stacking contexts. The rules in this paragraph do not apply to SVG elements, since SVG has its own rendering model ([SVG11], Chapter 3).          ---摘自《CSS Color Module Level 3》

 

簡單來說:

  • opacity值小於1的元素會建立新的層疊上下文
  • opacity值小於1的元素的層疊層級相當於z-index:0的定位元素。此時設定z-index會失效,除非該元素同時為定位元素
  • opacity值小於1的元素如果同時為定位元素時,則該元素將同時具有定位元素和opacity小於1元素的特性之和。換句話說,就是z-index等於“auto”時會被當成z-index等於“0”時建立新的層疊上下文

 

 

參考文章:

https://developer.mozilla.org/zh-CN/docs/Web/CSS/Understanding_z-index/The_stacking_context

http://www.w3.org/TR/CSS21/visuren.html#z-index

https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Understanding_z_index

RM8015: IE6 IE7 IE8(Q) 中定位元素 'z-index' 為預設值在某些情況下會產生新的層疊上下文

你需要瞭解的z-index世界

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.