Learn CSS Visually - CSS explained one diagram at a time - flexbox, grid, the box model and more

Flexbox or Grid: Which Should I Use and When?

By · Updated

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.

Decision diagram for flexbox vs CSS Grid: three questions in a column - do items need to line up in rows and columns at once, are you setting widths on flex items just to line them up with the row above, should items overlap or span rows or columns - where any yes leads to CSS Grid (two-dimensional, layout in) and three no answers lead to flexbox (one row or column, content out).
Three questions settle flexbox vs Grid for almost any layout. Criteria from the W3C Grid specification and MDN Web Docs.

What the diagram asks, in plain text:

  1. 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.
  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.
  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.

Flexbox vs CSS Grid — compiled by Learn CSS Visually from the W3C CSS Grid Layout Module Level 1 (Candidate Recommendation Draft, 26 March 2025) and MDN Web Docs, November 12, 2026
QuestionFlexboxCSS Grid
How many dimensions?one: a row or a columntwo: rows and columns at once
Which comes first?the content (“content out”, bottom-up)the layout (“layout in”, top-down)
Turn it on withdisplay: flexdisplay: grid
Main sizing toolsflex-grow, flex-shrink, flex-basis on the itemsfr, minmax(), repeat() on the container
When items wrapeach new line shares out its own spaceevery row uses the same columns
Spanning rows or columnsnoyes, e.g. grid-column: span 2
Overlapping itemsonly with tricks (negative margins, position)yes: place two items in the same grid area
Alignment and gapjustify-content, align-items, gapthe same, plus justify-items
Best atnavbars, toolbars, tags, the inside of a cardpage 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: auto on 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 with margin-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: absolute needed.

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.

Flexbox or Grid for 12 common layouts — compiled by Learn CSS Visually from the W3C CSS Grid Layout Module Level 1 (26 March 2025) and MDN Web Docs’ grid and flexbox guidance, November 12, 2026
LayoutPickWhy
Navbar: logo left, links rightFlexboxone row; margin-left: auto pushes the links right
Header with a truly centered logoGrid1fr auto 1fr keeps the middle column centered
Button group or toolbarFlexboxone row, sized by the buttons
Tags or chips that wrapFlexboxeach tag as wide as its text
Inside a card (image, text, button)Flexboxone column; margin-top: auto pins the button
Avatar next to textFlexboxone row, two items
Centering one boxEitherplace-items: center or justify-content + align-items
Page: header, sidebar, main, footerGridrows and columns at once; grid-template-areas
Card gallery or product gridGridcolumns line up in every row
Form: labels beside inputsGridthe label column lines up for every field
Dashboard with wide widgetsGridspanning rows and columns
Headline over a hero imageGridoverlap 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.

To-scale comparison of five cards in a 700px container: with flexbox flex-wrap and flex: 1 1 200px the first row holds three 220px cards and the last row's two cards stretch to 340px each, while with CSS Grid repeat(auto-fill, minmax(200px, 1fr)) both rows use the same three 220px columns, leaving an empty third cell in row two.
Same five cards, same 700px box. Flexbox sizes each row from its own items; Grid sizes the columns once for every row.
Card widths for five cards in a 700px container with a 20px gap — calculated by Learn CSS Visually from the flexbox and grid sizing rules (W3C CSS Flexible Box Layout Level 1 and CSS Grid Layout Level 1), November 12, 2026
SetupRow 1 cardsRow 2 cardsRow 2 lines up with row 1?
flex-wrap: wrap + flex: 1 1 200px3 x 220px, at 0, 240, 480px2 x 340px, at 0, 360pxno
repeat(auto-fill, minmax(200px, 1fr))3 x 220px, at 0, 240, 480px2 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; }

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)
gap in 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

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.

How this was written: Written from the W3C CSS Grid Layout Module Level 1 (Candidate Recommendation Draft, 26 March 2025), MDN Web Docs’ guide to grid and other layout methods, Google’s web.dev Learn CSS course, the Can I use support data (updated November 11, 2026) and the Chrome DevTools documentation, all read on November 12, 2026. The card widths in the wrap comparison are our own arithmetic from the flexbox and grid sizing rules. Code samples are simplified for learning; check MDN before shipping newer features such as subgrid.

Oh, one more thing…

If this helped, you’ll like our free updates: the insights we discover while researching, and the moments worth sharing with other people learning CSS — no spam, no promotions.

One short email when there’s something worth your time. Unsubscribe any time.