display
Flex or grid
Flexbox sizes from the content out: each item proposes a size, and the line shares out what is left or what is missing. Grid sizes from the container in: you define tracks, and items are placed into them. So flex suits a run of things whose sizes should follow their content, and grid suits a structure whose sizes should hold regardless of content.
Use it for
- Flex: toolbars, nav rows, tag lists, a label beside an input, anything that should wrap like text.
- Grid: page shells, card galleries, forms with aligned columns, anything that must line up in two directions.
- Grid for overlap: several items placed in the same cell stack without position: absolute, and the cell sizes to the largest.
- Both together is normal — a grid of cards whose insides are flex.
Watch for
- A wrapping flex container is not a grid. Each line is laid out independently, so items in the last row stretch differently from the rows above.
- Reaching for grid to get gap is no longer necessary: gap works in flexbox.
- Neither model reorders for accessibility. order and grid placement change what is seen, not the tab or reading order.
.toolbar { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.gallery { display: grid; gap: 1rem;
grid-template-columns: repeat(auto-fill, minmax(14rem, 1fr)); }
.stack > * { grid-area: 1 / 1; } /* overlap without absolute */