Sub topic 16
Grid or Flexbox: choosing the right tool
This chapter assumes you can write a grid container with grid-template-columns, place items by line or area, and use basic Flexbox properties such as flex-direction, justify-content, and flex. The goal is a decision process you can apply to a real layout, along with the mistakes people make when they pick the wrong tool.
The core difference
Flexbox lays items out along one axis at a time. You pick a direction (a row or a column), and items flow along it. If items wrap onto a second line, each line is sized on its own and does not know about the other lines.
Grid lays items out on two axes at once. The container defines rows and columns first, and items are then placed into the cells those tracks create. Every row can see every column.
A short way to remember it: Flexbox starts from the content and lets the layout follow. Grid starts from the layout and lets the content fill it.
| Question | Flexbox | Grid |
|---|---|---|
| Dimensions controlled | One at a time | Two together |
| Who drives sizing | Item content, by default | Track definitions, by default |
| Wrapped items | Each line sized separately | Items share the same column tracks |
| Overlap and layering | Awkward | Natural, with shared cells |
| Typical use | Components | Page and section layouts |
Seeing the difference in wrapping
The clearest way to feel the difference is to wrap the same items in both systems.
.flex-list {
display: flex;
flex-wrap: wrap;
gap: 12px;
}
.flex-list > li {
flex: 1 1 140px;
}
.grid-list {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
gap: 12px;
}Both lists wrap as the container narrows. With the Flexbox version, the last line holds fewer items, and flex: 1 1 140px lets those items grow to fill the whole line. A lone item on the last row can end up much wider than the items above it. With the Grid version, every item stays in a shared column, so the last row keeps the same column widths as the rows above.
If you want the last row to stretch, Flexbox does that for free. If you want items to line up in columns, use Grid.
A decision process
Work through these questions in order. Stop at the first clear answer.
- Do you need items to align in both rows and columns? Use Grid.
- Do you need to place an item in a specific cell, or make it span rows or columns? Use Grid.
- Is the layout a single row or a single column of items, where the content decides the sizes? Use Flexbox.
- Do you want items to wrap and then fill each line however they fit? Use Flexbox.
- Do you need items to overlap? Use Grid, because several items can share a cell.
Content-out and layout-in
The sizing direction explains most of the surprises you will meet.
In Flexbox, an item's base size comes from its content or its flex-basis. Free space is then shared out using flex-grow and flex-shrink. Change the text in one item and the others shift around it.
In Grid, the tracks are set on the container. Items fit into tracks. Changing the text in one item changes the layout only if a track uses content-based sizing, such as auto, min-content, or minmax() with an auto limit.
This is why a navigation bar suits Flexbox. The links are different lengths and you want them to sit side by side with natural widths. A photo gallery suits Grid, because you want uniform columns no matter what each caption says.
Common mistakes
Using Flexbox for a two-dimensional layout
Teams often build a card grid with flex-wrap and percentage widths, then fight with margins and the last row. Switching to Grid removes the width arithmetic and the margin hacks.
Using Grid for a simple one-axis row
A toolbar with a few buttons does not need tracks. You will end up writing grid-auto-flow: column and fighting track sizes that Flexbox would have handled by default.
.toolbar {
display: flex;
align-items: center;
gap: 8px;
}
.toolbar .spacer {
margin-inline-start: auto;
}The margin-inline-start: auto trick pushes everything after the spacer to the far end. This pattern is short in Flexbox and clumsier in Grid.
Expecting `justify-content` to behave the same way
In Flexbox, justify-content distributes items along the main axis. In Grid, it positions the whole set of tracks inside the container, and only has an effect when the tracks do not fill the container. If your tracks use 1fr, they fill the space and justify-content appears to do nothing.
Forgetting that `fr` and `flex` are different
1fr shares leftover space between tracks, but a track's minimum is auto, so a long unbreakable string can push a track wider than you expected. The fix is minmax(0, 1fr). Flex items have a similar rule, where min-width: auto stops them from shrinking below their content size. The fix there is min-width: 0.
.grid {
display: grid;
grid-template-columns: minmax(0, 1fr) minmax(0, 2fr);
}
.flex-child {
min-width: 0;
}Combining the two
Real pages use both. A common arrangement is Grid for the outer structure, and Flexbox inside each region for the components.
.page {
display: grid;
grid-template-columns: 220px 1fr;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
min-height: 100vh;
}
.page > header {
grid-area: header;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 1rem;
}
.page > aside { grid-area: sidebar; }
.page > main { grid-area: main; }
.page > footer { grid-area: footer; }Here the page frame needs rows and columns to agree, so it uses Grid. The header holds a logo and some links in a single row, so it uses Flexbox. A grid item can itself be a flex container, and a flex item can be a grid container. The two systems nest freely.
Edge cases and trade-offs
- Unknown item counts. Flexbox handles an unknown number of items in a single line gracefully. Grid also handles it, using the implicit grid, but you must choose
grid-auto-rowsorgrid-auto-columnsto keep sizes predictable. - Alignment across siblings. If cards each contain a title, text, and a button, and you want all the buttons to line up across cards, Flexbox inside each card cannot coordinate with its neighbors. Subgrid can, because child items join the parent's tracks.
- Source order and accessibility. Both systems can change the visual order with
orderor explicit placement. Keyboard focus and screen readers still follow the source order, so avoid large mismatches. - Browser support. Grid, Flexbox, and
gapfor both are supported in all current major browsers. Subgrid is newer, so check support for your audience before relying on it. - Both can work. Many layouts, such as a row of three equal cards, work in either system. Pick the one that matches how you expect the layout to change. If you may later add a second row or span an item, choose Grid.
Exercises
Exercise
A site footer has a logo on the left, a list of links in the middle, and a copyright line on the right, all in one row with different content widths. Which tool fits best, and why?
Show answer
Flexbox. The layout runs along one axis and the item widths come from their content. Use display: flex with justify-content: space-between and align-items: center.
Exercise
You build a product list with display: flex; flex-wrap: wrap and flex: 1 1 200px on each product. On the last row, a single product stretches across the whole width. How do you get consistent column widths, and what do you change?
Show answer
Switch to Grid. Use display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)) on the container and remove the flex declaration from the items. With auto-fill, the last row keeps the same column widths as the rows above it.
Exercise
A grid has grid-template-columns: 1fr 1fr. One cell contains a very long URL with no spaces, and the layout overflows its container. Why does this happen, and how do you fix it?
Show answer
The minimum size of an fr track is auto, so the track will not shrink below the width of its longest unbreakable content. Replace the tracks with minmax(0, 1fr) minmax(0, 1fr), and optionally add overflow-wrap: anywhere to the cell content.