# Z-index & Stacking Context

> Control the depth of overlapping elements, and understand why a z-index of 9999 sometimes refuses to work.

Subject: CSS | Lesson 18 of 44 | 6 min read
Interactive lesson: https://codewithanimation.in/css/z-index-and-stacking
Previous: [Positioning Schemes](https://codewithanimation.in/css/position-schemes.md) | Next: [Float, Clear & Legacy Display](https://codewithanimation.in/css/float-clear-and-legacy.md)

## What it is

While `position` lets you move elements around on the X and Y axes, `z-index` controls the Z-axis—depth.

When elements overlap, the browser needs to know which one should be rendered in front of the other. By default, elements that come later in your HTML code will overlap elements that came earlier. The `z-index` property allows you to override this order by assigning integer values. Higher numbers are closer to the user.

## Why it matters

Without `z-index`, UI components like sticky navigation bars, dropdown menus, and modal popups would be drawn behind the normal content of your page, rendering them unusable.

## How it works

To use `z-index`, two conditions must be met:

1. The element must have a `position` value other than `static` (e.g., `relative`, `absolute`, `fixed`, `sticky`). Note: Flexbox and Grid children can also use `z-index` without being explicitly positioned.

2. You assign an integer (positive, negative, or zero). For example: `z-index: 10`.

It is a best practice to use a defined scale (like 10, 20, 30, 40) rather than randomly typing `999` or `99999`. This leaves room to insert new layers between existing ones later.

## Common mistake: The Stacking Context Trap

The most notorious issue in CSS is when you give an element `z-index: 9999` and it still sits *behind* an element with `z-index: 2`.

This happens because of a concept called the **Stacking Context**. Think of a stacking context like a sealed folder. If 'Folder A' has a `z-index` of 1, and 'Folder B' has a `z-index` of 2, *everything* inside Folder B will always be in front of *everything* in Folder A.

Even if a file inside Folder A has `z-index: 9999`, it is trapped inside that folder. It can only be 9999 relative to other items *in the same folder*.

## Tip: Accidental Contexts

You might accidentally create a new stacking context without realizing it. Aside from using `position` and `z-index`, properties like `opacity` (less than 1), `transform`, `filter`, and `clip-path` automatically force the browser to create a new, inescapable stacking folder.

## Try it: Try it

Toggle the `z-index` on Parent A. Watch how giving it `z-index: 1` traps the red child (which has z-index: 9999) completely behind Parent B.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/z-index-and-stacking

## Check yourself

### Question 1

What is required for the `z-index` property to work on a standard block element (assuming it isn't inside a Flexbox or Grid container)?

- A. It must have a position other than 'static'
- B. It must be set to 'display: absolute'
- C. It must have a background color

Correct answer: A

For z-index to have any effect, the element must be positioned (relative, absolute, fixed, or sticky).

- Why not B: Display: absolute does not exist. You mean position: absolute.
- Why not C: Background color affects visibility, but not stacking logic.

### Question 2

If Parent A has `z-index: 1` and Parent B has `z-index: 2`, can a child inside Parent A with `z-index: 9999` overlap Parent B?

- A. No
- B. Yes
- C. Only if the child is position: fixed

Correct answer: A

No. Parent A and Parent B establish stacking contexts. Because B (2) is higher than A (1), everything inside B will always render in front of everything inside A, regardless of the children's internal z-index values.

- Why not B: The child's z-index only applies within Parent A's stacking context.
- Why not C: A fixed element can sometimes escape, but if Parent A is transformed, even fixed elements are trapped.

### Question 3

Which of the following CSS properties automatically creates a new stacking context?

- A. opacity: 0.99
- B. padding: 20px
- C. border: 1px solid black

Correct answer: A

Applying any opacity less than 1 (or properties like transform and filter) forces the browser to render the element and its children as a single flattened group, instantly creating a new stacking context.

- Why not B: Padding changes spacing, not rendering depth.
- Why not C: Borders do not affect stacking contexts.

## Remember this

- `z-index` requires `position` (relative, absolute, fixed, or sticky).
- Higher integer numbers render in front of lower numbers.
- A parent with a `z-index` creates a 'Stacking Context' (a sealed folder).
- A child with `z-index: 9999` cannot escape a parent whose `z-index` is lower than a sibling.
