CSS Grid vs Flexbox: Which to Use and When
The simple rule: Flexbox for one dimension, Grid for two. But real layouts are messier. Here's how to think about choosing between them.
The grid-vs-flexbox question has a memorably simple answer: one direction = flexbox, two directions = grid. Navigation bar items in a row? Flexbox. Page layout with header/sidebar/content positioned on X and Y axes? Grid. This rule handles 70% of cases.
Where Flexbox Excels
Flexbox is best when content determines layout. You don't know how many nav items there are, or how long they'll be — flexbox distributes space and wraps gracefully. Card components where the inner content (icon, title, description, button) stacks vertically. Any situation where you're arranging items along a single axis and want smart wrapping or space distribution.
Where CSS Grid Excels
Grid is best when layout determines content placement. You have a defined structure and you're placing items into it. Page layouts, dashboard grids, photo galleries with consistent sizing, any design where items need to align on both horizontal and vertical axes simultaneously.
Grid's killer feature: items can span multiple rows and columns. A featured card that spans 2 rows and 2 columns of a grid, with regular cards filling the rest — that's trivial with Grid and painful without it.
The Overlap Cases
A card grid (like a product listing) works with both. Flexbox with flex-wrap: wrap lays out cards in rows and wraps to new lines. Grid with grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)) does the same with better column alignment. For this pattern, Grid usually produces more consistent alignment, especially with varying-height cards.
Centering: The Classic Use Case
Centering content both horizontally and vertically — the question that haunted CSS for years — is now trivially easy:
- Flexbox: display: flex; justify-content: center; align-items: center
- Grid: display: grid; place-items: center
- Both work. Grid's place-items: center is the shortest to type.
Learning approach
CSS Grid Garden (cssgridgarden.com) and Flexbox Froggy (flexboxfroggy.com) are browser games that teach both through interactive exercises. An hour on each builds intuition faster than reading documentation.
Frequently Asked Questions
Can I use CSS Grid and Flexbox together?+
Is CSS Grid supported in all browsers?+
When should I use margin auto vs flexbox for centering?+
What is the fr unit in CSS Grid?+
🔧 Free Tools Used in This Guide
FreeToolKit Team
FreeToolKit Team
We build free browser-based tools and write practical guides that skip the fluff.
Tags: