Flexbox or grid?

Topic 1 of 2 in Choosing a layout tool6 min readBeginnerFree course

Overview

Flexbox and grid overlap: both can make a row of items, both can centre things, both can wrap. So which should you use? The answer is rarely a matter of taste — each has a clear strength, and real interfaces use both, often nested inside each other.

Flexbox, content out: one dimension — a row or a column; item sizes come from their content; wrapped lines are independent. Use for nav bars, button groups, tags and media objects. Grid, layout in: two dimensions — rows and columns; the container defines the tracks; items line up in both directions. Use for page layouts, card grids, forms and dashboards. They combine: a grid page layout, with flexbox inside the header and each card.

Content out vs layout in

The most useful way to think about the difference:

  • Flexbox works from the content out. Each item has a size based on its content; the container arranges them along a line and shares out extra space. You describe the items.
  • Grid works from the layout in. The container defines tracks; items are placed into them. You describe the structure.

That’s why wrapped flex lines don’t align into columns — each line only knows about its own items — while every row of a grid shares the same columns.

Four questions

  1. Do items need to line up in two directions? Rows and columns — card grids, galleries, forms, page layouts → grid.
  2. Should each item be as big as its content? Tags, buttons, nav links, chips → flexbox.
  3. Is it a single row or column with space distributed between items? Toolbars, headers, button groups → flexbox.
  4. Do you want to place things in specific regions or overlap them? Page regions, layered hero images → grid.

When both would work — a simple equal-width row, say — pick whichever reads more clearly. A centred single item works equally well either way.

Side by side

Task Flexbox Grid
Nav bar with logo left, links right ✓ natural (margin-left: auto) possible
Tags that wrap by width ✓ natural awkward
Card grid with aligned columns rows don’t align ✓ natural
Page: header, sidebar, main, footer nested containers ✓ one container
Form labels and inputs in columns needs fixed label widths ✓ max-content 1fr
Equal columns flex: 1 repeat(3, 1fr)
Overlapping layers needs positioning ✓ same cell
Footer at the bottom of a card ✓ margin-top: auto ✓ auto 1fr auto rows
Align titles across separate cards can’t ✓ subgrid

They nest beautifully

A typical page combines them like this:

CSS
/* Grid: the page structure */
body {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-height: 100dvh;
}

/* Flexbox: a row of header items */
.site-header { display: flex; align-items: center; gap: 1rem; }

/* Grid: the course cards */
.courses { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(16rem, 100%), 1fr)); gap: 1.5rem; }

/* Grid + subgrid: aligned card internals */
.course-card { display: grid; grid-row: span 3; grid-template-rows: subgrid; }

/* Flexbox: the card's footer row */
.course-card footer { display: flex; justify-content: space-between; align-items: center; }

Each container uses the tool that fits its job. Changing one doesn’t affect the others, because every container only lays out its own children.

Common mistakes

  • Using flexbox for a grid of cards and fighting the last row (which stretches) with max-width hacks and invisible placeholder elements. Use grid.
  • Using grid for a row of tags and getting equal-width boxes that cut off long text or leave gaps. Use flexbox.
  • Setting widths on flex items to fake columns (width: 33.33%) — that’s a grid in disguise, with gap maths to maintain.
  • Making everything a grid “to be safe”. Normal flow is still the right choice for prose — paragraphs, headings and lists don’t need a layout system.

A note on performance

Both are fast. Layout cost depends far more on the number of elements and how often you trigger re-layout than on which algorithm you choose. Pick based on the design, not on performance myths.

Short version: flexbox for lines of things, grid for grids of things and page structure. When in doubt, sketch it: if you draw rows and columns, use grid; if you draw a row of variably sized items, use flexbox.

Exercise

Pick the right tool, five times

For each component in the starter, choose flexbox or grid and implement it: (1) .tags — tags that wrap naturally by their text width; (2) .gallery — 6 thumbnails in aligned columns; (3) .form — labels and inputs where all inputs line up in one column; (4) .toolbar — buttons on the left and a search box pushed right; (5) .app — header, sidebar, main and footer. Add a one-line comment explaining each choice.

HTML
<div class="tags"><a>css</a><a>layout</a><a>container queries</a><a>grid</a><a>a11y</a></div>
<div class="gallery"><img alt="" /><img alt="" /><img alt="" /><img alt="" /><img alt="" /><img alt="" /></div>
<form class="form">
  <label for="n">Name</label><input id="n" />
  <label for="e">Email address</label><input id="e" />
</form>
<div class="toolbar"><button>Bold</button><button>Italic</button><input class="search" placeholder="Search" /></div>
<div class="app"><header>Header</header><nav>Sidebar</nav><main>Main</main><footer>Footer</footer></div>

<style>
  * { box-sizing: border-box; }
  .gallery img { background: #c9d8f8; aspect-ratio: 1; width: 100%; }
  /* your code here */
</style>

Summary

  • Flexbox is one-dimensional and content-out: items size themselves and the container fits them in a row or column.
  • Grid is two-dimensional and layout-in: the container defines tracks and items fit into them.
  • Ask: do items need to line up in both directions? If yes, grid. If they flow like words, flexbox.
  • Most components mix both — a grid page, flex headers, a grid of cards, flex inside each card.
  • Neither is ‘better’; both are fast, accessible and supported everywhere.