A stacking context orders its own descendants as a group. Raising one child’s z-index cannot necessarily put it above a sibling of an ancestor; transforms, opacity, positioning, and other properties can create new contexts. Clipping from an overflow ancestor is another independent limit. Browser top-layer primitives can place supported modal dialogs and popovers above ordinary document stacking and outside ancestor clipping, but their interaction models differ. A modal interrupts other interaction and needs a focus contract. A lightweight popover has different dismissal and accessibility requirements.
Stacking Context, Top Layer, and Overlay Contract
Working case
The mobile case-review screen has an overflow-scrolling activity feed. An action menu rendered inside one feed row clips at the feed edge. Raising its z-index from 47 to 4700 changes nothing because the ancestor still clips it. Move the menu to an appropriate top-layer popover where supported, or place it in a controlled overlay root with positioning and dismissal behavior tested. The note editor is a true modal: opening it moves focus inside, blocks background interaction, supports Escape and a visible close control, then returns focus to the invoking action.
Implementation boundary
First identify whether the control is an inline expansion, nonmodal popover, or modal dialog. Use the semantic primitive that matches the interaction. A top-layer placement solves painting and clipping, not every issue: anchor placement, focus order, outside-click policy, mobile keyboard behavior, and route teardown still need work. Do not place an interactive menu inside a container that is hidden from assistive technology when open. For a fallback overlay root, preserve reading and focus order, and test coordinate updates after scroll and resize.
function overlayKind(blocksBackground, needsLightDismiss) {
if (blocksBackground) return "modal-dialog";
return needsLightDismiss ? "popover" : "inline-panel";
}
console.log(overlayKind(true, false));
// Output: modal-dialogCost and tradeoffs
Choosing a new stacking number is O(1) but often creates a long-term conflict because it does not explain the ancestor relationship. Measuring an anchor and repositioning on each scroll or resize can trigger layout work; batch reads and writes and stop listeners when the overlay closes. Browser-managed top-layer placement can remove much custom painting code, but support and exact behavior must be tested on target devices. Measure clipping, focus return, Escape behavior, and layout work during fast scrolling rather than judging only a desktop screenshot.
Failure trace
A team adds z-index 999999 to the menu. It still clips and appears behind a transformed sibling. Inspect ancestor stacking and overflow, then select a placement strategy. A second fix moves it to the document root but forgets to return focus when it closes, leaving keyboard users at the top of the page. Test pointer, keyboard, zoom, and assistive technology operation. Scroll the feed while the menu is open, resize the viewport, press Escape, leave the route, and ensure no detached overlay remains.
Decision note
Treat an overlay as a behavior and focus contract with a painting strategy. Use top-layer semantics when they fit; solve clipping at the correct ancestor boundary rather than piling on arbitrary z-index values.
Verification
- Complete the primary case with the capability available and with it denied or absent.
- Interrupt the interaction during its pending state and confirm no stale resource, focus, or scroll state remains.
- Check that server authorization and validation still hold after the browser interaction.
Common Mistakes
- Trying to escape ancestor clipping with a larger z-index.
- Using a modal for a small optional menu.
- Fixing paint order while leaving focus stranded.
Connected lessons
Overlays, Scroll, and Mobile Viewport; Scroll Containers, Anchoring, and Chaining; Pointer Gesture, Touch Action, and Alternate Control; Visual Viewport, Keyboard, and Focused Input; Dialog Focus and Interruption Boundary; CSS Layout and Interface Systems; Complex Accessible Interactions.
Apply and check
Build Project: mobile review overlay and scroll and review Web Development: browser capability and viewport decisions quiz.
