Z-index

On this page 7

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 frontWhat is paintedTypical z-index
Context root backgroundBackground and borders of the root elementNot applicable
Negative stack levelsPositioned descendants with negative values-1 and lower
Block-level flow contentNon-positioned block boxesNone
FloatsFloated boxesNone
Inline flow contentText and inline boxesNone
Zero and auto levelsPositioned boxes with auto or 0auto or 0
Positive stack levelsPositioned boxes with positive values1 and higher
Painting layers inside one stacking context

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.

ValueStack levelCreates stacking contextCommon use
autoSame as parent level, effectively 0NoDefault for most elements
0Level 0Yes, on positioned elementsIsolating a component
Positive integerAbove level 0YesMenus, overlays, dialogs
Negative integerBelow in-flow contentYesDecorative backgrounds
Global keywordsInherited or reset valueDepends on resultResetting overrides
Accepted z-index values

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 conditionTrigger valueWhy it surprises developers
position with z-indexAny integer on a positioned boxExpected and intentional
position fixed or stickyAlwaysNo z-index needed
opacityAny value below 1Fade animations change layering
transformAny value other than noneAnimation libraries add it silently
filter and backdrop-filterAny value other than noneBlur effects isolate children
isolationisolateDesigned for deliberate isolation
will-changeA property that would create a contextPerformance hints alter stacking
containlayout or paintContainment for performance
Common properties that create a stacking context

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 tokenExample valueElements
Base0Normal page content
Raised10Cards on hover, sticky table headers
Dropdown100Menus and autocomplete lists
Sticky200Fixed site header
Overlay300Backdrop behind dialogs
Modal400Dialog windows
Toast500Notifications and alerts
Example z-index scale for an interface

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.99 or will-change as a fix without realizing it creates a new context.
  • Assuming auto and 0 are identical, although only 0 isolates 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.