Stacking context
A local stacking world created by certain CSS properties.
z-index controls stacking order when elements overlap. It works only within stacking context rules, which is where many CSS surprises begin.
:root {
--z-header: 10;
--z-modal: 100;
}
Layout
z-index affects how overlapping elements stack along the visual z-axis. But it only applies in certain layout conditions and within stacking contexts.
A stacking context is like a local layer group. An element inside one context cannot always jump above elements in another context just by using a huge number.
Professional CSS uses small z-index scales for headers, dropdowns, modals and overlays instead of random values like 999999.
A local stacking world created by certain CSS properties.
z-index commonly works with positioned elements.
Named z-index tokens keep order predictable.
Large numbers often hide a misunderstanding of context.
Visual model
Good layout CSS is not a pile of positions. It is a set of relationships: flow, axis, tracks, placement, layering, sticking and rhythm.
Normal content layer.
Persistent navigation layer.
Temporary menu layer.
Top interaction layer.
Examples
:root {
--z-header: 10;
--z-dropdown: 30;
--z-modal: 100;
}
.menu { z-index: 9999; }
.modal { z-index: 99999; }
.tooltip { z-index: 999999999; }
Rules that matter
Layout work is where real content, real devices and real users expose weak assumptions. The goal is a structure that explains itself in code and survives change.
A higher number only wins inside the relevant context.
Small named z-index values are easier to reason about.
They are usually a symptom, not a solution.
position, opacity, transform and isolation can affect stacking.
Modals and dropdowns should follow a shared layer system.
Inspect stacking contexts instead of guessing numbers.
Production thinking
z-index bugs create hidden controls, broken menus and confusing overlays. A layer system keeps interactions predictable.
Overlays must not trap or hide focus incorrectly. Layering should support keyboard and screen reader behavior.
Define z-index tokens for common layers and avoid component-level number battles.
Content hidden behind overlays or headers can damage usability, especially on mobile.
Live code lab
The preview runs in an isolated iframe. Links and forms stay inside the practice area, so you can experiment without leaving the lesson.
Mini assignment
Practice assignment
Try it yourself
Self-check
Answer these questions before moving on. If the answer is vague, change the lab layout and inspect the result.
Senior audit upgrade
A higher z-index cannot escape every stacking context. First find which ancestor created the stacking context.
position with z-index, transform, opacity below 1, filter, isolation and some contain values can create contexts.
Check parents before changing the child z-index.
Use named z-index tokens for nav, overlay, modal and toast layers.