# The Browser Top Layer vs CSS Stacking Context

- **Canonical URL:** https://frontend.rocks/vs/browser-top-layer-vs-css-stacking-context/
- **Relation:** Contrasts With
- **Last Verified:** 2026-11-15
- **Verdict:** Stacking contexts trap z-index inside ancestors, whereas the Top Layer renders above every stacking context.

## Comparison Matrix

| Dimension | The Browser Top Layer | CSS Stacking Context |
| :--- | :--- | :--- |
| **Maturity** | Baseline · Widely | Baseline · Widely |
| **Domain** | DOM & Events | Rendering Pipeline |
| **Mental Model** | End z-index: 99999 wars and overflow: hidden modal clipping—the Browser Top Layer paints <dialog> and [popover] in a document-level LIFO list above the root stacking context. | Hierarchical tree isolating Z-axis paint order. A high z-index remains trapped inside its parent stacking context. |
| **Prerequisites** | CSS Stacking Context (Top Layer elements paint in a dedicated layer above the entire document root stacking context.) | None |
| **When Not to Use** | When not to use Top Layer: Do not promote persistent inline banners or sticky headers; keep them in normal document flow, and use the inert attribute when non-Top-Layer regions must block background focus. | When not to use z-index: Use the Native <dialog> Element or Popover API in the Browser Top Layer for modals and tooltips instead of managing global z-index scales. |

## The Browser Top Layer — Under the Hood
The rendering engine maintains the Top Layer as a document-level LIFO ordered set painted after the entire root stacking context. No matter how many `overflow: hidden` clips or `z-index: 99999` contexts exist inside the DOM tree, promoted elements render cleanly above the whole document.

1. **LIFO Ordered Set**: The document maintains a `top layer` list; each `showModal()` or `showPopover()` appends to the end, painting last-in on top.
2. **Viewport Containing Block**: Top Layer elements escape ancestor `transform` and `clip-path` containing blocks.
3. **`::backdrop` Layer**: Each modal or popover receives its own `::backdrop` pseudo-element painted immediately beneath it.

```css
/* Animating Top Layer entry & exit cleanly */
dialog[open], [popover]:popover-open {
  opacity: 1;
  transform: scale(1);
  transition: opacity 0.2s, transform 0.2s, display 0.2s allow-discrete;
}
@starting-style {
  dialog[open], [popover]:popover-open { opacity: 0; transform: scale(0.96); }
}
```

## CSS Stacking Context — Under the Hood
1. During the Paint phase of the [Browser Rendering Pipeline](/concepts/browser-rendering-pipeline), the engine sorts boxes across 7 stacking levels (context background, `z-index < 0`, in-flow blocks, floats, inline, `z-index: 0`, `z-index > 0`).
2. Many non-positioning properties create a stacking context: `opacity < 1`, `transform !== none`, `filter !== none`, `isolation: isolate`, `contain: layout|paint`.
3. A child stacking context is painted as a single atomic unit inside its parent before subsequent sibling contexts are drawn.

```css
/* Header sits at root stacking order z-index: 10 */
.site-header { position: sticky; z-index: 10; }

/* Parent opacity < 1 creates a stacking context at z-index: 1 */
.card-wrapper { position: relative; z-index: 1; opacity: 0.99; }

/* Trapped inside .card-wrapper: still paints BEHIND .site-header */
.card-tooltip { position: absolute; z-index: 999999; }
```

## Interactive Labs & Dissections
- [The Browser Top Layer Lab](https://frontend.rocks/concepts/browser-top-layer/#lab) ([Markdown](https://frontend.rocks/concepts/browser-top-layer.md))
- [CSS Stacking Context Lab](https://frontend.rocks/concepts/css-stacking-context/#lab) ([Markdown](https://frontend.rocks/concepts/css-stacking-context.md))

## Related Architecture Autopsies
- [Autopsy #05: The z-index 9999 Modal Trap (and Its Native Exit)](https://frontend.rocks/autopsies/zindex-9999-tooltip-war/) ([Markdown](https://frontend.rocks/autopsies/zindex-9999-tooltip-war.md)): Tooltip or modal remains trapped behind a sticky header despite z-index: 9999 while JS positioning thrashes layout.

## Primary Sources
- https://fullscreen.spec.whatwg.org/#top-layer
- https://developer.chrome.com/blog/what-is-the-top-layer
- https://drafts.csswg.org/css2/#elaborate-stacking-contexts
- https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_positioned_layout/Understanding_z-index/Stacking_context
