CSS3 Advanced Modern Layout · 6 min read

CSS isolation for Stacking Contexts

Lesson 41 of 41 in the CSS3 learning path.

All Tutorials ❯

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

Syntax
.card { isolation: isolate; }

Example

Interactive preview available
stack.css
.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

  1. The card forms its own stacking context.
  2. The badge z-index is compared only with siblings inside the card.
  3. The modal lives outside that context.
  4. A high badge index cannot cover the modal.
  5. position relative is still useful for the badge anchor.
  6. isolate does not hide overflow.
  7. You do not need a random z-index on every card.
  8. 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.

Need more coins?

You do not have enough coins for this action.

Your balance: 0 coins

Share

Reactions

Loading reactions…