Each parent creates its own stacking context. Z-index only compares
within the same parent.
Parent A (z-index: auto)
Child A1 z-index: 1
Child A2 z-index: 2
Parent B (z-index: auto)
Child B1 z-index: 2
Child B2 z-index: 1
/* Each parent has its OWN stacking context */
/* Child z-index only compares within its parent */
.parent-a { position: relative; }
.parent-b { position: relative; }
/* Children in Parent A cannot overlap children in Parent B */
6. 💡 Practical Use: Modal Overlay
Modal overlay covers everything with high z-index.
The modal card has even higher z-index.
/* Question 1: Which appears on top: z-index: 10 or z-index: 100? */
/* Answer: z-index: 100 (higher number = on top) */ /* Question 2:
Does z-index work on static elements? */ /* Answer: No. Element needs
position: relative/absolute/fixed/sticky */ /* Question 3: What
happens if two elements have the same z-index? */ /* Answer: The one
that appears later in HTML is on top */ /* Question 4: What is a
typical z-index for a modal? */ /* Answer: 1000 or higher */ /*
Question 5: What does z-index: -1 do? */ /* Answer: Puts element
behind its parent */