Flexbox or Grid: Which Should I Use and When?
Disclosure: this page contains Amazon affiliate links. As an Amazon Associate we earn from qualifying purchases, at no extra cost to you.
Hey there, layout decider!
Flexbox vs Grid sounds like a rivalry, but it is really a sorting question: is this thing a line, or is it a grid?
Answer that and the choice makes itself.
Flexbox or Grid is the question that stalls a lot of layouts before the first line of CSS gets written.
The good news: you don’t pick a side forever, you pick per layout, and it only takes three questions.
If you like a printed reference beside your editor, the CSS Visual Dictionary paperback on Amazon draws both layout modes as pictures; the decision diagram below does the rest.
Short answer: Use flexbox when items line up in one direction — a row or a column — and their own sizes should decide the spacing (navbars, button groups, tags). Use CSS Grid when items must line up in rows and columns at the same time (page layouts, card galleries, forms). Most real pages use both: Grid outside, flexbox inside.
What the diagram asks, in plain text:
- Must items line up in rows and columns at the same time? (a page layout, a gallery, a form) Yes: use CSS Grid. No: go to question 2.
- Are you setting widths on flex items just so they line up with the row above? Yes: use CSS Grid. No: go to question 3.
- Should items overlap, or span several rows or columns of a fixed layout? Yes: use CSS Grid. No: use flexbox — one row or column, with sizes coming from the items.
Underneath all three: most pages use both, Grid for the page skeleton and flexbox inside each piece (the navbar, a card, a row of buttons).
What is the difference between flexbox and CSS Grid?
The difference between flexbox and CSS Grid is dimensions: flexbox lays items out along one axis at a time, while CSS Grid controls rows and columns together.
The W3C CSS Grid Layout Module Level 1, the CSS Working Group’s specification, says so in its introduction:
Unlike Flexible Box Layout, which is single-axis–oriented, Grid Layout is optimized for 2-dimensional layouts: those in which alignment of content is desired in both dimensions.
MDN Web Docs puts it in plainer words: flexbox was designed for layout in one dimension — either a row or a column
, and Grid was designed for two-dimensional layout — rows, and columns at the same time
.
Here is the whole comparison in one table.
| Question | Flexbox | CSS Grid |
|---|---|---|
| How many dimensions? | one: a row or a column | two: rows and columns at once |
| Which comes first? | the content (“content out”, bottom-up) | the layout (“layout in”, top-down) |
| Turn it on with | display: flex | display: grid |
| Main sizing tools | flex-grow, flex-shrink, flex-basis on the items | fr, minmax(), repeat() on the container |
| When items wrap | each new line shares out its own space | every row uses the same columns |
| Spanning rows or columns | no | yes, e.g. grid-column: span 2 |
| Overlapping items | only with tricks (negative margins, position) | yes: place two items in the same grid area |
Alignment and gap | justify-content, align-items, gap | the same, plus justify-items |
| Best at | navbars, toolbars, tags, the inside of a card | page skeletons, galleries, forms, dashboards |
What do “content out” and “layout in” mean?
“Content out” and “layout in” are MDN’s second test for flexbox vs Grid. MDN writes that Flexbox works from the content out
: you hand flexbox a set of items, and each item’s size decides how much room it gets.
And Grid works from the layout in
: you draw the tracks first, then place items into them, or let auto-placement fill the cells in order. The W3C Grid spec describes the same split: Grid uses a top-down approach to layout
, while flexbox uses a simpler bottom-up approach
.
When should you use flexbox?
Use flexbox when your items sit in one row or one column and you want their content to set the spacing. Google’s web.dev Learn CSS course calls flexbox a layout model designed for one-dimensional content
. Typical flexbox jobs:
- A navbar with the logo on the left and the links on the right (
margin-left: autoon the first link pushes the group over). - A row of buttons or toolbar icons, spaced with
gap. - Tags or chips that wrap onto new lines, each one only as wide as its text.
- The inside of a card: image, text and a button stacked with
flex-direction: column, the button pinned to the bottom withmargin-top: auto. - A media object: an avatar next to a block of text.
Our flexbox explainer draws the main axis and cross axis if you want the basics first.
When should you use CSS Grid?
Use CSS Grid when things must line up in two directions at once, or when you’d rather draw the layout first and drop content into it.
The W3C spec lists Grid’s specialties: it enforces 2-dimensional alignment
, allows explicit overlapping of items, and has more powerful spanning capabilities
. Typical CSS Grid jobs:
- A page skeleton — header, sidebar, main, footer — drawn with
grid-template-areas(how named grid areas work). - A card gallery or product grid where every column lines up, row after row.
- A form with a column of labels and a column of inputs.
- A dashboard where some widgets span two columns or two rows.
- Text on top of an image: both placed in the same grid area, no
position: absoluteneeded.
MDN also gives a handy warning sign: If you are using flexbox and find yourself disabling some of the flexibility, you probably need to use CSS grid layout.
That’s question 2 in the diagram. Our CSS Grid guide covers lines, tracks and areas.
Which should you use for navbars, cards, forms and other common layouts?
For a single row or column of content-sized items, use flexbox; for anything that has to line up in a grid of rows and columns, use CSS Grid; for centering one box, either works.
Here are twelve everyday layouts sorted with the three questions from the diagram.
| Layout | Pick | Why |
|---|---|---|
| Navbar: logo left, links right | Flexbox | one row; margin-left: auto pushes the links right |
| Header with a truly centered logo | Grid | 1fr auto 1fr keeps the middle column centered |
| Button group or toolbar | Flexbox | one row, sized by the buttons |
| Tags or chips that wrap | Flexbox | each tag as wide as its text |
| Inside a card (image, text, button) | Flexbox | one column; margin-top: auto pins the button |
| Avatar next to text | Flexbox | one row, two items |
| Centering one box | Either | place-items: center or justify-content + align-items |
| Page: header, sidebar, main, footer | Grid | rows and columns at once; grid-template-areas |
| Card gallery or product grid | Grid | columns line up in every row |
| Form: labels beside inputs | Grid | the label column lines up for every field |
| Dashboard with wide widgets | Grid | spanning rows and columns |
| Headline over a hero image | Grid | overlap by sharing one grid area |
Why does the last row stretch with flexbox but not with Grid?
The last row stretches with flexbox because every wrapped flex line shares out its own free space, while CSS Grid sizes its columns once for every row.
MDN explains that when flex items wrap, each new row (or column when working by column) is an independent flex line in the flex container
.
To see what that means in pixels, we put the same five cards in a 700px container with a 20px gap, once with flexbox and once with Grid, and worked out every width.
| Setup | Row 1 cards | Row 2 cards | Row 2 lines up with row 1? |
|---|---|---|---|
flex-wrap: wrap + flex: 1 1 200px | 3 x 220px, at 0, 240, 480px | 2 x 340px, at 0, 360px | no |
repeat(auto-fill, minmax(200px, 1fr)) | 3 x 220px, at 0, 240, 480px | 2 x 220px, at 0, 240px (third cell empty) | yes |
The flexbox math: three 200px cards plus two 20px gaps is 640px, which fits in 700px (a fourth card would need 860px), so line 1 holds three cards and shares the leftover 60px — 20px each, making 220px.
Line 2 holds the last two cards: 400px plus one 20px gap is 420px, leaving 280px, so each card grows by 140px to 340px.
The Grid math: with auto-fill, the W3C spec makes the number of columns the largest possible positive integer that does not cause the grid to overflow the content box of its grid container taking gap into account
.
Here that’s three, because 3 x 200px + 2 x 20px = 640px fits and four would not.
The 660px left after the two gaps splits into three 1fr columns of 220px each, and row 2 reuses those same columns.
/* flexbox: the last row stretches */
.row { display: flex; flex-wrap: wrap; gap: 20px; }
.row > * { flex: 1 1 200px; }
/* grid: every row shares the same columns */
.grid {
display: grid;
gap: 20px;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
}
Can you stop the last flex row from stretching?
You can stop the last flex row from stretching only by turning off flexbox’s growing: flex: 0 1 200px keeps every card at 200px, but then row 1 no longer fills the container either (here it leaves 60px empty on the right).
Capping widths with max-width has the same trade-off. That’s exactly the “disabling the flexibility” sign MDN describes — when you want every row to share the same columns, switch the container to CSS Grid.
Can you use flexbox and Grid together?
Yes — flexbox and CSS Grid are meant to be mixed. Any grid item can itself be a flex container, and any flex item can be a grid container, so the usual pattern is CSS Grid for the page and flexbox inside each area.
Little confession: just use Grid for everything
and just use flexbox for everything
are both tempting, and both make some layouts harder than they need to be. The fun part is mixing them — Grid draws the room, flexbox arranges the furniture.
.page {
display: grid;
grid-template-columns: 240px 1fr;
grid-template-areas:
"header header"
"sidebar main";
}
.site-header {
grid-area: header;
display: flex; /* a grid item that is also a flex container */
align-items: center;
gap: 16px;
}
.site-header .login { margin-left: auto; }
Should a navbar use flexbox or Grid?
Most navbars should use flexbox, because a navbar is one row of items sized by their text.
The one case where CSS Grid wins is a header with a logo that must sit in the exact center.
With flexbox, a wider left side pushes the logo off-center, while Grid’s 1fr auto 1fr gives both sides equal columns (as long as both sides fit), so the middle column stays centered.
/* flexbox: logo left, links right */
.nav { display: flex; align-items: center; gap: 16px; }
.nav .links { margin-left: auto; }
/* grid: logo dead center, menu left, account right */
.header {
display: grid;
grid-template-columns: 1fr auto 1fr;
align-items: center;
}
.header .account { justify-self: end; }
Both versions use the same alignment properties; our justify-content vs align-items guide explains which axis each one moves.
Is flexbox outdated now that CSS Grid exists?
No — flexbox is not outdated; CSS Grid was designed to work next to it, not to replace it. The W3C Grid specification says it plainly: It is expected that both will be valuable and complementary tools for CSS authors.
Both layout modes share the same Box Alignment properties and the same gap property, and flexbox is still the simpler tool for one row or column of content-sized items.
What is subgrid, and does it change the choice?
Subgrid lets a nested grid borrow its parent’s rows or columns, so the titles, text and buttons inside separate cards line up across the whole row — something neither flexbox nor a plain grid can do.
It’s part of the W3C CSS Grid Layout Module Level 2, and it makes the case for Grid on card layouts even stronger.
.cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 20px;
}
.card {
display: grid;
grid-row: span 3; /* title, text, button */
grid-template-rows: subgrid; /* share the parent's rows */
}
According to the Can I use data for CSS subgrid (updated November 11, 2026), subgrid works for 94.68% of users worldwide: Chrome and Edge 117, Firefox 71 and Safari 16 onward.
How can you tell whether an element uses flexbox or Grid?
Open your browser’s developer tools and look for the small layout badges in the Elements panel.
The Chrome DevTools documentation says that when an element has display: grid or display: inline-grid, you can see a grid badge next to it in the Elements panel
; flex containers get a “flex” badge the same way.
Clicking the badge draws an overlay of the lines and tracks on the page. Firefox has the same idea in its Layout panel, with a Grid Inspector and a Flexbox Inspector.
Do flexbox and CSS Grid work in every browser?
Yes — both flexbox and CSS Grid work in every current browser. The Can I use data, updated November 11, 2026 and read on November 12, 2026, shows:
- Flexbox
- 97.4% of users worldwide; unprefixed since Chrome 29, Edge 12, Firefox 28 and Safari 9
- CSS Grid
- 97.39% of users worldwide; since Chrome 57, Edge 16, Firefox 52 and Safari 10.1 (all 2017)
gapin flexbox- 97.27%; since Chrome and Edge 84, Firefox 63 and Safari 14.1
- Subgrid
- 94.68%; since Chrome and Edge 117, Firefox 71 and Safari 16
So browser support isn’t a reason to pick one over the other in 2026: choose by the shape of the layout.
Next time you start a layout, run the three questions before you type display. Then build those five cards both ways and watch the last row — once you have seen it stretch in flexbox and snap into place in Grid, you will never wonder again.
Sources and further reading
- W3C: CSS Grid Layout Module Level 1 — the specification (Candidate Recommendation Draft, 26 March 2025): introduction comparing Grid with flexbox, and the auto-fill repeat rule
- W3C: CSS Grid Layout Module Level 2 — subgrid
- W3C: CSS Flexible Box Layout Module Level 1 — flex lines, wrapping and flex-grow
- MDN Web Docs: Relationship of grid layout to other layout methods — one- vs two-dimensional layout, content out vs layout in
- web.dev Learn CSS: Flexbox — Google’s free CSS course
- web.dev Learn CSS: Grid — Google’s free CSS course
- Can I use: CSS Grid Layout — support data updated November 11, 2026; flexbox, flexbox gap and subgrid read from the same data file
- Chrome DevTools: inspect CSS grid — the grid badge and overlay
Questions and answers
Should I learn flexbox or grid first?
Most people find flexbox the easier start, because one row or column is simpler to picture than a whole grid. The alignment properties carry over: justify-content, align-items and gap work in both, and MDN notes that knowing flexbox helps you get to grips with grid. Learn both — most pages use them together.
Can you put flexbox inside CSS Grid?
Yes. Any grid item can also have display: flex, so a common pattern is CSS Grid for the page layout and flexbox inside each area, such as the header or a card. It works the other way round too: a flex item can be a grid container.
Is CSS Grid or flexbox better for responsive design?
Both are responsive. CSS Grid with repeat(auto-fill, minmax(200px, 1fr)) changes the number of columns to fit the screen without a media query and keeps every row lined up, while flexbox with flex-wrap wraps items by their size but lets the last row stretch. Pick Grid when the columns must line up.
Should I use grid or flexbox for a card layout?
Use CSS Grid for the set of cards, so every column lines up from row to row, and flexbox (flex-direction: column) inside each card to stack the image, text and button. Subgrid can line up the titles and buttons across cards in browsers from Chrome 117, Firefox 71 and Safari 16.
Is flexbox outdated?
No. Flexbox and CSS Grid do different jobs, and the W3C Grid specification calls them valuable and complementary tools. Flexbox is still the simpler choice for one row or column of items, like a navbar or a button group, and works for 97.4% of users worldwide.