CSS z-index Property

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 4 Examples
Layout

What You’ll Learn

The z-index property controls which overlapping element appears on top — essential for modals, dropdowns, and layered UI.

01

Stacking

Layer order.

02

Syntax

auto + integers.

03

auto

Default value.

04

position

Required pairing.

05

Contexts

Isolated layers.

06

Modals

Overlays & menus.

Introduction

The z-index property in CSS controls the vertical stacking order of elements that overlap. When elements overlap, the z-index value determines which one appears on top.

This property only works on positioned elements (those with a position value other than static), or on flex and grid items when z-index is set.

Definition and Usage

Use z-index when building modals, sticky headers, dropdown menus, tooltips, and any UI where elements share the same screen space. Pair it with position so the browser can layer elements correctly.

💡
Beginner Tip

Higher numbers do not always win globally. A child with z-index: 9999 can still sit behind a sibling parent if that parent creates a lower stacking context. Always check parent elements too.

📝 Syntax

The syntax for the z-index property is simple and can be applied to any positioned element.

syntax.css
element {
  z-index: auto | <integer>;
}

Basic Example

z-index.css
.modal {
  position: fixed;
  z-index: 1000;
}

Syntax Rules

  • auto uses default stacking based on document order and stacking contexts.
  • Integers can be positive or negative; higher values stack in front within the same context.
  • z-index has no effect on position: static elements unless they are flex or grid items.
  • Creating a new stacking context (e.g., with position + z-index) isolates child layering.
  • The property is not inherited, but stacking contexts affect descendants.

Related Properties

  • position — required for z-index on most elements
  • opacity — values less than 1 can create a stacking context
  • transform — non-none transforms create stacking contexts
  • isolation: isolate — explicitly creates a new stacking context

🎯 Default Value

The default value of the z-index property is auto, which means the element will use the default stacking order based on its order in the HTML and its parent stacking context.

⚡ Quick Reference

QuestionAnswer
Default valueauto
Bring element to frontHigher integer, e.g. z-index: 10;
Send element behindNegative integer, e.g. z-index: -1;
Works withposition: relative | absolute | fixed | sticky
Modal patternBackdrop 100, dialog 101
InheritedNo

💎 Property Values

The z-index property accepts auto or an integer.

ValueExampleDescription
autoz-index: auto;The element uses the default stacking order (default).
integerz-index: 3;Any integer, positive or negative. Higher values appear in front of lower values in the same stacking context.
auto 1 10 100 -1

When to Use z-index

z-index is helpful whenever overlapping elements need a clear front-to-back order:

  • Modals and dialogs — Layer backdrop and content above the page.
  • Dropdown menus — Keep menus above neighboring buttons and cards.
  • Sticky headers and toolbars — Ensure navigation stays above scrolling content when needed.
  • Tooltips and popovers — Display hints on top of interactive UI.
  • Decorative backgrounds — Use negative values to push shapes behind content.

👀 Live Preview

Three overlapping boxes with z-index: 1, 2, and 3:

Examples Gallery

In this example, we will stack three colored boxes using the z-index property.

📜 Core Patterns

Control basic layering with positioned elements and integer values.

Example 1 — Stacked colored boxes

In this example, we will stack three colored boxes using the z-index property.

index.html
<style>
  .box { position: absolute; width: 100px; height: 100px; }
  .box1 { background: red; z-index: 1; }
  .box2 { background: green; z-index: 2; }
  .box3 { background: blue; z-index: 3; }
</style>

<div class="box box1"></div>
<div class="box box2"></div>
<div class="box box3"></div>
Try It Yourself

How It Works

Each box is absolutely positioned. The blue box (z-index: 3) appears on top because it has the highest value.

Example 2 — Modal overlay

Layer a semi-transparent backdrop and dialog above page content with separate z-index values.

modal.css
.backdrop {
  position: fixed;
  inset: 0;
  z-index: 100;
}

.modal {
  position: fixed;
  z-index: 101;
}
Try It Yourself

How It Works

Both layers use position: fixed. The modal’s higher z-index keeps it above the backdrop.

📄 Advanced Layering

Understand stacking contexts and negative values for predictable layouts.

Example 3 — Stacking context trap

A child with a huge z-index cannot escape a parent’s lower stacking context.

context.css
.group-a { position: relative; z-index: 1; }
.group-b { position: relative; z-index: 2; }
.child-high { position: absolute; z-index: 999; }
Try It Yourself

How It Works

Group B wins because its stacking context (z-index 2) sits above Group A’s context (z-index 1), regardless of the child’s 999.

Example 4 — Negative z-index background

Push a decorative pseudo-element behind card content with z-index: -1.

negative.css
.card {
  position: relative;
  z-index: 1;
}

.card::before {
  content: "";
  position: absolute;
  inset: -8px;
  z-index: -1;
}
Try It Yourself

How It Works

The card creates a stacking context. The pseudo-element’s negative z-index places the glow behind the card’s background but still inside the card box.

♿ Accessibility

  • Focus order vs visual order — Changing z-index does not change tab order. Keep keyboard focus logical.
  • Modals — Trap focus inside dialogs and restore focus when closed; z-index alone does not make a modal accessible.
  • Hidden content — Elements behind others may still receive pointer events unless you also manage visibility or pointer-events.
  • Contrast — Overlapping layers should maintain readable text contrast.

z-index + position

z-index depends on positioning. Set position first, then choose a z-index that fits your UI layer scale.

layer-scale.css
/* Example layer scale */
.dropdown { position: absolute; z-index: 50; }
.sticky-header { position: sticky; z-index: 40; }
.modal { position: fixed; z-index: 100; }

🧠 How z-index Works

1

Elements overlap

Positioned elements share the same screen area.

Input
2

Stacking context applies

Parent contexts group children into layers.

Context
3

z-index compares values

Higher integers render in front within the same context.

Order
=

Correct visual layering

The intended element appears on top.

Browser Compatibility

The z-index property is fully supported in all modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. It is also supported in older browsers, making it a reliable property to use for controlling the stacking order of elements.

Modern browsers · Universal support

Reliable layering control

auto and integer z-index values work consistently across all major browsers.

99% Browser support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions
Full support
Opera All versions
Full support

Testing tip

Test modals and dropdowns in Chrome, Safari, and Firefox. Verify stacking when parent elements use transform or opacity.

z-index property 99% supported

Bottom line: z-index is safe to use everywhere for layering positioned elements.

Conclusion

The z-index property is an essential tool for web developers when designing layouts with overlapping elements.

By controlling the stacking order, you can create complex and visually appealing designs. Experiment with different z-index values to see how they affect the stacking order of elements on your web pages.

💡 Best Practices

✅ Do

  • Set position before using z-index
  • Use a consistent layer scale (e.g., 10, 20, 100)
  • Keep modal backdrop one step below dialog content
  • Understand parent stacking contexts
  • Test dropdowns inside transformed parents

❌ Don’t

  • Throw z-index: 999999 at every problem
  • Assume higher child z-index beats all parents
  • Use z-index on static elements without flex/grid
  • Forget keyboard focus when layering modals

Key Takeaways

Knowledge Unlocked

Five things to remember about z-index

Use these points when layering overlapping UI.

5
Core concepts
🕐 02

auto

Default value.

Default
🔀 03

position

Required pair.

Pattern
🔄 04

Contexts

Layer groups.

Concept
🌐 05

Modals

Common use case.

UI

❓ Frequently Asked Questions

z-index controls the vertical stacking order of positioned elements. Higher values appear in front of lower values when elements overlap.
The default is auto, which lets the browser use normal stacking order based on document order and stacking contexts.
No. z-index only affects elements with a position value other than static, or flex/grid items with a z-index set.
A stacking context is an isolated layer group. Child z-index values compete only inside their parent context, not globally across the page.
Yes. Negative integers place an element behind siblings in the same stacking context, often used for decorative backgrounds.

Practice in the Live Editor

Open the HTML editor and experiment with z-index on overlapping boxes and modals.

HTML Editor →

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