CSS Grid vs Flexbox
When to use CSS Grid and when to use Flexbox to build modern, responsive web layouts — a question every CSS developer asks. The short answer: Flexbox handles one-dimensional layouts (a row or a column), while Grid handles two-dimensional layouts (rows and columns at the same time). This guide explains the difference, shows when each is the better choice, and gives you a decision rule you can apply immediately.
Flexbox
Flexbox is ideal for one-dimensional layouts, arranging items either in a row
or a column. Its main axis follows the direction you choose
(flex-direction), and its main strengths are distributing space
between items and aligning them:
display: flex;
justify-content: space-between; /* horizontal distribution */
align-items: center; /* vertical alignment */
Because items grow, shrink and wrap along a single axis, Flexbox is the natural fit for components whose size is content-driven:
- Navigation bars and toolbars
- Card rows and media lists (avatar + text)
- Form fields and button groups
- Centering an element inside a container
CSS Grid
Grid is ideal for two-dimensional layouts: you define both columns and rows up front, then place items into that structure:
display: grid;
grid-template-columns: repeat(12, 1fr);
grid-template-rows: auto 1fr auto;
gap: 16px;
Grid is the right tool whenever the layout structure matters more than the content size:
- Full-page layouts (header, sidebar, main, footer)
- Dashboards and data-heavy screens
- Photo galleries with aligned cards
- Any design that needs explicit overlapping or spanning
Side by Side
| Aspect | Flexbox | Grid |
|---|---|---|
| Dimensions | One (row or column) | Two (rows and columns) |
| Content-driven | Items size based on content | Tracks are defined first, then content placed |
| Best for | Components and alignment | Page and dashboard layouts |
| Flow | Items flow and wrap automatically | Explicit placement and spanning |
| Overlap | Not natural | Native (grid areas, z-index) |
The Common Approach
Most real pages use both: Grid to define the overall page skeleton, and Flexbox inside each region to lay out the components. A practical example — a page shell in Grid with a toolbar laid out by Flexbox:
.page {
display: grid;
grid-template-columns: 240px 1fr;
grid-template-rows: auto 1fr;
}
.toolbar {
display: flex;
justify-content: space-between;
align-items: center;
}
How to Decide
- Ask: "do I control rows and columns?" If yes → Grid.
- Is it a component whose items should flow, wrap and align in one direction? → Flexbox.
- Need items to span multiple rows or overlap? → Grid.
- Just centering something? → Flexbox (or
place-items: center).
If you're still unsure, start with Flexbox — a flex container can be composed with nested containers to solve most problems, and you can upgrade to Grid when the layout stops being one-dimensional.
Conclusion
Grid and Flexbox are not competitors; they complement each other. Grid builds the page structure, Flexbox arranges the pieces inside it. Choosing by dimensionality — one axis versus two — gives you clean, maintainable CSS instead of fighting the layout system.