DevLab

Flexbox vs CSS Grid: When to Use Each

Learn the fundamental difference between one-dimensional Flexbox and two-dimensional Grid, with practical examples for navbars, galleries, and page layouts.

CSS

The One-Sentence Rule

Flexbox is for rows or columns. Grid is for rows and columns simultaneously.

Laying out items in a single direction — a navbar, a row of cards, a vertical sidebar — use Flexbox. Controlling items in two dimensions at once — a dashboard, a photo gallery, a page layout with header, sidebar, main, and footer — use Grid.

Flexbox in Practice

/* Horizontal nav with items spread out */
.navbar {
  display: flex;
  justify-content: space-between;  /* horizontal distribution */
  align-items: center;             /* vertical alignment */
}

/* Equal-width columns that wrap on small screens */
.card-row {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}
.card { flex: 1 1 280px; }

Grid in Practice

/* Dashboard layout with named areas */
.dashboard {
  display: grid;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
  grid-template-columns: 250px 1fr;
  grid-template-rows: 60px 1fr 40px;
}
.header  { grid-area: header; }
.sidebar { grid-area: sidebar; }

/* Auto-fill responsive gallery */
.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 8px;
}

They Are Complementary

The page layout uses Grid; content inside each grid cell often uses Flex. Grid defines macro structure (sidebar, main, header). Flex aligns items within each section (nav links, card contents, form rows).

Quick Decision Guide

ScenarioUse
Navigation barFlexbox
Card row that wrapsFlexbox
Photo gallery (auto-fill)Grid
Page layout with sidebarGrid
Form with aligned labels/inputsGrid
Center one elementEither (Flex simpler)

Frequently Asked Questions

When should I use Flexbox vs CSS Grid?

Use Flexbox for one-dimensional layouts — a single row or a single column of items. Use CSS Grid for two-dimensional layouts where you need to control both rows and columns simultaneously. Many real layouts combine both: Grid for the overall page structure, Flexbox for component-level alignment.

Can I use Flexbox and Grid together?

Yes, and this is the recommended approach. Use CSS Grid for the macro layout (page sections, card grids, dashboards) and Flexbox for micro layout within individual components (nav bars, button groups, card content alignment). They are complementary, not competing.

Is CSS Grid supported in all browsers?

Yes. CSS Grid has been supported in all major browsers since 2017 (Chrome 57, Firefox 52, Safari 10.1, Edge 16). As of 2026, global browser support exceeds 97%. There is no practical reason to avoid Grid for browser compatibility.

What is the difference between justify-content and align-items?

In Flexbox, justify-content aligns items along the main axis (horizontal by default) and align-items aligns them along the cross axis (vertical by default). In Grid, justify-content and align-content control the grid tracks, while justify-items and align-items control items within their cells.

More Learning Topics

Free · no newsletter yet

Want a note if we start a dev tools newsletter?

We don’t send a newsletter yet. Leave your email and we’ll write only if we start one: new tools on DevLab.

Stored with our email provider, Resend; never sold or shared. To be removed, write to us from the contact page.