Syntax
z-index: number;Examples
z-index Requires Positioning
A common mistake - z-index silently does nothing without position set.
.behind {
z-index: 999; /* has NO effect - position is still static (the default) */
}
.in-front {
position: relative; /* now z-index actually applies */
z-index: 999;
}The Stacking Context Trap
Why a huge z-index sometimes still loses to a smaller one.
.parent-a {
position: relative;
z-index: 1; /* creates a new stacking context */
}
.parent-a .child {
position: relative;
z-index: 9999; /* only competes WITHIN parent-a's stacking context */
}
.parent-b {
position: relative;
z-index: 2; /* parent-b beats parent-a entirely, so ALL its children render above,
regardless of how high a z-index .child has inside parent-a */
}Parameters and values
- z-index (number | auto): Stacking order among elements in the same stacking context
Best practices
- Remember z-index only has an effect on positioned elements (position other than static) - set position: relative (or another value) first
- Understand that properties like opacity < 1, transform, and filter also create new stacking contexts, which can trap z-index values unexpectedly
- Use a documented, limited scale of z-index values across a project (like 10, 20, 30, 100 for modals) rather than arbitrary huge numbers, to keep stacking predictable
- When a high z-index "does not work," the real cause is almost always an unexpected stacking context on an ancestor, not the z-index value itself being too low
At a glance
- Purpose
- Presentation and layout
- File extension
- .css
- Runs in
- Web browsers
- Usually used with
- HTML and JavaScript
Specifications & further reading
Related CSS documentation
Specificity
When multiple CSS rules target the same element with conflicting declarations, specificity determines which one wins. It is calculated as a four-part value: inline styles, ID selectors, class/attribute/pseudo-class selectors, and type/pseudo-element selectors, compared in that order from most to least significant.Cascade Layers (@layer)
@layer lets you explicitly define the priority order between groups of CSS rules, independent of selector specificity. Styles in a layer declared later always beat styles in an earlier layer, regardless of how specific the earlier layer's selectors are - solving specificity conflicts between resets, frameworks, components, and overrides in a predictable, structured way.Custom Properties (CSS Variables)
Custom properties, written as --name, let you define reusable values directly in CSS, accessed with var(). Unlike Sass variables, they are live in the browser - they can be read and changed with JavaScript, and they respect the cascade, meaning their value can be different in different parts of the DOM.Media Queries
Media queries apply CSS conditionally based on characteristics of the device or viewport, most commonly width, forming the foundation of responsive design. They use the @media at-rule with a condition, and can be combined with and/or logic to test multiple conditions at once.
When multiple CSS rules target the same element with conflicting declarations, specificity determines which one wins. It is calculated as a four-part value: inline styles, ID selectors, class/attribute/pseudo-class selectors, and type/pseudo-element selectors, compared in that order from most to least significant.Cascade Layers (@layer)
@layer lets you explicitly define the priority order between groups of CSS rules, independent of selector specificity. Styles in a layer declared later always beat styles in an earlier layer, regardless of how specific the earlier layer's selectors are - solving specificity conflicts between resets, frameworks, components, and overrides in a predictable, structured way.Custom Properties (CSS Variables)
Custom properties, written as --name, let you define reusable values directly in CSS, accessed with var(). Unlike Sass variables, they are live in the browser - they can be read and changed with JavaScript, and they respect the cascade, meaning their value can be different in different parts of the DOM.Media Queries
Media queries apply CSS conditionally based on characteristics of the device or viewport, most commonly width, forming the foundation of responsive design. They use the @media at-rule with a condition, and can be combined with and/or logic to test multiple conditions at once.