HTML & CSS
/Advanced
z-index & Stacking Context
Definition
The `z-index` property specifies the stack order of an element. An element with greater stack order is always in front of an element with a lower stack order.
Explain Like I'm New
If the X-axis is Left/Right, and the Y-axis is Up/Down, the Z-axis is towards the user's face. If two boxes overlap, the browser needs to know which one goes on top. The one with the higher `z-index` number wins.
Real World Example
A dropdown menu. If you click a dropdown, it must have a high `z-index` so it overlaps the text and images below it, instead of sliding underneath them.
Common Use Cases
- •Modals
- •Dropdowns
- •Sticky Headers
Interactive Example
Interview Questions
basic
- Can you apply a `z-index` to a `position: static` element?
intermediate
- You have a Modal with `z-index: 9999`, but a button with `z-index: 1` is somehow rendering ON TOP of it. Why?