CSS Grid vs Flexbox: When to Use Each
By Nitheesh Rajendran · · 4 min read
Flexbox and CSS Grid are both layout systems, and both are well supported in all current browsers. The question "which one should I use?" has a straightforward answer once you know their central difference:
- Flexbox arranges items along one axis (a row or a column).
- Grid arranges items in two dimensions (rows and columns at once).
That gives a rule of thumb: let the content decide the layout with Flexbox, and let the layout decide where content goes with Grid.
Flexbox: one direction at a time
Set display: flex on a container and its children line up along the main axis.
.toolbar {
display: flex;
align-items: center; /* cross axis */
justify-content: space-between; /* main axis */
gap: 1rem;
}
<div class="toolbar">
<a href="/">Logo</a>
<nav>Links</nav>
<button>Sign in</button>
</div>
Here the logo sits left, the button right, and the links in between, all vertically centred. Each item takes the space its content needs, and you can let some grow:
.toolbar nav { flex: 1; } /* take the remaining space */
Flexbox is ideal for:
- Navigation bars and toolbars
- Centering something horizontally and vertically
- A row of buttons or tags that may wrap
- Distributing space between items of unknown size
Allowing items to wrap onto new lines is done with flex-wrap: wrap, but each line is laid out separately, so items in different lines do not align into columns. That is the point where Grid is the better tool.
Grid: rows and columns together
With display: grid you define the tracks, and items are placed into them.
.cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
gap: 1rem;
}
<div class="cards">
<article>One</article>
<article>Two</article>
<article>Three</article>
<!-- more cards -->
</div>
This creates as many columns of at least 220px as fit in the container, shares the leftover width equally, and keeps rows and columns aligned. No media queries are needed: the number of columns adapts to the available width.
Grid is ideal for:
- Card or image galleries
- Overall page layouts (header, sidebar, main, footer)
- Forms with labels and inputs in aligned columns
- Anything where items need to line up both horizontally and vertically
Named areas make page layouts readable
.page {
display: grid;
grid-template-columns: 220px 1fr;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
min-height: 100vh;
}
.page > header { grid-area: header; }
.page > aside { grid-area: sidebar; }
.page > main { grid-area: main; }
.page > footer { grid-area: footer; }
The CSS shows the layout almost as a drawing. To make the sidebar stack above the content on small screens, redefine grid-template-areas inside a media query.
Comparing them directly
| Flexbox | Grid | |
|---|---|---|
| Dimensions | one at a time | rows and columns |
| Driven by | the content's size | the track definitions |
| Alignment across lines | no (each line is separate) | yes |
| Overlapping items | awkward | simple (place items in the same cell) |
| Best for | components | page and section layouts |
Use them together
They are not rivals. A typical page uses Grid for the overall structure and Flexbox inside the pieces:
.page { display: grid; grid-template-columns: 220px 1fr; }
.card { display: flex; flex-direction: column; gap: .5rem; }
.card footer { margin-top: auto; } /* pushes the footer to the bottom */
The last line is a handy trick: inside a column flex container, margin-top: auto pins an element to the bottom, so card footers line up even when the text lengths differ.
Common mistakes
- Using Flexbox with percentage widths to fake a grid. If you are calculating widths to make columns line up, use Grid instead.
- Forgetting
gap. Both layouts supportgap, which replaces most margin hacks. - Fixed column counts.
repeat(3, 1fr)breaks on narrow screens;repeat(auto-fill, minmax(...))adapts. - Content overflow in flex items. Flex items have
min-width: autoand may refuse to shrink; addmin-width: 0(oroverflow: hidden) to allow truncation. - Nesting too deeply. If a layout needs many wrapper elements, try Grid first; it often needs fewer.
A quick decision guide
- Is it a single row or column of items? Flexbox.
- Do items need to align in both directions? Grid.
- Is the size determined by the content? Flexbox.
- Is the size determined by a layout you plan? Grid.
- Not sure? Build it with Grid; switch to Flexbox for a single-axis component inside it.
Summary
Flexbox is for components that flow in one direction; Grid is for layouts that need rows and columns. Learn gap, justify-content/align-items, repeat(auto-fill, minmax()) and grid-template-areas, and you can build almost any layout without frameworks or hacks.