brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: isolation: isolate in CSS keeps a negative z-index inside its card

Css
~/tips/tip-css-isolation-isolate-z-index

isolation: isolate;

A new stacking context stops decorative layers from slipping behind the whole page.

I like decorative shapes behind a card’s text, done with a pseudo element and z-index: -1. Without one extra line, that shape disappears behind the card’s own background or even the page.

.card {
  position: relative;
  isolation: isolate;
  background: #ffffff;
}
.card::before {
  content: "";
  position: absolute;
  inset: auto 1rem -0.5rem auto;
  width: 4rem; height: 4rem;
  background: #ffe08a;
  border-radius: 50%;
  z-index: -1;
}

A negative z-index means “behind the content of my stacking context”. If the card does not form its own stacking context, that context is somewhere higher up, maybe the root, and the circle ends up behind the card’s white background where you cannot see it.

isolation: isolate creates a new stacking context and does nothing else. No transform hack, no extra z-index on the card that might fight with a sticky header. The circle now sits behind the card’s text but in front of its background.

It also limits mix-blend-mode to the element, which was its original purpose. It is worth reaching for whenever z-index behaves strangely, because many z-index puzzles are really stacking context puzzles. Every current browser supports it.

Further reading: MDN: isolation, MDN: Stacking context

More Css tips CSS Overview in Chrome DevTools audits the colors and fonts of a page .notice:empty { .toast { article { counter-reset: h2; } .toolbar { All Css tips (66) All topics

Link