Educational guide to z-index
How to tell which box paints in front
Follow the stacking context tree and see which levels the browser compares when two boxes overlap.
Mental model
Find the stacking context the boxes share. Compare the level of each branch in that context and use the painting phase and document order to resolve ties.
- Parent A
z-index: 1- Child A
z-index: 999
- Child A
- B
z-index: 2
Painting order among siblings
These positioned boxes share a context. Negative levels paint first, followed by level zero (including auto for these boxes), then positive levels. When levels and painting phases match, the box that comes later in the HTML paints in front.
Both set to auto
Zero and negative levels
Tied at 2
The child at level 999
B stays at level 2 in all five comparisons. The dashed border marks parent A and the child stays at level 999.
With position: relative and z-index: auto, the child participates in the shared context. With z-index: 0, the parent stays at level zero and creates its own context containing the child.
Child at 999 in parent at 1
Parent at 3
Parent set to auto
Parent at 0
Parent set to auto and isolated
Flex and grid items
An integer z-index also creates a stacking context on a flex or grid item with position: static. When level and painting phase match, HTML order decides the tie, with any changes made by order.
Flexbox with B in front
Grid with A in front
What creates a stacking context
When inspecting an overlap, check the computed styles of the ancestors too. These declarations create a stacking context on the boxes described.
position- An integer level on a positioned box creates a stacking context. Fixed and sticky boxes create one even with z-index: auto.
transform- Any transform value other than none creates a stacking context on a CSS box, including an identity transform such as translate(0).
opacity- An opacity value below 1 creates a stacking context and applies transparency to the box and its contents as a group.
isolation- isolation: isolate explicitly creates a stacking context. In the comparison, the parent creates one with position: relative and z-index: auto.
The browser’s top layer
The browser puts a dialog opened as a modal and a popover while it is shown in the top layer. This layer paints above the document’s content.
Use showModal() on a dialog when the interaction requires modality and use popover for non-modal content. Each API manages when the element enters and leaves the top layer.
Common pitfalls
Follow these checks from the box to the stacking context that decides the overlap.
The child at level 999 is covered
- Expected result
- Child A should paint in front of B.
- Cause
- The context containing the child is at level 1 and B participates in the shared context at level 2.
- Solution
- Raise the parent to level 3 if its whole branch should paint in front. If the parent is relative and its context comes solely from the integer level, try auto so the child participates in the shared context.
A static box seems to ignore its level
- Expected result
- The static box should paint in front when its z-index increases.
- Cause
- The box is in its container’s block flow with position: static.
- Solution
- Check the parent’s display. For a box in block flow, use position: relative to assign it a level and keep it in place.
Two boxes overlap at the same level
- Expected result
- A should paint in front of B.
- Cause
- A and B share a level and painting phase. B comes later in this scene’s HTML.
- Solution
- Give A a higher level in the same context. Keep the HTML order that reading and focus require.