Sub topic 10
Stacking contexts and why z-index sometimes fails
This chapter assumes you know the box model, CSS selectors, and the position values (static, relative, absolute, fixed, sticky). You should also know that z-index controls which overlapping element is painted on top.
The working model
A stacking context is an isolated group of elements that are painted together. Inside a group, z-index values compete only with other members of the same group. The group as a whole then takes one place in its parent's stacking order.
The root <html> element creates the first stacking context. Other elements create new ones when they match certain rules.
This is why a very large z-index can still lose. If an element sits inside a stacking context that is painted below another one, no value inside it can lift it above that other context.
What creates a stacking context
Common triggers:
- A positioned element (
relative,absolute) with az-indexother thanauto. - A
fixedorstickyelement. These always create one. - A flex or grid child with a
z-indexother thanauto, even if it is not positioned. - An element with
opacityless than 1. - An element with
transform,filter,perspective, orclip-pathset to a value other thannone. - An element with
isolation: isolate. - An element with
will-changeset to a property that would create a context.
Note that position: relative alone, with z-index: auto, does not create a stacking context.
A example where z-index fails
Here the tooltip has z-index: 9999, but it still appears under the sidebar's content.
<div class="header">
<div class="tooltip">Tooltip</div>
</div>
<div class="sidebar">Sidebar</div>
<style>
.header {
position: relative;
z-index: 1;
height: 60px;
background: lightgray;
}
.tooltip {
position: absolute;
top: 40px;
left: 20px;
z-index: 9999;
padding: 0.5rem;
background: gold;
}
.sidebar {
position: relative;
z-index: 2;
margin-top: -20px;
height: 120px;
background: steelblue;
}
</style>The header has position: relative and z-index: 1, so it forms a stacking context at level 1 in the root. The sidebar forms one at level 2. The tooltip lives inside the header's context. Its 9999 only ranks it among the header's own children. The whole header group, tooltip included, is painted below the sidebar group.
The fix is to change the groups, not the number. Raise the header's z-index above the sidebar's, for example z-index: 3.
Seeing the structure
Read the tree from the top. Siblings are compared with each other first. Only then are children painted inside their parent's slot.
Accidental contexts
The most common source of confusion is a context you did not mean to create. A designer adds transform: translateY(0) to animate a card, or opacity: 0.99 to fix a rendering glitch. Any modal inside that card is now trapped in the card's context.
.card {
transform: translateZ(0); /* creates a stacking context */
}
.card .dropdown {
position: absolute;
z-index: 1000; /* only ranks inside .card */
}A transform also makes the element the containing block for its fixed descendants. A position: fixed modal placed inside such a card will be positioned relative to the card, not the viewport.
Using isolation on purpose
You can create a context deliberately to keep a component's internal z-index values from leaking out.
.widget {
isolation: isolate;
}With this rule, any z-index inside .widget stays inside it, and the widget as a whole sits at z-index: auto level in its parent. This is cleaner than adding a meaningless position: relative; z-index: 0.
Debugging checklist
- Find the two elements that overlap and list each one's ancestors.
- Walk up from each element to find the nearest ancestor that creates a stacking context.
- Find the first ancestor pair that share a parent context. Those two are the ones actually being compared.
- Check their
z-indexvalues and their order in the HTML. With equal values, the later element in the source is painted on top. - Look for unexpected
transform,opacity,filter, orwill-changerules on the ancestors.
Browser developer tools help here. Select an element and inspect the computed styles of each ancestor in turn.
Exercise
A dropdown has position: absolute; z-index: 5000, but it is covered by a sibling section with position: relative; z-index: 2. The dropdown's parent has position: relative; z-index: 1. Why does it lose, and what is one fix?
Show answer
The parent has a z-index other than auto and is positioned, so it forms a stacking context at level 1. The dropdown's 5000 only applies inside that context. The sibling section forms a context at level 2, so it is painted above the whole parent group. One fix is to raise the parent's z-index above 2. Another is to move the dropdown out of the parent, for example directly under body.