- Flexbox is for layout in one direction (a row or a column).
- Grid is for layout in two directions (rows and columns together).
Flexbox: Best for Rows or Columns of Items
Use Flexbox for navigation bars, button groups, card rows and centering things.
<nav class="navbar">
<a href="/">Home</a>
<a href="/courses">Courses</a>
<a href="/contact">Contact</a>
</nav>
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
gap: 1rem;
padding: 1rem;
}
What this does:
-
display: flexturns the container into a flex container. -
justify-contentcontrols spacing along the main axis (horizontal by default). -
align-itemscontrols alignment on the cross axis (vertical by default). -
gapadds space between items without margin hacks.
Centering anything (the classic problem)
.center {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
Grid: Best for Full Page and Card Layouts
Use Grid when you need to control rows and columns together, such as a product gallery, dashboard or page layout.
<section class="cards">
<div class="card">1</div>
<div class="card">2</div>
<div class="card">3</div>
<div class="card">4</div>
<div class="card">5</div>
<div class="card">6</div>
</section>
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
gap: 1.5rem;
}
This one line, repeat(auto-fit, minmax(220px, 1fr)), creates a responsive grid with no media queries. Cards are at least 220px wide, and the grid fits as many as it can per row.
A simple page layout with named areas
.page {
display: grid;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
grid-template-columns: 250px 1fr;
gap: 1rem;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }
On mobile, collapse it to one column:
@media (max-width: 768px) {
.page {
grid-template-areas:
"header"
"main"
"sidebar"
"footer";
grid-template-columns: 1fr;
}
}
Quick Comparison
| Situation | Use |
|---|---|
| Navigation bar | Flexbox |
| Centering one element | Flexbox |
| Row of buttons or tags | Flexbox |
| Image or card gallery | Grid |
| Full page layout | Grid |
| Dashboard with rows and columns | Grid |
| Component inside a grid cell | Flexbox |
Using Both Together
A common pattern is Grid for the overall page and Flexbox inside each component:
.card {
display: flex;
flex-direction: column;
justify-content: space-between;
}
The grid places the cards. Flexbox arranges what is inside each card.
Common Beginner Mistakes
- Using Grid for everything. A simple row of buttons does not need it.
-
Fixed pixel widths. Prefer
fr,%,minmax()andauto-fitfor responsive layouts. -
Forgetting
gap. Use it instead of margins between items. - Not testing on mobile. Use your browser DevTools device mode.
Practice Exercise
Build this small project:
- A navbar with Flexbox
- A hero section centered with Flexbox
- A 3-column responsive card section with Grid
- A footer
- Make the cards stack into one column on mobile
Doing this once will teach you more than reading ten tutorials.
Wrapping Up
Flexbox for one dimension, Grid for two, and both together for real projects. Once this clicks, CSS layout stops feeling like guesswork.
What layout problem confused you the most when you started? Let me know in the comments.
I teach web development at 99EEDU in Kolkata. If you want hands-on practice with HTML, CSS and JavaScript, see the Responsive HTML course.
Top comments (0)