See why a child with a high z-index can stay behind a sibling
Admin ·
Follow the stacking context around a child before raising the child's z-index. A parent context can keep all of its descendants behind a sibling, even if the child carries a larger number.
Compare the two layers in the original context
Select Reset example. The positioned parent has z-index auto and no transform. The child has z-index 9 and the sibling 2. The child appears on top in the overlap because it shares the outer stacking context with the sibling.
Set Child z-index to 2. With the competing values tied, the later sibling paints on top in this example. Restore the child to 9.
W3C CSS 2 stacking context painting orderW3C CSS Positioned Layout painting order and stacking contexts
Turn on a transform that changes only the context
Enable Transform parent. The sample uses translateX(0). It leaves the parent in place and creates a stacking context. The outer sibling at level 2 now paints above the parent's context at level 0.
Raise Child z-index to 999. The sibling remains on top. Set Parent z-index to 3 and the child becomes visible on top again because the entire parent context now paints above the sibling.
transform: translateX(0);W3C CSS Transforms rendering modelW3C CSS 2 stacking context painting order
Inspect the exported parent and sibling relationship
Download stacking-context.html and open it in a browser. Inspect which element is nested inside the parent and which is outside. Keep that structure when reproducing the example in another page.
If your own layout behaves differently, inspect the ancestors for rules that create stacking contexts. The visualizer illustrates a positioned parent and one optional transform in a fixed arrangement.
W3C CSS Positioned Layout painting order and stacking contextsW3C CSS Transforms rendering model
Common questions
What happens if I turn Transform parent off while Parent z-index is 3?
The positioned parent still creates a stacking context because it has a numeric z-index.
How can I return to shared stacking for this example?
Choose auto for Parent z-index and clear Transform parent. The child and sibling compete in the outer example context.