Skip to content

QuizNot attempted

Stacking contexts

A modal has z-index: 9999 but appears UNDER a header with z-index: 10. What is the likely cause?
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • The modal is inside a parent that creates its own stacking context (e.g. transform, opacity < 1, or position + z-index), and that parent sits below the header
  • z-index only competes within the same stacking context
  • an ancestor with transform, opacity < 1, filter, or positioned + z-index traps its children at the ancestor's own level, so 9999 inside a low context still loses to 10 outside.
  • Not flex items only: z-index works on positioned elements and on flex/grid items.
  • Not a max of 1000: there is no such cap (it is a 32-bit integer).
  • Not sticky: sticky is just another positioned value and doesn't beat a higher z-index on its own.
  • Fix: portal the modal to <body> or use the <dialog> top layer.