Z-INDEX /// STACKING CONTEXT /// ISOLATION /// Z-INDEX /// STACKING CONTEXT /// ISOLATION /// Z-INDEX ///

CSS Z-Index

Escape the 2D plane. Master stacking contexts, DOM order, and the infamous z-index warfare.

z-index.css
1 / 12
12345
🥞

Tutor:The web isn't flat. While X and Y are up, down, left, and right, the Z-axis comes directly out of the screen towards you.


Skill Matrix

UNLOCK NODES BY MASTERING THE Z-AXIS.

Concept: Positioning

Before mastering the Z-Axis, you must position your element. Static elements ignore z-index entirely.

System Check

Which of these position values does NOT allow z-index to work?


Community Holo-Net

Debate Stacking Contexts

ACTIVE

Stuck trying to get a modal to overlap a sticky header? Ask the community!

CSS Z-Index: Conquering the Z-Axis

Author

Pascual Vila

Frontend Instructor // Code Syllabus

The most common CSS joke: `z-index: 9999999; !important`. But z-index is not magic; it operates on strict mathematical rules defined by DOM order, positioning, and stacking contexts.

The Natural Stacking Order

Before we even touch z-index, the browser has a built-in way of deciding what goes on top. If two elements overlap, the browser looks at the HTML document. The element that appears later in the HTML will be drawn on top of the elements that appear before it.

Furthermore, within a single container, elements are painted in this specific order (from back to front):

  • Background and borders of the root element
  • Descendant non-positioned blocks (like standard divs)
  • Descendant positioned elements (like position: absolute)

Activating Z-Index

The property z-index allows us to override the natural stacking order. However, it comes with a strict prerequisite: it only applies to positioned elements.

If you add z-index: 100 to a normal static div, nothing happens. You must add position: relative, absolute, fixed, or sticky for the browser to acknowledge the z-index value.

The Final Boss: Stacking Contexts

This is where developers cry. A Stacking Context is a three-dimensional conceptualization of HTML elements. When an element forms a stacking context, everything inside it is evaluated together against the rest of the document.

Imagine two folders on a desk. Even if a paper inside Folder A is marked "priority 1,000,000", if Folder B is placed on top of Folder A, you won't see the paper. The paper is trapped inside Folder A.

What triggers a new stacking context?

  • A positioned element with a z-index value other than auto.
  • An element with opacity less than 1.
  • Elements with transform, filter, or perspective applied.
  • The modern solution: isolation: isolate.
View Modern Workarounds+

Use isolation: isolate! Instead of adding position: relative; z-index: 1; just to create a stacking context (which might break other layouts), use isolation: isolate. It does exactly one thing: creates a new stacking context safely.

Frequently Asked Questions

Why is my z-index not working?

1. The element isn't positioned. Add position: relative;.
2. It's trapped inside a parent element that has formed a stacking context with a lower z-index than the element you are trying to overlap.

Can I use negative z-index?

Yes! z-index: -1 is often used to push decorative pseudo-elements (like ::before) behind their parent's background. Just ensure the parent forms a stacking context so the pseudo-element doesn't disappear behind the body.

Should I use z-index: 9999?

Ideally, no. This is called "z-index warfare." It's better to structure your HTML logically, rely on DOM order, and use small integer steps (1, 2, 3) or dedicated variables (e.g., --z-modal: 100) to manage layers systemically.

Z-Axis Glossary

z-index
A CSS property that specifies the stack order of an element. Elements with higher z-index overlap lower ones.
snippet.css
position
Defines the positioning method. Required for z-index to take effect (relative, absolute, fixed, sticky).
snippet.css
stacking context
A three-dimensional conceptualization of HTML elements along an imaginary z-axis relative to the user.
snippet.css
isolation
A CSS property specifically designed to create a new stacking context without messing with positioning.
snippet.css
DOM order
The order in which elements appear in the HTML source code. Later elements sit on top naturally.
snippet.css
opacity
Sets the transparency level. An opacity less than 1 automatically creates a new stacking context.
snippet.css