Sknoe / Notes
Why Your z-index Is Ignoring You (Even at 9999)
If a huge z-index still puts your tooltip behind another element, the number isn't the problem. Here is how stacking contexts actually work.
We have all done it. You have a tooltip, dropdown, or modal that gets cut off or tucked behind a sibling container.
You set z-index: 10. Nothing. You try z-index: 9999. Still stuck behind. You slap z-index: 999999 !important in sheer frustration. It still refuses to move forward.
At that point, it feels like CSS is personally defying logic. Or the imposter syndrom start setting in.. But wait! We will figure this out.
z-index is not a single, global scoreboard where the highest number across the entire HTML document wins. It determines stacking order within a local stacking context. And if something is cut off, that can be a clipping problem rather than a stacking-order problem—so we will check for both.
1. When does z-index actually apply?
Before looking at complex stacking layers, rule out the most common beginner gotcha: on a normal, non-flex, non-grid element, z-index does not change the stacking order unless the element is positioned.
/* Does not change the stacking order of a normal static element */.badge {z-index: 10;}/* Works because the element is positioned */.badge--on-top {position: relative;z-index: 10;}
Setting position: relative without specifying top, left, right, or bottom leaves the element in its normal-flow position while allowing z-index to apply. The important exception: a flex or grid item can use z-index while still having position: static. These are direct items of a flex or grid container, not every descendant inside it.
2. The trap: the parent's context sets the boundary
Here is the exact scenario that leads to the 9999 headache: two overlapping parent cards where a child inside Card 1 needs to overlap Card 2.
<div class="board"><div class="card card--first">First card<div class="tooltip">I'm inside the first card</div></div><div class="card card--second">Second card</div></div>
.board {position: relative;width: 24rem;height: 12rem;}.card {position: absolute;top: 1rem;width: 12rem;height: 8rem;padding: 1rem;box-sizing: border-box;}.card--first {left: 1rem;z-index: 1;background: #b9d8f4;}.card--second {left: 7rem;z-index: 2;background: #f4c9a9;}.tooltip {position: absolute;top: 2rem;left: 10rem;z-index: 9999;padding: 0.5rem;background: #222;color: white;}
Even though the tooltip has z-index: 9999, it appears behind .card--second, which has z-index: 2. Because both cards are positioned and have integer z-index values, each card creates a stacking context. The browser compares those card contexts in the board's context: Card 2 is at level 2, above Card 1 at level 1. The tooltip's 9999 is only meaningful inside Card 1's context; it cannot escape that context to outrank Card 2.
3. How can you fix it?
Solution A: raise the parent context
If Card 1 can legitimately sit above Card 2, adjust the stacking order on the parents. This moves the whole first card forward, not just its tooltip.
.card--first {z-index: 3;}.card--second {z-index: 2;}
Solution B: render the overlay outside the trapped parent
For a popup or dropdown that needs to cross component boundaries, render it in a place outside the problematic stacking context. In React, a portal can render an element into another DOM node; Vue has a similar feature called Teleport. You still need to position the overlay, manage focus and keyboard interaction, and make sure its new ancestors do not introduce a different clipping or stacking problem. A portal changes where the element sits in the DOM; it does not automatically solve every layout issue.
Solution C: use the browser's top layer for the right UI
If you are building a true modal, confirmation dialog, or popover, stop fighting z-index altogether. Modern browsers give you a native Top Layer—a special rendering space that sits completely above the rest of the page, ignoring every stacking context below it.
You enter it automatically when you open a native <dialog> using .showModal(), or trigger an element with the HTML popover attribute. Because it bypasses your page's floor-by-floor hierarchy, you never need z-index hacks—though it is meant for genuine overlays, not every small inline tooltip or hover menu.
<button popovertarget="help-popover">Open help</button><div id="help-popover" popover><p>This shown popover is in the browser's top layer.</p></div>
4. What creates a stacking context unexpectedly?
Positioned elements with a z-index are not the only things that create stacking contexts. Common triggers include:
- An element with
opacityless than1 - An element with
transform,filter,perspective, orclip-pathset to anything other thannone - An element with
position: fixedorposition: sticky - An element with
isolation: isolate - An element with
contain: layoutorcontain: paint - A flex or grid item with a
z-indexother thanauto - A container query element with
container-type: sizeorcontainer-type: inline-size
Seeing this list should give you an idea why a z-index problem can appear after a seemingly unrelated style change. A transform used for a hover animation, or opacity: 0.99 used for a fade, can create a new stacking context around the element and its descendants. isolation: isolate also creates a context: it is useful when you want to contain local stacking relationships, but it does not let a child escape its parent's context. So actions have consequences buddy!
5. The modern escape hatch: clipping is a separate problem
If an element is cut off at a container's edge, check for clipping as well as stacking order. For example, overflow: hidden or a clip-path on an ancestor can clip overflowing content. A larger z-index does not undo that clipping. You may need to change the clipping rule or render the overlay outside the clipping ancestor.
If you are building a true modal, a native dialog opened with showModal() can be a better fit than trying to out-number every layer on the page. A popover is another option for suitable non-modal UI. Both use the browser's top layer when shown, which sits above ordinary page content.
A checklist for debugging stubborn layers
Next time an element refuses to come forward, run through this checklist before adding another zero:
- Check whether
z-indexapplies: Make sure the element is positioned (relative,absolute,fixed, orsticky) or is a flex or grid item. - Climb the ancestor chain: Inspect parent containers for properties that silently create a new stacking context (such as
transform,opacity,filter, orz-index). - Compare the nearest competing parents: Find where the two overlapping elements share a common ancestor, and compare the stacking level of those parent groups—not the nested child.
- Check for clipping rules: Verify that an ancestor isn’t cutting the element off with
overflow: hidden,overflow: clip, orclip-path. - Fix the boundary: Raise the correct parent's
z-index, render the overlay outside the trapped container (via a portal), or move it to the browser's Top Layer (<dialog>orpopover) when the UI calls for it.
Hope this was helpful! Writing this gave me flashbacks to the number of times z-index flummoxed me as a beginner—and now I finally get it!
Did this click?
Tap the bulb if this explanation helped.