Skip to content

CSS z-index Not Working Fix

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about CSS z. We cover key concepts, practical examples, and best practices.

The Problem

You set z-index: 9999 on an element but it still renders behind other elements. The z-index property only works within the same stacking context, and many CSS properties create new stacking contexts without warning.

Quick Fix

Step 1: Ensure position is set

z-index only works on positioned elements:

/* Wrong — no position set */
.overlay {
    z-index: 100;
}

/* Right */
.overlay {
    position: relative;
    z-index: 100;
}

Expected output: The element now respects z-index.

Step 2: Check for parent stacking contexts

A positioned parent with z-index creates a new stacking context:

.parent {
    position: relative;
    z-index: 1;
}
/* Wrong — child z-index only works inside parent */
.child {
    position: absolute;
    z-index: 9999;
}
/* Right — raise parent z-index or flatten the structure */
.parent {
    position: relative;
    z-index: 10;
}

Expected output: The child appears above elements stacked against the parent.

Step 3: Remove opacity, transform, or filter on ancestors

These properties create new stacking contexts:

/* Wrong — opacity creates stacking context */
.modal-container {
    opacity: 0.99;
}
.modal {
    position: absolute;
    z-index: 1000;
}

/* Right — remove opacity or apply it differently */
.modal-container {
    /* no opacity on container */
}
.modal {
    position: absolute;
    z-index: 1000;
    opacity: 0.99;
}

Expected output: The modal appears above all content.

Step 4: Verify no isolation property blocks stacking

/* Wrong — isolation: isolate creates a new stacking context */
.container {
    isolation: isolate;
}

Expected output: Removing isolation: isolate restores normal stacking.

Step 5: Use the browser DevTools to inspect stacking contexts

Chrome DevTools shows stacking context boundaries in the Elements panel. Look for elements labeled "Stacking context" to identify the root cause.

Prevention

  • Always pair z-index with a position value
  • Minimise nesting depth to avoid accidental stacking contexts
  • Use isolation: isolate intentionally, not accidentally
  • Keep z-index values in a logical range (10-100) to avoid escalation

Common Mistakes with z index not working

  1. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
  2. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  3. Misunderstanding that String is [Char] with poor performance for large text operations

These mistakes appear frequently in real-world CSS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why does z-index work on some elements but not others?

The element may be inside a different stacking context. Check positioned ancestors — any ancestor with position: relative and z-index creates a context that scopes child z-index values.

Do transform and opacity always create stacking contexts?

Since CSS3, transform, opacity (below 1), filter, clip-path, and mix-blend-mode all create new stacking contexts. This is often why z-index stops working after adding a transform.

How do I make an element appear above everything on the page?

Place the element in position: fixed with a high z-index directly in the body, not nested inside positioned containers. Fixed positioning creates a stacking context at the root level, avoiding parent context issues.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro