CSS Stacking Contexts
How does the browser decide which element to render "on top" when elements overlap?
Default Behavior
- When all siblings are rendered in Flow layout, the DOM order controls how the background elements overlap, but the content will always float to the front.
- If one sibling uses positioned layout, it will appear above its non-positioned sibling, no matter what the DOM order is.
- If both siblings use positioned layout, the DOM order controls which element will be on top. Unlike in Flow layout, the content does not float to the front.
Using z-index
z-indexis a CSS property that allows you to control the order of elements in the stacking context.z-indexonly works with positioned elements.z-indexcan also be used with flex/grid children.- The
zinz-indexrefers to thezaxis in 3D space. - Default value of
z-indexisauto, which equates to0.
Negative z-indexes
z-index values must be integers, and they are allowed to be negative.
Even though it won't offer much benefit.
Stacking Contexts
z-index isn't a global value, it's a value applied to a stacking context.
An website is collection of stacking contexts. Stacking contexts can be contained in other stacking contexts, and together create a hierarchy of stacking contexts.
- Each stacking context is completely independent of 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.
Creating new contexts
Here are some declarations that create a new stacking context:
- Setting
opacityto a value less than1 - Setting
positiontoabsoluteorrelativeandz-indexvalue other than auto. - Setting
positiontofixedorsticky(Noz-indexneeded for these values!) - Applying a
mix-blend-modeother thannormal - Adding a
z-indexto a child inside a display:flexor display:gridcontainer - Using
transform,filter,clip-path, orperspective - Explicitly creating a context with
isolation: isolate
More info on MDN
Debugging stacking contexts
Here is a super awesome tool to help you debug stacking contexts: Stacking Contexts Inspector