62

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.

Are there other properties that apply a stacking context?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
jotavejv
  • 1,936
  • 2
  • 16
  • 16
  • 1
    `position: fixed` can create a stacking context on Chrom(e|ium)! There's a flag for it specifically in `chrome://flags`. – Ry- Apr 22 '13 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 '13 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. – Ry- Apr 22 '13 at 13:19
  • @minitech Oh interesting. `=]` Will check it out. – Fabrício Matté Apr 22 '13 at 13:19
  • @minitech I wasn't aware of the fixed position chrome flag, thanks – jotavejv Apr 22 '13 at 13:26
  • related: https://stackoverflow.com/a/52937920/8620333 some properties will also create containing block for absolute and fixed element in addition to the creation of a stacking context. – Temani Afif Feb 06 '21 at 09:29

1 Answers1

85

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 auto on an element that is positioned (i.e. an element with position 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 article and this set of CSSWG telecon minutes.

    • Another exception to this is with a flex item and a grid item. Setting z-index 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 whose content is anything other than normal.

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

  • In filter effects, setting filter to anything other than none.

  • In compositing and blending, setting isolation to isolate and setting mix-blend-mode to a value different from normal

  • In will change, setting will-change to a property whose any non-initial value would create a stacking context.

  • In masking, setting clip-path/mask with a value other than none.

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.

Temani Afif
  • 245,468
  • 26
  • 309
  • 415
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • Oh, it wasn't a flag in Chrome 22? I suppose that's why it got moved, then. – Ry- Apr 22 '13 at 13:33
  • @minitech From what I've read, it seems like the flag was introduced so you can disable the new `position:fixed` behavior introduced in Chrome 22 for back-compat. – Fabrício Matté Apr 22 '13 at 13:34
  • @minitech Also from the linked article: "To test if your page is going to change, go to Chrome's `about:flags` and 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 '13 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 '13 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 '13 at 14:25
  • @Fabrício Matté: I don't think there are any practical applications, but it is worth keeping in mind when dealing with edge cases as stacking context behavior mostly affects positioned descendants. – BoltClock Apr 22 '13 at 14:28
  • Does the position 'sticky' auto create a stacking context as well? – denis Jan 01 '14 at 12:15
  • @and-dev: Yes according to the rules given, because it's a value other than `static`. Whether it should do so only for `z-index` other than `auto` or for all values is unclear - that'll depend on how it eventually gets implemented. – BoltClock Jan 01 '14 at 12:18
  • Thank you for the clarification. "Setting z-index to anything other than 1" - Does this mean that a positioned element with z-index:1 does not create a stacking context? – denis Jan 01 '14 at 14:37
  • @and-dev: Oh, that's a mistake in my answer. It should be auto, not 1. – BoltClock Jan 02 '14 at 03:43
  • @BoltClock Added [CSS filter effects](http://stackoverflow.com/questions/25764404/why-does-stacking-order-change-on-webkit-filter-hover/25764603#25764603) to the list – Hashem Qolami Sep 13 '14 at 22:54
  • @Hashem Qolami: Looks good, thanks. No need to notify me in a comment as I will be notified of edits :) – BoltClock Sep 14 '14 at 00:03
  • 2
    ```-webkit-overflow-scrolling: touch``` [creates a new stacking context as well](https://developer.mozilla.org/en-US/docs/Web/CSS/-webkit-overflow-scrolling). – Artem Andreev Apr 15 '15 at 08:21