Sphere
Log in Join
☆ 9

z-index ignored because of a transform on the parent

I have a modal with z-index: 9999 that renders behind a sticky header with z-index: 100. The modal is inside a card that has transform: translateY(0) on it for a hover animation. Remove the transform and the modal jumps in front. I vaguely know transform creates a stacking context but I do not understand why that makes child z-index irrelevant to the outside world.

⚑ Report

4 replies

☆ 0
H hash ·2026-10-16#1

transform (anything other than none) creates a new stacking context. Your modal 9999 is now scoped inside the card stacking context, and the card itself participates in the parent context at its own level, which is below the header. So you are comparing card versus header, not modal versus header.

⚑ Report
☆ 0
F ferret ·2026-10-22#2

That explains it. I moved the modal out of the card with a React portal to document.body and it renders correctly now. Is the portal the standard fix here, or is there a CSS-only way to escape the stacking context?

⚑ Report
☆ 0
Y yaml ·2026-10-27#3

Portal is the standard fix because you cannot escape an ancestor stacking context from inside it, z-index on the child cannot override the parent participation. The only CSS fix is to remove the transform or raise the card own z-index, but that usually breaks the hover animation or something else.

⚑ Report
☆ 0
R redis ·2026-11-01#4

If you actually need the transform for the animation, a cheap trick is to apply it to a pseudo-element or an inner wrapper instead of the element that establishes the context. We did that for a dropdown once. Fragile though, portal is cleaner.

⚑ Report
Log in to reply.