DEV Community

Cover image for Flexbox vs CSS Grid: When to Use Which (With Examples)
Eeduu Institute digital
Eeduu Institute digital

Posted on

Flexbox vs CSS Grid: When to Use Which (With Examples)

  • 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>
Enter fullscreen mode Exit fullscreen mode
.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  padding: 1rem;
}
Enter fullscreen mode Exit fullscreen mode

What this does:

  • display: flex turns the container into a flex container.
  • justify-content controls spacing along the main axis (horizontal by default).
  • align-items controls alignment on the cross axis (vertical by default).
  • gap adds space between items without margin hacks.

Centering anything (the classic problem)

.center {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
}
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 1.5rem;
}
Enter fullscreen mode Exit fullscreen mode

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; }
Enter fullscreen mode Exit fullscreen mode

On mobile, collapse it to one column:

@media (max-width: 768px) {
  .page {
    grid-template-areas:
      "header"
      "main"
      "sidebar"
      "footer";
    grid-template-columns: 1fr;
  }
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

The grid places the cards. Flexbox arranges what is inside each card.

Common Beginner Mistakes

  1. Using Grid for everything. A simple row of buttons does not need it.
  2. Fixed pixel widths. Prefer fr, %, minmax() and auto-fit for responsive layouts.
  3. Forgetting gap. Use it instead of margins between items.
  4. Not testing on mobile. Use your browser DevTools device mode.

Practice Exercise

Build this small project:

  1. A navbar with Flexbox
  2. A hero section centered with Flexbox
  3. A 3-column responsive card section with Grid
  4. A footer
  5. 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)