CSS Flexbox is a one-dimensional layout module that arranges items along a single axis, row or column, and handles component-level alignment. CSS Grid is a two-dimensional layout module that manages rows and columns simultaneously. CSS Flexbox arranges items along a single axis (row or column) and handles component-level alignment: navigation bars, button groups, centered content, tag lists, and vertically stacked card layouts. CSS Grid is a two-dimensional layout system, it manages rows and columns simultaneously and suits page-level structure: header/sidebar/main/footer shells, image galleries, and dashboard grids where items span multiple columns. Flexbox has 99%+ global browser support in 2026; Grid has 97%+. They work together, not against each other. The practical decision rule: if layout runs in one direction only, use Flexbox. If you need control in both directions at once, or need explicit item placement (like grid-column: span 2), use Grid. Most production UIs use Grid for the outer shell and a flex container inside each region for alignment.
Developers argue about this constantly. Some teams ban Grid entirely because "Flexbox does everything" and others go the opposite direction. Both positions are wrong. These two tools were built for different jobs, and knowing which one to reach for first saves real time.
The frustrating part is that picking wrong rarely fails loudly. It fails as a layout that works at every width you checked and collapses at one you did not, and by then the wrong container is three components up.
What Problem Does Flexbox Solve?
Flexbox excels at distributing items along a single axis. It's "one-dimensional" - you work in either a row or a column, not both at once. It became the default for component-level alignment because the math it handles automatically used to require float hacks and clearfix.
/* Classic navbar - items in a row with space between */
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 0 1rem;
}
/* Push the last button to the far right */
.navbar__actions {
margin-left: auto;
}
The mental model for Flexbox: you have a flex container and its child elements inside. You set display: flex on the container, then control alignment properties like justify-content and align-items on both axes. That's it. When developers first picked it up around 2016, it typically replaced a huge share of float-based layout CSS almost overnight. Per MDN Web Docs, Flexbox reached broad browser support years before Grid did, which is part of why it became the default reflex for any alignment problem, even the two-dimensional ones it was never really designed to solve well.
What Problem Does CSS Grid Solve?
Grid is two-dimensional. You define rows and columns using grid-template-areas or explicit track sizes, then place items anywhere in that matrix. It's the right tool when layout in both directions matters simultaneously.
/* Classic page shell with sidebar */
.page {
display: grid;
grid-template-columns: 250px 1fr;
grid-template-rows: 64px 1fr 48px;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
min-height: 100dvh;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }
Try doing this cleanly with Flexbox. You can't - you'd need nested Flexbox containers with carefully managed widths and some fragile height calculations. Grid solves it in 10 lines. According to MDN's own comparison of the two specs, Grid was designed from the outset to manage both axes at once, while Flexbox's single-axis model means a second dimension has to be faked with wrapper elements and manual width math, a workaround that breaks the moment content changes height unexpectedly.
When Should You Use Flexbox?
Reach for Flexbox when you have a list of items that need to line up along a single direction. These patterns are Flexbox territory:
- Navigation bars and tab lists
- Button groups and icon-with-label pairs
- Card content layout (image + title + description stacked vertically)
- Centering a single element inside a container
- Distributing items with equal gaps
/* Tag list that wraps naturally */
.tags {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
}
/* Vertical card layout */
.card {
display: flex;
flex-direction: column;
}
.card__body {
flex: 1; /* grows to fill available space */
}
.card__footer {
margin-top: auto; /* pins to bottom */
}
The flex: 1 and margin-top: auto pattern belongs in almost every card component. It keeps the footer pinned to the bottom regardless of content height, without knowing the card height in advance. Centering is the other job people reach for Flexbox on, though Grid's place-items: center is shorter; I walk through every approach in centering a div in CSS. Five properties carry most of the work: display: flex, flex-direction, justify-content, align-items, and gap, a short enough list to memorize rather than look up each time.
When Should You Use CSS Grid?
Reach for Grid when the layout has meaningful structure in both dimensions. These patterns belong to Grid:
- Full page layouts (header, sidebar, content, footer)
- Image galleries and media grids
- Dashboard card grids with varying column spans
- Form layouts with label and input alignment
- Any layout where you want items to align across rows
/* Responsive card grid - no media queries needed */
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
gap: 1.5rem;
}
That repeat(auto-fill, minmax(280px, 1fr)) pattern is one of CSS's most useful features for responsive layout. Cards automatically reflow from one column to many as space grows. No JavaScript. No breakpoints required. I pull the whole pattern apart, knob by knob, in responsive CSS grid without media queries, including the auto-fit versus auto-fill gotcha that trips up almost everyone.
Grid also has an answer for the one thing nested layouts always got wrong: alignment across separate grids. When card titles and prices refuse to line up row to row, CSS subgrid is the fix, and it's been Baseline in every major browser since late 2023.
For the design-side decisions behind these layouts - when bento grids beat traditional cards, how many tiles a dashboard should display before cognitive load flips negative, and where Subgrid changes the alignment math - see CSS Layout Patterns in 2026 on Art of Styleframe. It's the same problem space approached from the design end of the pipeline.
Placing Items Explicitly
Grid also lets you place specific items in specific cells, which Flexbox simply can't do:
/* Featured article spans two columns */
.article-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
}
.article-grid__featured {
grid-column: span 2;
}
What Is the Practical Decision Rule?
Here's the rule that actually holds up:
One direction only? Use Flexbox. Two directions, or explicit placement? Use Grid.
When I read this rule back against the layouts on this site, the one place it needed a tiebreaker was the topics card grid (its styles have a .topic-span-2 class that does grid-column: span 2): it reads as one direction until a card has to span two columns, and that single requirement is what makes it Grid. If you find yourself reaching for flex-basis percentages to line items up with the row above, you have already left Flexbox's job description.
When you're unsure, start with Flexbox. If you find yourself adding nested Flexbox containers with flex-wrap to fake grid-like behavior, that's the sign to switch to Grid. A responsive layout built with Grid's auto-fill is almost always cleaner than the Flexbox workaround.
How to decide in practice:
- Sketch the layout and ask whether it needs control in one direction or two.
- If one direction, reach for Flexbox first, it is simpler for the common cases.
- If you catch yourself nesting Flexbox containers to fake row/column alignment, switch to Grid.
- Use both together: Grid for the page shell, Flexbox for alignment inside each region.
They also stack well. A common real-world pattern:
/* Grid for the page skeleton */
.app {
display: grid;
grid-template-columns: 240px 1fr;
grid-template-rows: 60px 1fr;
}
/* Flexbox inside each cell for alignment */
.topbar {
display: flex;
align-items: center;
justify-content: space-between;
}
.sidebar-nav {
display: flex;
flex-direction: column;
gap: 0.25rem;
}
Outer structure with Grid. Inner alignment with Flexbox. This combination handles the majority of real UI layouts without reaching for any third tool.
Where do Grid and Flexbox actually behave differently?
The decision rule above is about intent. It says nothing about what the two do when content misbehaves, and that's where layouts break in production. So I measured it. Playwright 1.62.1 drove Chromium 151.0.7922.34, Firefox 153.0 and WebKit 26.5 on an Apple M1, and every number below is read back from real layout with getBoundingClientRect() and scrollWidth, not worked out from the CSS. Each group ran twice in fresh browsers. The script asserts all of the layout results (163 checks), so a browser release that changes an answer fails the run.
Does 1fr protect you from a long word better than flex: 1?
No. This is the test I'd run before believing any claim that Grid is sturdier. Three columns in a 600 px container, a 634 px unbreakable word in the middle one:
| Layout | left / middle / right px | container overflow px |
|---|---|---|
flex: 1 | 48 / 634 / 48 | 130 |
grid 1fr 1fr 1fr | 48 / 634 / 48 | 130 |
flex: 1 + min-width: 0 | 200 / 200 / 200 | 234 |
grid minmax(0, 1fr) | 200 / 200 / 200 | 234 |
flex: 1 + overflow: hidden | 200 / 200 / 200 | 0 |
flex: 1 + overflow-wrap: break-word | 48 / 634 / 48 | 130 |
flex: 1 + overflow-wrap: anywhere | 200 / 200 / 200 | 0 |
flex: 1 and grid 1fr produce the same three widths to 0.01 px. Both treat a track or item as no smaller than its min-content size, so the word forces the middle one to 634 px and squeezes its neighbors to the width of "short". Same blowout, same engines, same numbers. The popular fix, min-width: 0 or minmax(0, 1fr), gives you three tidy 200 px columns, and then the word pokes 234 px out of the container because nothing told it to break. That's the row people miss: equal columns, still overflowing.
min-width: 0 and minmax(0, 1fr) equalize the columns but leave the content overflowing; only overflow: hidden, overflow-wrap: anywhere or max-width: 100% actually remove the overflow.
What removes the overflow? overflow: hidden clips it. overflow-wrap: anywhere lets the word break and, unlike break-word, also lowers the min-content size, so it works with no min-width: 0 at all. break-word leaves the blowout exactly where it was (130 px), which is the single most surprising line in the table.
The unsized image behaves the same way. A 1200x600 image in the middle item gives 48 / 1200 / 48 and 696 px of overflow in both models, and min-width: 0 turns that into 200 / 200 / 200 with 800 px of overflow. Adding max-width: 100% to the image fixes both at once (200 / 200 / 200, zero overflow). One flex-only trap showed up here: with flex: 1 1 auto and that same max-width: 100% image, the middle item came out 484 px wide against 58 px for its neighbors. The grid layouts I tested kept the same image at 200 px.
All three engines agreed within about 2 px, and the gap is font metrics: the word is 633.7 px in Chromium and WebKit and 635.8 px in Firefox.
Do flex-wrap and grid auto-fill disagree on columns?
There was no difference in column count. I swept the container from 320 to 1280 px in 8 px steps, 121 widths, with eight items, a 16 px gap and a 200 px minimum. Flex-wrap with flex: 1 1 200px, flex-wrap with flex: 0 0 200px and grid repeat(auto-fill, minmax(200px, 1fr)) produced the same column count at every width, and the same first-row item width for the growing variants, in all three engines. The two models agree until the last row.
At 700 px the eight items fall into rows of 3, 3 and 2. Here's what the last row looks like:
| Layout | Last row widths |
|---|---|
flex-wrap, flex: 1 1 200px | 342 + 342 px |
flex-wrap, flex: 0 0 200px | 200 + 200 px |
grid auto-fill / auto-fit | 222.67 + 222.67 px |
A growing flex row stretches its two leftover items to 342 px, wider than any item above them. Grid keeps them at the column width. If your design wants a card grid that looks like a grid, that single row is often the deciding factor.
How much slower is Grid for thousands of items?
I forced a layout pass by changing the container's width and reading offsetHeight, ten passes per sample and nine samples per run. Milliseconds per layout at 8,000 items, mean of two passes:
| Engine | flex-wrap | grid auto-fill | flex, one row | grid, one row |
|---|---|---|---|---|
| Chromium | 15.0 | 17.4 | 4.2 | 6.2 |
| Firefox | 14.5 | 26.8 | 8.3 | 22.5 |
| WebKit | 25.1 | 53.9 | 30.6 | 57.2 |
Treat the absolute numbers as noise. The CPU was shared with other jobs the whole time (load average 24 to 36 on 8 cores), and the milliseconds moved by up to a third between complete runs. The ratios held across two complete runs: grid auto-fill cost 1.07x to 1.15x a wrapping flex container in Chromium, 1.77x to 1.84x in Firefox and 2.15x to 2.32x in WebKit. A single grid row cost 1.5x to 2.7x a single flex row everywhere.
So is Grid slow? No. At 500 items every layout in every engine took about 4 ms or less. You'd need an unvirtualized list in the thousands before this matters, and at that point the fix is windowing, not a different display value.
The test has limits. It times layout of plain text items, not paint or real components, and Firefox's timer readings come out on a 0.1 ms grid. Text direction is left to right. The script and its assertions are in the css-grid-flexbox-cross-engine example; npm test reprints every table on your machine.
How Do CSS Grid and Flexbox Properties Compare Side by Side?
Before the pattern list, here's a quick reference for the properties you'll actually use:
| Property area | CSS Grid | Flexbox |
|---|---|---|
| Axes | Two (rows + columns simultaneously) | One (row or column, not both) |
| Defining structure | grid-template-columns, grid-template-rows | flex-direction |
| Item sizing | fr units, minmax(), auto | flex-grow, flex-shrink, flex-basis |
| Responsive layout | auto-fill/auto-fit + minmax() | flex-wrap |
| Gaps | gap, row-gap, column-gap | gap (supported since 2021) |
| Explicit placement | grid-column, grid-row, grid-area | Not supported, order only |
| Named regions | grid-template-areas | Not supported |
| Alignment | justify-items, align-items, place-items | justify-content, align-items |
| Browser support (2026) | 97%+ globally | 99%+ globally |
Neither has a meaningful browser support gap anymore. Pick on fit, not on support.
Which Tool Fits Common Layout Patterns?
| Pattern | Best tool | Why |
|---|---|---|
| Horizontal nav with logo left, links right | Flexbox | Single axis, space-between |
| Page shell (header/sidebar/main) | Grid | Two-dimensional structure |
| Responsive card grid | Grid | Auto-fill handles reflow |
| Vertically centered modal | Flexbox or Grid | Both work; Grid's place-items: center is shorter |
| Form label + input pairs | Grid | Column alignment across rows |
| Chip/tag list with wrap | Flexbox | Natural wrapping behavior |
Related
- CSS container queries - pair container queries with Grid and Flexbox to make components that respond to their parent's size, not just the viewport
- CSS to React style converter - paste a Grid or Flexbox rule and get the camelCase
styleobject a React component expects - React Server Components - layout decisions affect which components need to be Client Components; understanding the split matters for CSS that works across the server/client boundary
- React Hooks pitfalls - dynamic layout logic often ends up inside hooks; this covers the state and effect patterns that drive it
- Recharts - composable chart library (Line, Bar, Pie, Area) built on D3; slots into Grid dashboard layouts and handles responsive sizing via the ResponsiveContainer wrapper
- React Resizable Panels - when Grid and Flexbox aren't enough, this adds drag-to-resize handles between panels; useful for code editors, dashboards, and split-pane layouts
- Styleframes design process on Art of Styleframe - design principles that shape the visual decisions behind responsive layouts