CSS isolation for Stacking Contexts
Lesson 41 of 41 in the CSS3 learning path.
How do I stop a card shadow from mixing with a popup z-index?
isolation: isolate starts a new stacking context. z-index inside it is local to that box.
A badge with z-index 5 inside a card should not climb over a modal that lives elsewhere.
Syntax & Parameters
.card { isolation: isolate; }
Example
Interactive preview available.card {
position: relative;
isolation: isolate;
z-index: 0;
}
.badge {
position: absolute;
z-index: 2;
}
.modal {
position: fixed;
z-index: 20;
}
.card, .badge, .modal {
outline: 1px solid transparent;
}
Expected Output
The badge stacks above other card content and stays under the page modal.
Line-by-Line Walkthrough
- The card forms its own stacking context.
- The badge z-index is compared only with siblings inside the card.
- The modal lives outside that context.
- A high badge index cannot cover the modal.
- position relative is still useful for the badge anchor.
- isolate does not hide overflow.
- You do not need a random z-index on every card.
- Debug stacking by naming which context each box belongs to.
Common Pitfalls & Security
Check this before you copy it.
- isolation does not clip children. Overflow still paints outside unless you hide it.
- A transform or opacity on the card also creates a stacking context. Know which property did it.
- z-index on a static box is ignored.
- Spraying isolate on every div makes later overlays harder to reason about.
Best Practices & Modern Standards
Isolate a component that has its own internal layers.
Keep page-level overlays, such as modals, in a context above those components.
Real-World Use
A course card can lift its own badge without the badge ever covering the site menu or a dialog.
Frequently Asked Questions
Does isolate set z-index?
It creates a context. You can still set z-index: 0 so the card participates in the parent order.
How is this different from overflow hidden?
Overflow clips painting. Isolation only groups stacking.
What else creates a context?
opacity less than 1, transform, filter, and a positioned z-index.
Should every card use it?
Use it when the card has layered pieces. Skip it on a flat link.