CSS Grid vs. Flexbox: When to Use Which for Modern Layouts

CSS Grid Flexbox Modern Layouts

CSS Grid and Flexbox are the two most powerful layout systems in modern CSS. They overlap in capabilities, which can make it hard to decide which one to use. This article compares them, shows real, visible examples, and gives practical guidelines for choosing the right tool for each layout problem.

High-level comparison

At a glance:

CSS Grid in action: two-dimensional layouts

Example 1: Responsive gallery with CSS Grid

Resize the window to see how the grid adapts from 4 columns to 2 columns on smaller screens.

Live demo: Grid gallery

Code for the gallery:

<div class="grid-gallery">
  <div>Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
  <div>Item 4</div>
  <div>Item 5</div>
  <div>Item 6</div>
  <div>Item 7</div>
  <div>Item 8</div>
</div>

/* CSS */
.grid-gallery {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.75rem;
}

.grid-gallery div {
  background: linear-gradient(135deg, #6366f1, #ec4899);
  border-radius: 8px;
  height: 70px;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
}

@media (max-width: 800px) {
  .grid-gallery {
    grid-template-columns: repeat(2, 1fr);
  }
}

Example 2: Page layout with header, sidebar, content, and footer

This is where Grid shines: defining named areas and letting the browser place them.

Live demo: Grid page layout
Header Grid template areas
Sidebar
  • Navigation
  • Filters
  • Shortcuts
Main content

This area expands to fill available space. On smaller screens, the sidebar stacks above or below it.

Code for the layout:

<div class="grid-layout">
  <header class="grid-header">Header</header>
  <aside class="grid-sidebar">Sidebar</aside>
  <main class="grid-main">Main content</main>
  <footer class="grid-footer">Footer</footer>
</div>

/* CSS */
.grid-layout {
  display: grid;
  grid-template-columns: 220px 1fr;
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
  gap: 0.75rem;
}

.grid-header { grid-area: header; }
.grid-sidebar { grid-area: sidebar; }
.grid-main   { grid-area: main; }
.grid-footer { grid-area: footer; }

@media (max-width: 800px) {
  .grid-layout {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto 1fr auto;
    grid-template-areas:
      "header"
      "sidebar"
      "main"
      "footer";
  }
}

When to prefer CSS Grid

Flexbox in action: one-dimensional layouts

Example 3: Responsive navigation bar with Flexbox

On narrow screens, the nav collapses into a vertical stack thanks to a media query.

Live demo: Flexbox navigation

Code for the navigation:

<nav class="flex-nav">
  <div class="flex-nav-logo">MySite</div>
  <div class="flex-nav-links">
    <a href="#">Home</a>
    <a href="#">Features</a>
    <a href="#">Pricing</a>
    <a href="#">Contact</a>
  </div>
</nav>

/* CSS */
.flex-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.flex-nav-links {
  display: flex;
  gap: 0.75rem;
}

@media (max-width: 600px) {
  .flex-nav {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.5rem;
  }
}

Example 4: Flexible card row with wrapping

Flexbox makes it easy to create rows of cards that wrap naturally as space changes.

Live demo: Flexbox cards

Starter

Perfect for small projects and experiments.

Pro

For growing teams that need more control.

Enterprise

Custom solutions for large organizations.

Code for the card row:

<div class="flex-cards">
  <article class="flex-card">...</article>
  <article class="flex-card">...</article>
  <article class="flex-card">...</article>
</div>

/* CSS */
.flex-cards {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
}

.flex-card {
  flex: 1 1 180px; /* grow, shrink, basis */
  background: #fff;
  border-radius: 10px;
  padding: 0.75rem;
}

When to prefer Flexbox

Using Grid and Flexbox together

In real projects, you rarely choose only Grid or only Flexbox. A common pattern is:

Example 5: Pricing section using Grid + Flexbox

Grid arranges the plans; Flexbox aligns actions inside each card.

Live demo: Combined Grid + Flexbox pricing

Basic

For individuals
$9/mo
  • 3 projects
  • Email support
  • Community access

Pro

For small teams
$29/mo
  • Unlimited projects
  • Priority support
  • Advanced analytics

Enterprise

For large organizations
Custom
  • Dedicated manager
  • Custom integrations
  • 24/7 support

Key idea: Grid defines the columns and spacing between plans, while Flexbox handles the internal alignment of buttons and text.

Best practices and decision guide

Quick decision checklist

Practical tips

Next article: Speed up page loading with the Speculation Rules API