What You'll Learn
You'll discover that flex and grid items automatically create stacking contexts when given a z-index value—even without any position property at all.
The Surprising Rule for Flex and Grid Items
In the traditional CSS world, you learned that z-index only works on positioned elements (relative, absolute, fixed, or sticky). But flex items (direct children of a display: flex container) and grid items (direct children of a display: grid container) break this rule.
When you apply z-index to a flex or grid item, it doesn't need position: relative or any other positioning value. The z-index just works, and it also creates a brand-new stacking context for that item.
Why This Matters
Think of flex and grid containers as special "organized environments" where the browser already treats their children differently from normal flow. Just like how positioned elements get special treatment with layering, flex and grid items get the same privileges—they can layer on top of each other using z-index without needing to be positioned first.
This means inside a flexbox or grid layout, you have immediate control over which items appear above others, making overlapping effects simpler to achieve.
Example Scenario
Imagine three cards in a flex container. By default, they sit side-by-side. If you give the middle card z-index: 2, it will now appear above its siblings—no position: relative required. That middle card also becomes a stacking context, meaning any positioned descendants inside it will stack relative to that card, not the entire page.
Key Takeaway: Flex and grid items create stacking contexts when assigned z-index, giving you layering control without positioning values.