CSS z-index Property

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The z-index property sets the stack level of a box along the z-axis. When positioned boxes overlap, a higher value paints in front of a lower one.

It applies to elements with position other than static, and to flex or grid items. The initial value is auto. It is not inherited. An integer value also creates a local stacking context.

Baseline Widely available.

Standard CSS 2 property. Use small, intentional integers. Huge values like 9999 are rarely needed and harder to maintain.

CSS Syntax

syntax.css
z-index: auto | <integer> | initial | inherit;

Integers may be positive, 0, or negative. Decimals are not valid.

Property Values

ValueDescriptionTry it
autoDefault. Stack level 0; no new stacking context from z-indexTry it
<integer>Stack level such as 1, 10, or -1Try it
negativePlaces the box behind siblings in the same contextTry it
with positionNeeds a non-static position (or flex/grid item)Try it
initialResets to autoTry it
inheritTakes the parent’s z-indexTry it

How to Use z-index

Position the boxes, then set clear stack levels.

  1. Give overlapping boxes a non-static position (for example relative or absolute).
  2. Set z-index integers so the front box has the higher value.
  3. Keep a small scale (for example 1, 2, 3) instead of huge numbers.
  4. Remember stacking contexts — a child cannot escape a parent context with a huge z-index.
PieceRequired?Role
Positioning / flex / gridYesMakes z-index apply
z-indexYesSets the stack level
OverlapFor demosWithout overlap, order is hard to see
Stacking context awarenessImportantParent contexts can trap children

Live Preview

Two positioned boxes overlap. Change the teal box’s z-index and watch which one is on top.

Purple box stays at z-index: 0. Teal box changes.

Overlap demo

Teal z-index: 1 — teal is in front

CSS Examples

Each snippet focuses on z-index. Use Try It Yourself for a live editor.

Example 1 — Higher Integer Wins

Give the front box a larger z-index than the back box.

CSS
<style>
  .box {
    position: absolute;
  }

  .back {
    z-index: 1;
  }

  .front {
    z-index: 2;
  }
</style>

<div class="stage">
  <div class="box back">1</div>
  <div class="box front">2</div>
</div>
Try It Yourself

How It Works

  1. Position both boxes. position: absolute lets them overlap.
  2. Assign stack levels. Back uses 1, front uses 2.
  3. Higher paints later. The box with 2 appears on top.
  4. Keep numbers small. You only need relative order, not huge values.

Example 2 — Negative Behind

A negative z-index can tuck a box under its siblings.

CSS
<style>
  .box {
    position: absolute;
  }

  .under {
    z-index: -1;
  }

  .over {
    z-index: 0;
  }
</style>

<div class="stage">
  <div class="box under">-1</div>
  <div class="box over">0</div>
</div>
Try It Yourself

How It Works

  1. Use a negative level. -1 sits behind stack level 0.
  2. Same context. Both boxes share one parent stacking context.
  3. Good for decoration. Background shapes often use negative values.
  4. Watch the parent. A parent with its own context can still clip or cover the child.

Example 3 — Auto (Default)

With auto, order follows normal stacking (usually source order).

CSS
<style>
  .box {
    position: absolute;
    z-index: auto;
  }
</style>

<div class="stage">
  <div class="box first">A</div>
  <div class="box second">B</div>
</div>
Try It Yourself

How It Works

  1. Initial value. Omitting z-index is the same as auto.
  2. Stack level 0. Boxes share the default layer.
  3. Later usually wins. In source order, the second overlapping box typically paints on top.
  4. No new context. Unlike an integer, auto does not create a stacking context by itself.

Edge Cases & Pitfalls

Most “z-index bugs” are stacking-context bugs, not number bugs.

position

Needs a non-static position (usually)

On normal flow boxes, set position to relative, absolute, fixed, or sticky. Flex/grid items are an exception.

context

Children cannot escape a parent context

If a parent creates a stacking context, a child with z-index: 9999 still stacks only inside that parent.

opacity

Other properties also create contexts

opacity less than 1, transform, isolation: isolate, and more can create stacking contexts without a z-index integer.

scale

Avoid huge magic numbers

Prefer a small local scale. Competing 99999 values make overlays hard to debug.

Browser Support

z-index is a browser layout property. Node.js, Deno, and Bun do not apply it.

✓ Baseline · Long supported

z-index

Supported in Chrome, Firefox, Safari, Edge, Opera, Samsung Internet, and Internet Explorer. Universal across modern and legacy browsers.

99% All major browsers
Google Chrome Yes
Full support
Mozilla Firefox Yes
Full support
Apple Safari Yes
Full support
Microsoft Edge Yes
Full support
Opera Yes
Full support
Samsung Internet Yes
Full support
Internet Explorer 6+
Full support
z-index Universally supported

Bottom line: Safe everywhere. Focus on stacking contexts and positioning more than version numbers. Not a server runtime API.

Key Takeaways

  • Role: set the stack level of overlapping positioned (or flex/grid) boxes.
  • Values: auto or an integer (positive, 0, or negative).
  • Initial value: auto. Not inherited. Integers create a local stacking context.
  • Tip: fix stacking contexts before raising numbers — 9999 rarely fixes a trapped child.

One line: z-index decides which overlapping positioned box paints on top — inside its stacking context.

Frequently Asked Questions

It sets the stack level of a positioned element (or a flex/grid item) along the z-axis. Higher values paint in front of lower values when boxes overlap.
The initial value is auto. The element uses stack level 0 and does not create a new local stacking context from z-index alone.
No for normal flow boxes. The element usually needs position other than static. Flex and grid items can also use z-index without repositioning.
A stacking context is an isolated layer group. Child z-index values compete only inside that group, not against the whole page.
Yes. Negative integers place a box behind siblings in the same stacking context, often used for backgrounds.
It is Baseline and long-supported in all major browsers, including Internet Explorer. It is a browser layout property, not a Node.js feature.

Next: CSS Selectors

Learn how to target elements before styling their stacking order.

CSS Selectors tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful