The stacking context

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


http://stackoverflow.com/questions/16148007/which-css-properties-create-a-stacking-context


The stacking context

In the previous example, Adding z-index, the rendering order of certain DIVs is influenced by their z-index value. This occurs because these DIVs have special properties which cause them to form a stacking context.

A stacking context is formed, anywhere in the document, by any element which is either

  • the root element (HTML),
  • positioned (absolutely or relatively) with a z-index value other than "auto",
  • elements with an opacity value less than 1. (See the specification for opacity),
  • on mobile WebKit and Chrome 22+, position: fixed always creates a new stacking context, even when z-index is "auto" (Seethis post)

Within a stacking context, child elements are stacked according to the same rules previously explained. Importantly, the z-index values of its child stacking contexts only have meaning in this parent. Stacking contexts are treated atomically as a single unit in the parent stacking context.

In summary:

  • Positioning and assigning a z-index value to an HTML element creates a stacking context, (as does assigning non-full opacity).
  • Stacking contexts can be contained in other stacking contexts, and together create a hierarchy of stacking contexts.
  • Each stacking context is completly independent from its siblings: only descendant elements are considered when stacking is processed.
  • Each stacking context is self-contained: after the element's contents are stacked, the whole element is considered in the stacking order of the parent stacking context.
Note: The hierarchy of stacking contexts is a subset of the hierarchy of HTML elements, because only certain elements create stacking contexts. We can say that elements that do not create their own stacking contexts are  assimilated by the parent stacking context.

The example

Example of stacking rules modified using z-index

In this example every positioned element creates its own stacking context, because of their positioning and z-index values. The hierarchy of stacking contexts is organized as follows:

  • Root
    • DIV #1
    • DIV #2
    • DIV #3
      • DIV #4
      • DIV #5
      • DIV #6

It is important to note that DIV #4, DIV #5 and DIV #6 are children of DIV #3, so stacking of those elements is completely resolved within DIV#3. Once stacking and rendering within DIV #3 is completed, the whole DIV #3 element is passed for stacking in the root element with respect to its sibling's DIV.

Notes:

  • DIV #4 is rendered under DIV #1 because DIV #1's z-index (5) is valid within the stacking context of the root element, while DIV #4's z-index (6) is valid within the stacking context of DIV #3. So, DIV #4 is under DIV #1, because DIV #4 belongs to DIV #3, which has a lower z-index value.
  • For the same reason DIV #2 (z-index 2) is rendered under DIV#5 (z-index 1) because DIV #5 belongs to DIV #3, which has an higher z-index value.
  • DIV #3's z-index is 4, but this value is independent from z-index of DIV #4, DIV #5 and DIV #6, because it belongs to a different stacking context.
  • An easy way to figure out the rendering order of stacked elements along the Z axis is to think of it as a "version number" of sorts, where child elements are minor version numbers underneath their parent's major version numbers. This way we can easily see how an element with a z-index of 1 (DIV #5) is stacked above an element with a z-index of 2 (DIV #2), and how an element with a z-index of 6 (DIV #4) is stacked below an element with a z-index of 5 (DIV #1). In our example (sorted according to the final rendering order):
    • Root
      • DIV #2 - z-index is 2
      • DIV #3 - z-index is 4
        • DIV #5 - z-index is 1, stacked under an element with a z-index of 4, which results in a rendering order of 4.1
        • DIV #6 - z-index is 3, stacked under an element with a z-index of 4, which results in a rendering order of 4.3
        • DIV #4 - z-index is 6, stacked under an element with a z-index of 4, which results in a rendering order of 4.6
      • DIV #1 - z-index is 5

Example Source Code

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  <head>

    <title>Understanding CSS z-index: The Stacking Context: Example Source</title>

    <style type="text/css">
      * {
        margin: 0;
        }
      html {
        padding: 20px;
        font: 12px/20px Arial, sans-serif;
        }
      div {
        opacity: 0.7;
        position: relative;
        }
      h1 {
        font: inherit;
        font-weight: bold;
        }
      #div1, #div2 {
        border: 1px dashed #696;
        padding: 10px;
        background-color: #cfc;
        }
      #div1 {
        z-index: 5;
        margin-bottom: 190px;
        }
      #div2 {
        z-index: 2;
        }
      #div3 {
        z-index: 4;
        opacity: 1;
        position: absolute;
        top: 40px;
        left: 180px;
        width: 330px;
        border: 1px dashed #900;
        background-color: #fdd;
        padding: 40px 20px 20px;
        }
      #div4, #div5 {
        border: 1px dashed #996;
        background-color: #ffc;
        }
      #div4 {
        z-index: 6;
        margin-bottom: 15px;
        padding: 25px 10px 5px;
        }
      #div5 {
        z-index: 1;
        margin-top: 15px;
        padding: 5px 10px;
        }
      #div6 {
        z-index: 3;
        position: absolute;
        top: 20px;
        left: 180px;
        width: 150px;
        height: 125px;
        border: 1px dashed #009;
        padding-top: 125px;
        background-color: #ddf;
        text-align: center;
        }
    </style>

  </head>
  <body>

    <div id="div1">
      <h1>Division Element #1</h1>
      <code>position: relative;<br/>
      z-index: 5;</code>
    </div>

    <div id="div2">
      <h1>Division Element #2</h1>
      <code>position: relative;<br/>
      z-index: 2;</code>
    </div>

    <div id="div3">

      <div id="div4">
        <h1>Division Element #4</h1>
        <code>position: relative;<br/>
        z-index: 6;</code>
      </div>

      <h1>Division Element #3</h1>
      <code>position: absolute;<br/>
      z-index: 4;</code>

      <div id="div5">
        <h1>Division Element #5</h1>
        <code>position: relative;<br/>
        z-index: 1;</code>
      </div>
   
      <div id="div6">
        <h1>Division Element #6</h1>
        <code>position: absolute;<br/>
        z-index: 3;</code>
      </div>

    </div>

  </body>
</html>
  
  

Division Element #1

position: relative;
z-index: 5;
  
  

Division Element #2

position: relative;
z-index: 2;
   
   

Division Element #3

position: absolute;
z-index: 4;
   
   

Division Element #4

position: relative;
z-index: 6;
   
   

Division Element #5

position: relative;
z-index: 1;
   
   

Division Element #6

position: absolute;
z-index: 3;
   
   

See also

Original Document Information






I'm studying about stacking contexts and doing some tests with the properties that create a stacking context.

I did several tests and found that in addition to z-index, of course, the following properties also create a stacking context:

  • transform other than none;
  • opacity other than 1;
  • And perspective.

Does anyone know any other properties that apply a stacking context?

share | improve this question
 
 
position: fixed can create a stacking context on Chrom(e|ium)! There's a flag for it specifically inchrome://flags. –   minitech  Apr 22 at 13:17
 
@minitech Yes, z-index requires a position other than static to create a stacking context. I believe OP is aware of that. Or do do mean that position:fixed creates a stacking context on its own without z-index?–   Fabrício Matté  Apr 22 at 13:17 
 
@FabrícioMatté: Yes, the flag states that all fixed-position elements create a stacking context. I don't know whether it's a bugfix. –   minitech  Apr 22 at 13:19 
 
@minitech Oh interesting. =] Will check it out. –   Fabrício Matté  Apr 22 at 13:19
 
@minitech I wasn't aware of the fixed position chrome flag, thanks –   jotavejv  Apr 22 at 13:26

1 Answer

up vote 14 down vote accepted
+500

One or more of the following scenarios will cause an element to establish its own stacking context1 for its descendants:

  • The root element always holds a root stacking context. This is why you can start arranging elements without having to position the root element first. Any element that doesn't already participate in a local stacking context (generated by any of the other scenarios below) will participate in the root stacking context instead.

  • Setting z-index to anything other than 1 on an element that is positioned (i.e. an element withposition that isn't static).

    • Note that this behavior is slated to be changed for elements with position: fixed such that they will always establish stacking contexts regardless of their z-index value. Some browsers have begun to adopt this behavior, however the change has not been reflected in either CSS2.1 or the new CSS Positioned Layout Module yet, so it may not be wise to rely on this behavior for now.

      This change in behavior is explored in another answer of mine, which in turn links to this articleand this set of CSSWG telecon minutes.

    • Another exception to this is with a flex item. Setting z-index on a flex item will always cause it to establish a stacking context even if it isn't positioned.

  • Setting opacity to anything less than 1.

  • Transforming the element:

  • Creating a CSS region: setting flow-from to anything other than none on an element whosecontent is anything other than normal.

  • In paged media, each page-margin box establishes its own stacking context.

Note that a block formatting context is not the same as a stacking context; in fact, they are two completely independent (although not mutually exclusive) concepts.


1 This does not include pseudo-stacking contexts, an informal term that simply refers to things that behave like independent stacking contexts with respect to positioning, but actually participate in their parent stacking contexts.

share | improve this answer
 
 
Oh, it wasn't a flag in Chrome 22? I suppose that's why it got moved, then. –   minitech  Apr 22 at 13:33
 
@minitech From what I've read, it seems like the flag was introduced so you can disable the newposition:fixed behavior introduced in Chrome 22 for back-compat. –   Fabrício Matté  Apr 22 at 13:34 
 
@minitech Also from the linked article: "To test if your page is going to change, go to Chrome's about:flagsand turn on/off "fixed position elements create stacking contexts". If your layout behaves the same in both cases, you're set. If not, make sure it looks acceptable to you with that flag enabled, as that will be the default in Chrome 22. " –   Fabrício Matté  Apr 22 at 13:39
 
@minitech: I revamped the answer and added a little footnote on pseudo-stacking contexts based on your deleted answer, although my answer doesn't actually include them as they may be confusing. –   BoltClock Apr 22 at 14:10 
 
Very throughout answer, +1. But I'm wondering now, about those pseudo-stacking contexts implicitly created by inline-block and inline-table elements, do they have any practical application or is it something not worth caring about? –   Fabrício Matté  Apr 22 at 14:25 
show 1 more comment

I'm studying about stacking contexts and doing some tests with the properties that create a stacking context.

I did several tests and found that in addition to z-index, of course, the following properties also create a stacking context:

  • transform other than none;
  • opacity other than 1;
  • And perspective.

Does anyone know any other properties that apply a stacking context?

share | improve this question
 
 
position: fixed can create a stacking context on Chrom(e|ium)! There's a flag for it specifically inchrome://flags. –   minitech  Apr 22 at 13:17
 
@minitech Yes, z-index requires a position other than static to create a stacking context. I believe OP is aware of that. Or do do mean that position:fixed creates a stacking context on its own without z-index?–   Fabrício Matté  Apr 22 at 13:17 
 
@FabrícioMatté: Yes, the flag states that all fixed-position elements create a stacking context. I don't know whether it's a bugfix. –   minitech  Apr 22 at 13:19 
 
@minitech Oh interesting. =] Will check it out. –   Fabrício Matté  Apr 22 at 13:19
 
@minitech I wasn't aware of the fixed position chrome flag, thanks –   jotavejv  Apr 22 at 13:26

1 Answer

up vote 14 down vote accepted
+500

One or more of the following scenarios will cause an element to establish its own stacking context1 for its descendants:

  • The root element always holds a root stacking context. This is why you can start arranging elements without having to position the root element first. Any element that doesn't already participate in a local stacking context (generated by any of the other scenarios below) will participate in the root stacking context instead.

  • Setting z-index to anything other than 1 on an element that is positioned (i.e. an element withposition that isn't static).

    • Note that this behavior is slated to be changed for elements with position: fixed such that they will always establish stacking contexts regardless of their z-index value. Some browsers have begun to adopt this behavior, however the change has not been reflected in either CSS2.1 or the new CSS Positioned Layout Module yet, so it may not be wise to rely on this behavior for now.

      This change in behavior is explored in another answer of mine, which in turn links to this articleand this set of CSSWG telecon minutes.

    • Another exception to this is with a flex item. Setting z-index on a flex item will always cause it to establish a stacking context even if it isn't positioned.

  • Setting opacity to anything less than 1.

  • Transforming the element:

  • Creating a CSS region: setting flow-from to anything other than none on an element whosecontent is anything other than normal.

  • In paged media, each page-margin box establishes its own stacking context.

Note that a block formatting context is not the same as a stacking context; in fact, they are two completely independent (although not mutually exclusive) concepts.


1 This does not include pseudo-stacking contexts, an informal term that simply refers to things that behave like independent stacking contexts with respect to positioning, but actually participate in their parent stacking contexts.

share | improve this answer
 
 
Oh, it wasn't a flag in Chrome 22? I suppose that's why it got moved, then. –   minitech  Apr 22 at 13:33
 
@minitech From what I've read, it seems like the flag was introduced so you can disable the newposition:fixed behavior introduced in Chrome 22 for back-compat. –   Fabrício Matté  Apr 22 at 13:34 
 
@minitech Also from the linked article: "To test if your page is going to change, go to Chrome's about:flagsand turn on/off "fixed position elements create stacking contexts". If your layout behaves the same in both cases, you're set. If not, make sure it looks acceptable to you with that flag enabled, as that will be the default in Chrome 22. " –   Fabrício Matté  Apr 22 at 13:39
 
@minitech: I revamped the answer and added a little footnote on pseudo-stacking contexts based on your deleted answer, although my answer doesn't actually include them as they may be confusing. –   BoltClock Apr 22 at 14:10 
 
Very throughout answer, +1. But I'm wondering now, about those pseudo-stacking contexts implicitly created by inline-block and inline-table elements, do they have any practical application or is it something not worth caring about? –   Fabrício Matté  Apr 22 at 14:25 
show 1 more comment
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值