All articles
Layout

CSS Flexbox vs Grid: When to Use Which in 2025

10 February 2025 8 min read Layout

A practical, no-nonsense guide to choosing between Flexbox and CSS Grid. Includes real-world layout scenarios, code examples and the rules senior developers actually follow.

If you've ever stared at a layout problem wondering whether to reach for display:flex or display:grid, you're in good company. This is one of the most searched CSS questions — and the honest answer is: they solve different problems.

The core mental model

Here's the simplest way to think about it:

  • Flexbox is one-dimensional — it lays items out in a row or a column.
  • CSS Grid is two-dimensional — it controls rows and columns simultaneously.

That single distinction drives almost every decision. If you're arranging items along a single axis — a nav bar, a row of buttons, a card's internal layout — reach for Flexbox. If you're placing elements on a two-dimensional canvas — a page layout, a dashboard, a magazine grid — reach for Grid.

When to use Flexbox

Flexbox shines when content drives layout. The items determine how much space they need, and Flexbox distributes the rest.

Perfect Flexbox use cases:

  • Navigation bars with logo on the left, links on the right
  • Vertically and horizontally centring a single element
  • Rows of cards that wrap onto new lines when they run out of space
  • The internal layout of a component (icon + label + badge)
  • Sticky footer layouts where the main content should grow to fill available space
/* Classic nav bar */
nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* Perfectly centred element */
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
}
When to use CSS Grid

Grid shines when layout drives content. You define the structure first, then place items into it.

Perfect Grid use cases:

  • Page-level layouts (header, sidebar, main, footer)
  • Dashboard panels that need to span multiple columns or rows
  • Image galleries with consistent sizing
  • Any layout where items need to align on both axes
  • Magazine or editorial layouts where items overlap or span irregularly
/* Classic page layout */
body {
  display: grid;
  grid-template-columns: 240px 1fr;
  grid-template-rows: 60px 1fr auto;
  grid-template-areas:
    "header  header"
    "sidebar main"
    "footer  footer";
  min-height: 100vh;
}

header  { grid-area: header;  }
aside   { grid-area: sidebar; }
main    { grid-area: main;    }
footer  { grid-area: footer;  }
The rule senior developers actually follow

After years of writing CSS, most experienced developers land on a simple heuristic:

Use Grid for the macro layout (the page structure). Use Flexbox for the micro layout (components and their internals).

They're not mutually exclusive. A Grid-based page layout can contain Flexbox-based components — and often does. A sidebar built with Grid might use Flexbox internally to align its navigation links.

The worst thing you can do is pick one and refuse to use the other. They're complementary tools.

Quick reference
Situation Use
Nav bar, headerFlexbox
Page layout (sidebar + main)Grid
Centring a single elementFlexbox
Image galleryGrid
Card component internalsFlexbox
Dashboard panelsGrid
Wrapping tag/chip listFlexbox (with wrap)
Overlapping elementsGrid
// Try these tools Grid Generator Templates