CSS z-index Property
The CSS z-index property controls the stacking order of overlapping elements along the imaginary z-axis, which extends toward and away from the viewer.
Elements with higher stack levels generally appear in front of elements with lower levels within the same stacking context. The property is commonly used for dropdown menus, navigation panels, overlays, tooltips, and other interface components that overlap.
On This Page
Syntax
The z-index property accepts the keyword auto or an integer value.
selector {
z-index: auto | <integer>;
}
Common declarations include:
.element {
z-index: auto;
}
.background-element {
z-index: -1;
}
.content-element {
z-index: 1;
}
.dropdown-menu {
z-index: 100;
}
.modal-overlay {
z-index: 1000;
}
Integer values can be positive, negative, or zero. Decimal numbers, percentages, and length values are not supported.
The property also accepts CSS-wide keywords such as initial, inherit, unset, revert, and revert-layer.
Property Values
| Value | Description |
|---|---|
auto |
Does not assign an explicit integer stack level. The element participates in the applicable stacking order without creating a stacking context solely because of its z-index value. This is the initial value. |
<integer> |
Specifies an integer stack level. Higher values are painted above lower values within the same stacking context. |
0 |
Assigns a stack level of zero and creates a stacking context on applicable elements. |
initial |
Resets the property to auto. |
inherit |
Uses the computed z-index value of the parent element. |
unset |
Resets the property to auto because z-index is not inherited. |
revert |
Reverts the property according to the CSS cascade. |
revert-layer |
Reverts the property according to the previous cascade layer. |
There is no special maximum value that guarantees an element will appear above everything else. Stacking contexts and the browser's top layer can limit the effect of even very large values.
How It Works
When elements overlap, the browser determines which element appears in front according to CSS painting and stacking rules.
The z-index property allows authors to influence that order for applicable elements.
.box-one {
position: absolute;
z-index: 1;
}
.box-two {
position: absolute;
z-index: 2;
}
When these boxes overlap within the same stacking context, .box-two appears above .box-one because its stack level is higher.
The property does not move an element horizontally or vertically. It only affects the painting order.
Understanding the z-Axis
CSS layouts are normally described using the horizontal x-axis and vertical y-axis. The z-axis represents depth relative to the viewer.
Although z-index controls stacking along this conceptual axis, it does not create a three-dimensional perspective effect.
A higher value means an element is painted in front of lower-level elements within the same stacking context, not that it is physically closer in a 3D scene.
Elements with Equal Stack Levels
When overlapping elements have the same stack level and belong to the same relevant painting category, elements appearing later in the document generally paint above earlier elements.
.first-box,
.second-box {
position: absolute;
z-index: 1;
}
If the second box follows the first in the HTML, it normally appears in front when they overlap.
Other painting rules, including stacking contexts and element categories, can affect the final order.
Positioned Elements
The z-index property applies to positioned elements, including those using relative, absolute, fixed, or sticky positioning.
Relative Positioning
.relative-box {
position: relative;
z-index: 2;
}
The element remains in normal document flow while its stacking level can be controlled.
Absolute Positioning
.absolute-box {
position: absolute;
top: 20px;
left: 20px;
z-index: 5;
}
The element is removed from normal flow and can overlap other content. Its z-index determines its stacking level within the applicable context.
Fixed Positioning
.fixed-header {
position: fixed;
top: 0;
left: 0;
width: 100%;
z-index: 100;
}
Fixed elements create stacking contexts and can be layered above other page content, subject to ancestor stacking contexts and the top layer.
Sticky Positioning
.sticky-navigation {
position: sticky;
top: 0;
z-index: 50;
}
Sticky elements also create stacking contexts. Assigning an appropriate stack level can help prevent nearby content from painting over them.
Static Positioning
For an ordinary element using position: static, z-index generally has no effect unless the element qualifies through another rule, such as being a Flexbox or Grid item.
.ordinary-box {
position: static;
z-index: 10;
}
In this example, the z-index declaration does not establish a stacking level for an ordinary block element.
Stacking Contexts
A stacking context is a group of elements that is stacked independently from other groups.
Within a stacking context, descendant elements are ordered according to their own stacking rules. The entire context is then treated as a single unit when compared with other stacking contexts.
Why Stacking Contexts Matter
Consider two parent elements, each creating its own stacking context.
.parent-one {
position: relative;
z-index: 1;
}
.parent-two {
position: relative;
z-index: 2;
}
.child {
position: absolute;
z-index: 9999;
}
If .child is inside .parent-one, it cannot escape that parent's stacking context simply by using a very large z-index.
The entire .parent-one context remains below .parent-two because the parents have stack levels of 1 and 2.
Stacking Context Example
<div class="parent-one">
<div class="child">z-index: 9999</div>
</div>
<div class="parent-two">
z-index: 2
</div>
The child's high value only controls its order inside the first parent's stacking context. It does not place the child above the second parent's context.
This is one of the most common reasons a z-index declaration appears not to work.
Creating Stacking Contexts
Several CSS properties and layout conditions can create stacking contexts.
| Condition | Example |
|---|---|
| Root element | The document's root element creates a stacking context. |
| Positioned element with integer z-index | position: relative; z-index: 1; |
| Fixed or sticky positioning | position: fixed; or position: sticky; |
| Flexbox or Grid item with integer z-index | z-index: 2; on a flex or grid item |
| Opacity below 1 | opacity: 0.8; |
| Transform | transform: translateX(10px); |
| Filter | filter: blur(2px); |
| Isolation | isolation: isolate; |
| Containment | contain: layout; or contain: paint; |
| Relevant will-change value | will-change: transform; |
Other properties and conditions, including certain masks, clipping effects, container queries, and top-layer participation, can also create stacking contexts.
Using isolation
The isolation property can deliberately create a new stacking context.
.component {
isolation: isolate;
}
This is useful when an interface component needs its internal stacking order separated from surrounding content.
However, isolation does not allow descendants to escape their parent's stacking context.
Negative z-index Values
Negative z-index values place applicable elements at lower stack levels within their stacking context.
.background-decoration {
position: absolute;
z-index: -1;
}
This can be useful for decorative shapes placed behind other content.
However, negative values require care because the element may be painted behind other elements, backgrounds, or parts of its containing layout.
Creating a Controlled Background Layer
.card {
position: relative;
isolation: isolate;
}
.card-decoration {
position: absolute;
inset: 10px;
z-index: -1;
}
The isolated card establishes a stacking context, allowing the decorative element to be placed behind its content without unexpectedly moving behind unrelated page elements.
Always check the actual painting order when using negative values, especially with backgrounds and nested stacking contexts.
z-index with Flexbox and Grid
Flexbox and Grid items can use z-index without requiring a non-static position value.
Flexbox Example
.flex-container {
display: flex;
}
.flex-item-one {
z-index: 2;
}
.flex-item-two {
z-index: 1;
}
If these flex items overlap, the first item can appear above the second because it has a higher stack level.
Grid Example
.grid-container {
display: grid;
}
.grid-item-one {
grid-area: 1 / 1;
z-index: 1;
}
.grid-item-two {
grid-area: 1 / 1;
z-index: 2;
}
Both grid items occupy the same grid area. The second item appears above the first because its z-index is higher.
This behavior makes z-index useful for overlapping Grid and Flexbox layouts without adding unnecessary positioning declarations.
z-index and the Top Layer
Some browser-managed interface elements are placed in the top layer, which is separate from the normal document stacking hierarchy.
Examples include modal dialogs opened with showModal(), displayed popovers, and elements in fullscreen mode.
Elements in the top layer are painted above ordinary document content, regardless of how large an ordinary element's z-index value is.
Modal Dialog Example
<dialog id="example-dialog">
<p>This is a modal dialog.</p>
</dialog>
const dialog = document.querySelector("#example-dialog");
dialog.showModal();
Calling showModal() places the dialog in the top layer.
A normal page element using z-index: 999999 cannot be placed above that modal dialog simply by increasing its stack level.
Use the appropriate browser-managed top-layer features when building modal interfaces rather than relying on unusually large z-index values.
Examples
Example 1: Two Overlapping Boxes
Assign different stack levels to control which box appears in front.
.box-one {
position: absolute;
z-index: 1;
}
.box-two {
position: absolute;
z-index: 2;
}
The second box appears above the first when they overlap within the same stacking context.
Example 2: Dropdown Navigation
A dropdown menu may need to appear above nearby page content.
.navigation {
position: relative;
z-index: 100;
}
.dropdown-menu {
position: absolute;
top: 100%;
left: 0;
z-index: 1;
}
The dropdown is positioned relative to its navigation container. Its stacking behavior also depends on the stacking contexts of surrounding elements.
Example 3: Sticky Header
A sticky header can use z-index to remain above scrolling content.
.site-header {
position: sticky;
top: 0;
z-index: 100;
background: #fff;
}
The background prevents content underneath the header from showing through its surface.
Example 4: Decorative Background
A negative stack level can position a decorative element behind foreground content.
.feature-card {
position: relative;
isolation: isolate;
}
.feature-decoration {
position: absolute;
z-index: -1;
}
The isolated stacking context helps keep the decorative element within the intended component.
Example 5: Overlapping Grid Items
Grid items can overlap and use z-index without additional positioning.
.grid {
display: grid;
}
.grid-item-a,
.grid-item-b {
grid-area: 1 / 1;
}
.grid-item-a {
z-index: 1;
}
.grid-item-b {
z-index: 2;
}
The second item is painted above the first.
Example 6: Component Layer Scale
Using a consistent set of stacking values makes complex interfaces easier to maintain.
:root {
--z-content: 1;
--z-dropdown: 100;
--z-sticky: 200;
--z-overlay: 500;
}
.dropdown {
position: absolute;
z-index: var(--z-dropdown);
}
.sticky-panel {
position: sticky;
z-index: var(--z-sticky);
}
.overlay {
position: fixed;
z-index: var(--z-overlay);
}
These values provide a predictable layering convention when the elements participate in compatible stacking contexts.
Inheritance
The z-index property is not inherited. Child elements do not automatically receive the parent's stack level.
.parent {
position: relative;
z-index: 5;
}
.child {
/* z-index remains auto */
}
The initial value is auto.
Although the child does not inherit the parent's value, it still participates in the parent's stacking context when the parent creates one.
This distinction is important: belonging to a stacking context is not the same as inheriting a z-index value.
To explicitly inherit the parent's computed value, use z-index: inherit on an applicable child element.
Practical Example
This example displays three overlapping boxes with different z-index values. The blue box has the lowest positive stack level, the green box is in the middle, and the orange box appears on top.
HTML
<div class="z-index-demo">
<div class="z-index-box z-box-one">
z-index: 1
</div>
<div class="z-index-box z-box-two">
z-index: 2
</div>
<div class="z-index-box z-box-three">
z-index: 3
</div>
</div>
CSS
.z-index-demo {
position: relative;
isolation: isolate;
width: 100%;
max-width: 420px;
height: 250px;
border: 1px solid #ccc;
background: #f5f5f5;
}
.z-index-box {
position: absolute;
box-sizing: border-box;
display: flex;
align-items: flex-start;
justify-content: center;
width: 150px;
height: 110px;
padding: 12px 4px;
color: #fff;
font-size: 14px;
font-weight: bold;
}
.z-box-one {
top: 20px;
left: 20px;
z-index: 1;
background: #2471a3;
}
.z-box-two {
top: 65px;
left: 75px;
z-index: 2;
background: #1e8449;
}
.z-box-three {
top: 110px;
left: 130px;
z-index: 3;
background: #af601a;
}
Result: The orange box appears above the green and blue boxes because it has the highest z-index value. The green box appears above the blue box, which has the lowest value.
The example uses isolation: isolate to keep the demonstration's stacking context separate from surrounding page content.
Common Mistakes
Using z-index on an Ordinary Static Element
The z-index property generally does not affect ordinary static elements that are not Flexbox or Grid items.
/* Usually ineffective */
.box {
z-index: 10;
}
/* Effective on a positioned element */
.box {
position: relative;
z-index: 10;
}
Check whether the element is positioned or qualifies as a Flexbox or Grid item before adding positioning solely for stacking.
Using Extremely Large Values
A very large value does not guarantee that an element appears above everything else.
.tooltip {
position: absolute;
z-index: 999999;
}
If the tooltip belongs to a lower stacking context, it can still appear behind another element.
Inspect ancestor stacking contexts instead of repeatedly increasing the value.
Ignoring Parent Stacking Contexts
A child cannot escape its parent's stacking context through z-index alone.
If a dropdown appears behind nearby content, check whether its navigation container is inside a lower stacking context.
Using Decimal Values
The z-index property accepts integers, not fractional numbers.
/* Incorrect */
.box {
z-index: 1.5;
}
/* Correct */
.box {
z-index: 2;
}
Confusing z-index with 3D Transforms
The z-index property controls painting order. It does not provide three-dimensional positioning or perspective.
Use CSS transforms and perspective properties when a 3D effect is required.
Ignoring Other Stacking Context Triggers
Properties such as opacity, transform, and filter can create stacking contexts even when no explicit z-index is assigned.
Review these properties when the stacking order behaves unexpectedly.
Trying to Outrank the Top Layer
An ordinary element cannot appear above a browser-managed top-layer element merely by increasing its z-index.
Use appropriate dialog, popover, or fullscreen behavior when top-layer placement is required.
Important Considerations
- Stacking contexts: Values are compared within the applicable stacking context, not globally across the entire document.
- Positioning: The property applies to positioned elements and Flexbox or Grid items.
- Integer values: Positive, negative, and zero values are supported. Decimal values are invalid.
- Inheritance: The property is not inherited, although children participate in their ancestor stacking contexts.
- Other triggers: Opacity, transforms, filters, isolation, and other conditions can create stacking contexts.
- Top layer: Modal dialogs, popovers, and fullscreen elements can appear above ordinary stacking contexts.
- Negative values: Elements may appear behind backgrounds or other content, depending on the painting order.
- Consistent scales: Use a documented set of stacking levels instead of arbitrary large values.
- Accessibility: Ensure that layered menus, dialogs, and controls remain visible, usable, and keyboard accessible.
- Debugging: Inspect the element and its ancestors when a stacking value does not produce the expected result.
The most important rule is that z-index works within stacking contexts. Understanding those contexts is more useful than assigning increasingly large numbers.
Browser Support
The CSS z-index property is widely supported across modern browsers, including Chrome, Edge, Firefox, Safari, and Opera.
Support for integer stack levels on positioned elements is well established. Stacking contexts, Flexbox and Grid item behavior, and browser-managed top-layer features should be considered when testing more complex layouts.
For current compatibility information, visit Can I Use? .
Specifications
The z-index property is defined in the CSS Positioned Layout specification, with foundational stacking and painting behavior described in CSS 2.2.
