Parent-Child Context Boundaries
What you'll learn: How stacking contexts create invisible walls that prevent child elements from layering between elements outside their parent's context.
The Isolation Rule
When an element creates a stacking context, it becomes a self-contained layering environment. All its children render together as a single unit, no matter what z-index values they have. Those children cannot slip out and layer between elements in the parent's stacking context.
Think of it like transparent containers: Each stacking context is a sealed plastic box. You can stack boxes on top of each other, but the items inside one box can never interleave with items inside a different box. The box itself determines where all its contents appear in the stacking order.
How This Affects Your Layout
Let's say you have:
- A sidebar with
z-index: 1(creates a stacking context) - Inside the sidebar, a button with
z-index: 9999 - A modal overlay outside the sidebar with
z-index: 10
You might expect the button to appear above the modal because 9999 > 10. But it won't! The button is trapped inside the sidebar's stacking context. The browser compares the sidebar (z-index: 1) with the modal (z-index: 10), and the modal wins. Everything inside the sidebar, including that button, renders below the modal.
Why This Matters
This boundary rule explains many unexpected layering bugs. When a tooltip, dropdown, or popup seems stuck behind something despite having a huge z-index, the problem is usually that its parent created a stacking context. The child can't escape that boundary.
Key Takeaway: Children inside a stacking context are isolated from the outside world—they can only compete with their siblings, never with elements in parent or sibling contexts.