Choosing Between Flexbox and Grid

Intermediate · 10 min

A practical way to pick between Flexbox and Grid that goes past one dimension versus two, based on whether content or layout should be in charge.

Before thisMulti-Column LayoutFlexbox FundamentalsGrid Fundamentals and Tracks

Kevin Powell · 10 min

Choosing Between Flexbox and Grid

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Kevin PowellReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • The slogan "flexbox for one dimension, grid for two" is a starting point only. Flexbox can wrap into two dimensions and grid works well for a single row.
  • A better question is who is in charge. In flexbox, the content decides sizes and the container distributes the leftover. In grid, the container defines tracks first and content is fitted into them.
  • Flexbox lines are independent: in a wrapped row of five `flex: 1 1 200px` items in 700px, the last row's two items measured 350px each, wider than the first row's 233px. A grid keeps all five at 233px.
  • Pick grid when items should line up in both directions, or when you want to set sizes up front. Pick flexbox when you want items to flow, wrap and share space based on their own size.
  • They combine well: grid for the page or component skeleton, flexbox inside a cell to arrange a toolbar, a button with an icon or a row of tags.

The usual advice, and why it falls short

Everyone learns the rule: flexbox is one-dimensional, grid is two-dimensional. It’s true that a flex container deals with one axis at a time and that grid deals with rows and columns together. But as a decision tool it breaks quickly. Flexbox wraps, so it makes two-dimensional arrangements. Grid can lay out a single row. Following the slogan, you will sometimes pick the wrong tool.

A more useful question is who is in charge, the content or the layout.

Content-out and layout-in

With flexbox, sizes start from the content. Each item has a basis, from its content or its width, and the container hands out leftover space or takes space back. You describe how items should share. The layout is a result of what’s inside.

With grid, you decide the structure first. You define columns and rows, and then you put items in the cells. The content has to fit what you made, and the layout is the same whatever’s inside.

Consider a row of tags. You don’t know how many, or how wide each one is, and you want them to wrap naturally. That is content-out, so flexbox. Consider a pricing page with three plans that should all be the same width, with matching rows of features. You know the structure, so that is layout-in, which is grid.

The wrapping test

The clearest difference is what happens when items wrap. Take five boxes in a 700px container:

.flex { display: flex; flex-wrap: wrap; }
.flex > * { flex: 1 1 200px; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }

In a browser, the flex version made three boxes of 233px on the first line and two boxes of 350px on the second. The second line had no knowledge of the first, so its two items grew to fill it. The grid made all five boxes 233px, with an empty slot at the end. Both are reasonable, depending on the design, and that’s the point of knowing the difference.

This also shows the trap in the 1D/2D rule. This list is one-dimensional content, a series of cards, but a grid is the right choice if you want the columns to be consistent.

Which one to reach for

Use this checklist, and stop at the first yes.

  1. Do items need to line up in both directions? Grid. Flexbox lines don’t align with each other.
  2. Is it a single row or column where items size themselves, with leftover space going to some of them? Flexbox. A navigation bar, a toolbar, a media object.
  3. Do you want to define sizes before you know the content? Grid. Page skeletons and dashboards.
  4. Should items wrap naturally with no column structure? Flexbox. Tags, chips, button groups.
  5. Is this one flowing stream of text or list items that should break across columns? Neither: use multi-column layout.

When it’s still unclear, build it one way and check the behaviour at awkward widths. If you find yourself setting widths on flex items to force a column structure, switch to grid. If you find yourself fighting grid-template-columns to make a row of unequal items, switch to flexbox.

They work together

Nothing says a page uses only one. A common structure is grid for the skeleton and flexbox inside each region:

.page   { display: grid; grid-template-columns: 220px 1fr; gap: 1.5rem; }
.toolbar { display: flex; align-items: center; gap: 0.5rem; }
.toolbar .spacer { margin-left: auto; }

A grid item can itself be a flex container or a grid container. Nesting this way is normal. The alignment properties are shared too, so align-items, justify-content and gap that you learned for flexbox work in grid, with some differences you’ve seen in the lessons on alignment and gap and grid fundamentals.

What the video covers

Kevin Powell’s “Stop the Flexbox for 1D, Grid for 2D layout nonsense” (July 2024, about ten minutes) argues the same point from two examples. His description says flexbox is great at some 2D layouts and grid can be a better choice for some 1D layouts, so the 1D/2D rule shouldn’t be how you decide. Its timestamps cover why grid might be better for some 1D layouts, why flexbox might be better for some 2D layouts, and the point that grid gives you more control on sizing, while flexbox is better for letting the children have control. I could not read a transcript, so I do not describe his examples. The wrapping numbers and the checklist are the prose’s own. The runners-up are the more conventional comparison, from 2021, and a short beginner version from 2025.

Where this fits

This closes the layout-mode lessons of the roadmap: flexbox, grid and multi-column. The next group, responsive design, uses all three at different screen sizes. For a shorter treatment aimed at the whole stack, see the frontend roadmap’s flexbox and grid layout.

Check yourself

  1. 01A wrapped list of cards has `flex: 1 1 200px`. The last row has two cards that are much wider than the cards above. Why, and what fixes it?

    Each flex line distributes its own leftover space independently, so the two cards on the last line grow to fill it. `display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr))` keeps one set of columns for every row, so the last row's cards match the rest.

  2. 02Why is "flexbox is for one dimension" misleading?

    Flexbox can wrap into multiple lines, so it does make 2D arrangements, but each line is laid out separately and doesn't align with the lines above or below. Grid is the one that coordinates both axes at once.

  3. 03You want a toolbar with an icon, a label and a button pushed to the right. Flexbox or grid?

    Flexbox. It is a single row where each item sizes itself from its content, with `gap` for spacing and `margin-left: auto` to push the button. Grid would need you to define columns for something that has no column structure.

  4. 04Where do the two layout modes usually meet in a real page?

    Grid draws the large structure, such as header, sidebar, main and footer, or the card grid. Flexbox is used inside those regions for rows of related things, like navigation links, tag lists and a card's footer.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark