Skip to content
Preptima
mediumConceptEntryMidSenior

How do you decide between flexbox and grid, and why does z-index so often not do what you expect?

5 min readUpdated 2026-08-15

Answer

Short answer

Flexbox distributes items along one axis and lets content decide the sizes; grid defines tracks up front and places items into them, aligning in both axes.z-index looks broken because it only orders siblings inside one stacking context, and ordinary properties like transform and opacity create new ones.

Was this answer helpful?

Likely follow-ups

  • A row of flex items with long unbroken text overflows its container instead of shrinking. What is happening and what do you change?
  • Build a card list that fits as many 16rem columns as the viewport allows, with no media queries.
  • A dropdown with a z-index of 9999 renders behind a card with a z-index of 2. Walk me through how you would find the cause.
  • When would you use isolation to scope a subtree instead of adjusting z-index values?

Related questions

Further reading