CSS z-index Not Working Fix
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-indexwith apositionvalue - Minimise nesting depth to avoid accidental stacking contexts
- Use
isolation: isolateintentionally, not accidentally - Keep
z-indexvalues in a logical range (10-100) to avoid escalation
Common Mistakes with z index not working
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[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
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro