When does z-index actually apply?
<div class="stage">
<div class="first">A</div>
<div class="second">B</div>
</div>.stage {
display: grid;
grid-template-columns: 120px 120px;
isolation: isolate;
}
.first,
.second {
min-height: 100px;
}
.first {
z-index: 2;
background: #bfdbfe;
}
.second {
z-index: 1;
margin-inline-start: -40px;
background: #fed7aa;
}
z-index accepts auto or an integer, including negative integers. It applies to positioned elements and to flex or grid items even when the latter are statically positioned. [1][3]
In the example A covers B across their 40px overlap although B comes later in HTML. Both are grid items. Equal levels within the same paint category are resolved by painting order, not a universal last-in-DOM rule. auto differs from 0: a relative box with zero creates a stacking context, whereas auto normally does not. [1][3]
Why does z-index: 999999 lose to 2?
<div class="stage">
<div class="card">
<div class="tooltip">Details</div>
</div>
<div class="header">Header</div>
</div>.stage {
position: relative;
height: 180px;
isolation: isolate;
}
.card {
position: relative;
z-index: 1;
height: 140px;
background: #dbeafe;
}
.tooltip {
position: absolute;
z-index: 999999;
inset: 20px auto auto 20px;
padding: 16px;
background: #fde68a;
}
.header {
position: absolute;
z-index: 2;
inset: 0 0 auto;
height: 64px;
background: #cbd5e1;
}
A stacking context is painted as a unit against other contexts. Here the header has level 2 and the card level 1. The tooltip remains inside the card even at 999999, so it cannot cover the header. Compare the enclosing contexts first. [2]
Temporarily changing .card to z-index: 3 proves the cause, but raises the whole card, possibly covering navigation. If only the tooltip should escape, move it into a shared overlay root or use the top layer instead.
Which properties create hidden stacking contexts?
<div class="shell">
<div class="notice">Notice</div>
</div>.shell {
width: 240px;
height: 160px;
margin: 48px;
transform: translateX(0);
background: #dbeafe;
}
.notice {
position: fixed;
inset: 0 auto auto 0;
z-index: 20;
padding: 12px;
background: #fde68a;
}
Common triggers include opacity below 1, non-none transform or filter, isolation: isolate, layout or paint containment, container-type: size or inline-size, relevant will-change values, and fixed or sticky positioning. The computed value matters. [1][2]
Even translateX(0) changes the fixed notice’s containing block in this example, placing it at the shell rather than the viewport. Removing transform distinguishes positioning from stacking. Isolation alone creates a stacking context but does not establish a fixed-position containing block.
Is the dropdown behind something or clipped?
<div class="card">
<div class="preview">
<div class="thumbnail">Preview</div>
</div>
<div class="tooltip">Details</div>
</div>.card {
position: relative;
width: 200px;
}
.preview {
height: 80px;
overflow: hidden;
border-radius: 12px;
}
.thumbnail {
height: 120px;
background: #dbeafe;
}
.tooltip {
position: absolute;
z-index: 1;
inset: 60px auto auto 160px;
width: 120px;
padding: 8px;
background: #fde68a;
}
An ancestor with overflow: hidden or clip may clip overflowing descendants. A larger z-index does not undo applicable clipping; the containing block matters, so not every overflow ancestor clips every positioned descendant. [4]
Here only the preview is clipped, preserving rounded corners, while the tooltip is its sibling inside the card. It can extend past the card. If the whole panel must remain scrollable or clipped, consider moving the overlay outside that ancestor or using the top layer.
How can a non-modal overlay escape the layout?
<div class="card">
<button type="button" popovertarget="details">Details</button>
<div id="details" popover="auto">
<p>Additional information about the product.</p>
<button type="button" popovertarget="details" popovertargetaction="hide">
Close
</button>
</div>
</div>.card {
overflow: hidden;
isolation: isolate;
}
#details {
max-inline-size: min(24rem, 90vw);
padding: 1rem;
border: 1px solid #94a3b8;
border-radius: 12px;
}
A shown popover enters the browser-managed top layer. The declarative popover="auto" and popovertarget provide toggling, light dismissal and Escape dismissal. [5][7]
It does not make the background inert, supply ARIA menu semantics or automatically solve anchor placement in every configuration. Choose a positioning strategy, such as CSS Anchor Positioning where supported, and test keyboard interaction and browser support.
How do you build a modal without z-index escalation?
<button id="open-dialog" type="button">Open settings</button>
<dialog id="settings" aria-labelledby="settings-title">
<h2 id="settings-title">Settings</h2>
<p>Review your preferences.</p>
<form method="dialog">
<button type="submit" autofocus>Close</button>
</form>
</dialog>#settings {
max-inline-size: min(30rem, 90vw);
padding: 1.5rem;
border: 0;
border-radius: 12px;
}
#settings::backdrop {
background: rgb(15 23 42 / 60%);
}const openButton = document.querySelector("#open-dialog");
const dialog = document.querySelector("#settings");
openButton.addEventListener("click", () => {
if (!dialog.open) {
dialog.showModal();
}
});
showModal() puts a native dialog in the top layer and makes the rest of the same document inert. An open attribute or show() alone is not modal. Style the background using ::backdrop. [5][6][10]
The sample supplies an accessible name, initial focus and a close control via method="dialog". Escape normally closes it. Test focus restoration, scrolling and assistive technology behavior. A dialog inside an iframe cannot cover the embedding document.
How should application-wide layers be organized?
<header class="site-header">Header</header>
<main class="page-content">Content</main>
<div class="overlay-root">
<div class="notice">Notice</div>
</div>:root {
--z-content: 0;
--z-header: 10;
--z-overlay: 20;
}
.page-content {
position: relative;
z-index: var(--z-content);
isolation: isolate;
}
.site-header {
position: sticky;
inset-block-start: 0;
z-index: var(--z-header);
}
.overlay-root {
position: fixed;
inset: 0;
z-index: var(--z-overlay);
pointer-events: none;
}
.notice {
position: absolute;
inset: 1rem 1rem auto auto;
padding: 1rem;
background: #fde68a;
pointer-events: auto;
}
Use a small named scale for content, headers and overlays sharing the same DOM level. Here the page content isolates component layers; the overlay root is its sibling. pointer-events: none prevents the empty overlay surface from intercepting clicks, while its interactive content restores auto.
React createPortal can render into that root while preserving React context and event propagation through the React tree. It does not supply top-layer behavior, modality or anchor positioning. [8] Tokens do not repair accidental ancestor stacking contexts; native dialogs and popovers remain separate mechanisms.
How do you identify the real cause in DevTools?
const target = document.querySelector(".tooltip");
if (target) {
const ancestors = [];
for (let element = target; element; element = element.parentElement) {
const style = getComputedStyle(element);
ancestors.push({
element: element.tagName.toLowerCase(),
classes: element.className,
position: style.position,
zIndex: style.zIndex,
opacity: style.opacity,
transform: style.transform,
isolation: style.isolation,
contain: style.contain,
overflowX: style.overflowX,
overflowY: style.overflowY,
});
}
console.table(ancestors);
}
Inspect the hidden element in Elements, compare Styles and Computed, and examine ancestors of both overlapping elements. [9] This script lists selected computed properties for an ordinary HTML element. Run it after loading the tooltip example. It is not an exhaustive stacking-context detector: masks, individual transforms and top-layer state are among its omissions.
A crossed-out declaration points to cascade issues. If the computed value is right, compare the nearest diverging contexts. A disappearance at a container edge suggests clipping. Toggle properties individually. document.elementFromPoint(x, y) can help diagnose hit testing, but skips elements with pointer-events: none and is not a complete paint-order analyzer.
