Z-index is the CSS property that sets the stacking order of a box along the imaginary z-axis that points from the screen toward the viewer, so that overlapping elements with a higher z-index value are painted in front of those with a lower one. It accepts the keyword auto or an integer, including zero and negative numbers.
In CSS, z-index matters whenever content overlaps: dropdown menus, sticky headers, modal dialogs, tooltips and decorative layers. Its behavior is often misunderstood because the value is never global. A z-index only competes with siblings inside the same stacking context, which is why a value of 9999 can still end up behind an element set to 1.
How z-index decides painting order
Z-index decides painting order by assigning each qualifying box a stack level within its parent stacking context. The browser then paints boxes from the lowest level to the highest, so later layers cover earlier ones wherever they overlap. Boxes with the same stack level are painted in document source order.
Historically, z-index applied only to positioned elements, meaning those with position set to relative, absolute, fixed or sticky. Modern CSS also honors z-index on flex items and grid items even when they keep position: static, a detail that surprises developers who learned the older rule.
Inside a single stacking context, the CSS painting algorithm follows a fixed layer sequence. The table shows that sequence from back to front.
| Layer, back to front | What is painted | Typical z-index |
|---|---|---|
| Context root background | Background and borders of the root element | Not applicable |
| Negative stack levels | Positioned descendants with negative values | -1 and lower |
| Block-level flow content | Non-positioned block boxes | None |
| Floats | Floated boxes | None |
| Inline flow content | Text and inline boxes | None |
| Zero and auto levels | Positioned boxes with auto or 0 | auto or 0 |
| Positive stack levels | Positioned boxes with positive values | 1 and higher |
This order explains why a negative z-index can place a positioned box behind the text of its parent but still above the parent’s background, provided the parent forms the stacking context.
Z-index values and their effects
The value of z-index determines both the stack level and whether the element creates a new stacking context. The difference between auto and 0 is the most important subtlety: both paint at the same level, but only the integer creates a new context for descendants.
The table summarizes the accepted values and what each one does.
| Value | Stack level | Creates stacking context | Common use |
|---|---|---|---|
| auto | Same as parent level, effectively 0 | No | Default for most elements |
| 0 | Level 0 | Yes, on positioned elements | Isolating a component |
| Positive integer | Above level 0 | Yes | Menus, overlays, dialogs |
| Negative integer | Below in-flow content | Yes | Decorative backgrounds |
| Global keywords | Inherited or reset value | Depends on result | Resetting overrides |
Values must be whole numbers; decimals such as 1.5 are invalid and ignored. Browsers store z-index as a 32-bit signed integer, so the practical maximum is 2147483647, although relying on very large numbers usually signals an architectural problem rather than a solution.
Z-index and the stacking context
A z-index value is meaningful only relative to its stacking context, the group of elements that is painted as a single unit. Once an ancestor forms a context, all of its descendants are flattened into that ancestor’s layer, and no descendant can escape it, regardless of its own z-index.
Many properties other than z-index create stacking contexts, which is the usual source of confusing bugs. The table lists frequent triggers.
| Property or condition | Trigger value | Why it surprises developers |
|---|---|---|
| position with z-index | Any integer on a positioned box | Expected and intentional |
| position fixed or sticky | Always | No z-index needed |
| opacity | Any value below 1 | Fade animations change layering |
| transform | Any value other than none | Animation libraries add it silently |
| filter and backdrop-filter | Any value other than none | Blur effects isolate children |
| isolation | isolate | Designed for deliberate isolation |
| will-change | A property that would create a context | Performance hints alter stacking |
| contain | layout or paint | Containment for performance |
The isolation: isolate declaration is the cleanest way to create a context on purpose without changing position, opacity or transforms. It lets a component manage its internal layers while keeping them contained.
Managing z-index in larger projects
In larger codebases, z-index is best managed through a small, documented scale rather than ad hoc numbers. Teams commonly define named tokens with CSS variables or Sass maps, such as --z-dropdown or --z-modal, so every layer has an agreed position.
The table shows a representative layering scale; the exact numbers matter less than consistent spacing and naming.
| Layer token | Example value | Elements |
|---|---|---|
| Base | 0 | Normal page content |
| Raised | 10 | Cards on hover, sticky table headers |
| Dropdown | 100 | Menus and autocomplete lists |
| Sticky | 200 | Fixed site header |
| Overlay | 300 | Backdrop behind dialogs |
| Modal | 400 | Dialog windows |
| Toast | 500 | Notifications and alerts |
Leaving gaps between levels allows new layers to be inserted later without renumbering. Pairing the scale with isolation: isolate on component roots keeps internal values like 1, 2 and 3 local, which stops the familiar z-index arms race in which developers keep raising numbers to win conflicts.
Some overlays no longer need z-index at all. The native dialog element opened with showModal() and elements using the Popover API render in the browser’s top layer, which sits above every stacking context in the document.
Common z-index mistakes
Most z-index problems come from misreading the context hierarchy, not from choosing the wrong number. The following list covers the errors seen most often in production code.
- Setting z-index on a static block element outside flex or grid layout, where it has no effect.
- Raising a child’s value while a parent with a transform traps it in a lower context.
- Using
opacity: 0.99orwill-changeas a fix without realizing it creates a new context. - Assuming
autoand0are identical, although only0isolates descendants. - Spreading unrelated magic numbers such as 999, 9999 and 99999 across files.
Browser developer tools help diagnose these issues. Inspecting the computed styles of each ancestor for context-creating properties usually reveals the element that blocks the intended layering faster than trial and error.
The simple explanation
Think of a web page as a stack of transparent sheets lying on a desk. Z-index tells the browser which sheet goes on top, so the one with the larger number covers the ones below it.
There is a catch. Some sheets are stapled into folders, and a sheet inside a folder can only move up or down within that folder. Even a sheet marked with a huge number stays under a folder that lies lower in the pile. That is why the key is to know which folder an element belongs to before changing its number.
How it is used in practice
A front-end team maintains a news site with a sticky header at z-index 200 and a cookie banner at 500. After a redesign, a new image carousel uses transform for smooth sliding, and its full-screen lightbox, set to z-index 9999, suddenly appears underneath the header.
Inspection shows that the carousel wrapper has transform: translateX(0), which creates a stacking context at level auto inside the main content area. The lightbox can never rise above the header, because its context sits lower. The team moves the lightbox markup to the end of the body and later replaces it with a native dialog opened through showModal(), which places it in the top layer without any z-index.
The outcome is a lightbox that covers the header and banner on every device, and the team records the incident in its style guide to ban arbitrary large values in favor of named layer tokens.