Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which CSS properties create a stacking context?

Tags:

css

z-index

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?

like image 385
jotavejv Avatar asked Apr 22 '13 13:04

jotavejv


People also ask

Which CSS property allows you to control the stacking order of elements?

The z-index property specifies the stack order of an element. An element with greater stack order is always in front of an element with a lower stack order.

Does position relative create a stacking context?

TLDR; elements with position: relative do not create a new stacking context, but get drawn on top of non-positioned elements (i.e. elements with position: static ) either way because they are positioned elements.

Which CSS property will you use to decide the stacking order element coming below or above another element of the elements you have in your webpage?

The z-index property in CSS controls the vertical stacking order of elements that overlap.

What property determines the stacking order for layered elements?

The z-index property determines the stack level of an HTML element. The “stack level” refers to the element's position on the Z axis (as opposed to the X axis or Y axis). A higher value means the element will be closer to the top of the stacking order. This stacking order runs perpendicular to the display, or viewport.


1 Answers

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:

    • Setting transform to anything other than none.

    • Setting transform-style to preserve-3d.

    • Setting perspective to anything other than none.

  • 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.


1This 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.

like image 140
BoltClock Avatar answered Oct 18 '22 10:10

BoltClock