Flexbox or grid?
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.
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
- Do items need to line up in two directions? Rows and columns — card grids, galleries, forms, page layouts → grid.
- Should each item be as big as its content? Tags, buttons, nav links, chips → flexbox.
- Is it a single row or column with space distributed between items? Toolbars, headers, button groups → flexbox.
- 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:
/* 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-widthhacks 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.
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.
<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><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%; }
/* 1. Flexbox: sizes come from each tag's text; lines don't need to align */
.tags { display: flex; flex-wrap: wrap; gap: 0.5rem; }
/* 2. Grid: thumbnails must line up in columns and rows */
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(8rem, 1fr)); gap: 0.5rem; }
/* 3. Grid: labels and inputs align across rows (two dimensions) */
.form { display: grid; grid-template-columns: max-content 1fr; gap: 0.75rem 1rem; align-items: baseline; }
/* 4. Flexbox: one row of content-sized items, one pushed to the end */
.toolbar { display: flex; gap: 0.5rem; align-items: center; }
.toolbar .search { margin-inline-start: auto; }
/* 5. Grid: page regions in rows and columns */
.app {
display: grid;
grid-template: "header header" auto "nav main" 1fr "footer footer" auto / 12rem minmax(0, 1fr);
min-height: 20rem;
}
.app header { grid-area: header; }
.app nav { grid-area: nav; }
.app main { grid-area: main; }
.app footer { grid-area: footer; }
</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.