Choose a layout method for the problem at hand.
Ask what needs to line up. Flexbox works along a row or column, with each wrapped line arranged separately. Grid keeps rows and columns aligned together. You can use either for components or page layouts.
| Need | Useful starting point | Reason |
|---|---|---|
| A row of navigation links with different text lengths. | Flexbox. | Items can keep natural widths and wrap independently. |
| A vertical stack with a final action pushed down. | Flexbox. | A column and an auto margin handle one main axis. |
| Cards whose columns must align across rows. | Grid. | Rows share the same column tracks. |
| A header spanning a sidebar and main region. | Grid. | Placement can describe both dimensions together. |
These are starting points rather than exclusive rules. A component's size does not determine which system it should use.
Assume five short articles are direct children of either example container:
.flex-collection {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.flex-collection > article {
flex: 1 1 10rem;
min-width: 0;
}At a 640-pixel content width with a 16-pixel root font size, three cards fit on the first line and two on the second. Each line distributes its own remaining width, so the two cards on the second line grow wider than the first three.
.grid-collection {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 1rem;
}Here, all rows use the same three column tracks. The two cards on the final row keep those column widths and leave the third cell unoccupied. This numeric template always keeps three columns; use automatic repetition when the count should adapt.
The question is whether each line should distribute its own space or whether several rows must share column boundaries.
A row of buttons should wrap, and each line should distribute its own remaining space. Which layout is a useful starting point?
Flexbox fits this constraint: each flex line distributes space independently. Grid can also wrap items, but shared columns are not required here.
Now change the requirement: cards on every row must share column boundaries. Which layout would you start with, and why?
Grid provides shared column tracks across rows. The key evidence is the alignment requirement, not the fact that the items are cards.
.dashboard {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 14rem), 1fr));
gap: 1rem;
}
.card-actions {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
}The dashboard controls the card tracks. The nested actions container arranges its own buttons. Each layout controls its own direct children, so Grid and Flexbox can work together here.
Try it yourself
HTML + CSS