CSSFlexboxGridWeb Design
CSS Flexbox vs Grid — Complete Guide to Modern Layouts
2026-02-2510 min read
Flexbox vs Grid: When to Use Which
CSS provides two powerful layout systems. Understanding when to use each is key to writing clean, maintainable layouts.
Flexbox: One-Dimensional Layouts
Use Flexbox when you need to arrange items in a single row or column. Think of it as a rubber band — items stretch and shrink to fill available space. Best for:.nav {
display: flex;
justify-content: space-between;
align-items: center;
gap: 1rem;
}
Grid: Two-Dimensional Layouts
Use Grid when you need to control both rows and columns simultaneously. Think of it as a spreadsheet — you define the grid structure and place items within it. Best for:.dashboard {
display: grid;
grid-template-columns: 250px 1fr 300px;
grid-template-rows: auto 1fr auto;
gap: 1rem;
min-height: 100vh;
}
Key Flexbox Properties
Container Properties
| Property | Values | Effect |
|---|---|---|
flex-direction | row, column | Main axis direction |
justify-content | flex-start, center, space-between | Main axis alignment |
align-items | stretch, center, flex-start | Cross axis alignment |
flex-wrap | nowrap, wrap | Allow items to wrap |
gap | length | Space between items |
Item Properties
| Property | Purpose |
|---|---|
flex-grow | How much an item grows relative to others |
flex-shrink | How much an item shrinks |
flex-basis | Initial size before growing/shrinking |
align-self | Override container's align-items for one item |
Key Grid Properties
Container Properties
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: auto;
gap: 1rem;
}
Useful Grid Patterns
Auto-fill responsive grid (no media queries needed):.responsive-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
gap: 1rem;
}
Holy grail layout:
.layout {
display: grid;
grid-template: "header header header" auto
"nav main aside" 1fr
"footer footer footer" auto
/ 200px 1fr 200px;
}
Combining Flexbox and Grid
The most effective layouts use both. Use Grid for the overall page structure and Flexbox for component-level layouts.
/ Grid for page layout /
.page {
display: grid;
grid-template-columns: 1fr;
gap: 2rem;
}/ Flexbox for card content /
.card {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
.card-footer {
display: flex;
justify-content: space-between;
align-items: center;
margin-top: auto;
}
Common Mistakes to Avoid
min-width: 0 — Flex items won't shrink below their content size by defaultgap — Modern alternative to margins between itemsPractice Tools
Use SnapTools' CSS Flexbox Playground and CSS Grid Generator to experiment with these properties in real-time. Build your layout visually, then copy the generated CSS.
Conclusion
Start with Grid for your page structure, use Flexbox inside components, and you'll write cleaner CSS with less code.