Skip to content

CSS Grid vs Flexbox: Choosing the Right Layout Tool

CSS Grid and Flexbox solve different layout problems. Learn which to use for page structure, component layouts, and common UI patterns with examples.

· · 8 min read

Updated: May 17, 2026

Side-by-side visualization: CSS Grid two-axis explicit placement versus Flexbox single-axis distribution along main axis

Quick Take

Use Grid for two-dimensional layouts where you control rows and columns simultaneously; use Flexbox for one-dimensional component layouts like navbars, button groups, and card rows. You'll use both in every project, the choice is about which dimension you're managing.

Quick take: - Flexbox is a one-dimensional layout tool (row or column). Grid is two-dimensional (rows and columns simultaneously). Per MDN Web Docs, Flexbox is ideal for component-level alignment while Grid suits page-level structure. Both have 97%+ global browser support in 2026, choose is covered here.

CSS Flexbox is a one-dimensional layout tool, it arranges items along a single axis (row or column) and handles component-level alignment: navigation bars, button groups, centered content, tag lists, and vertically stacked card layouts. CSS Grid is a two-dimensional layout system, it manages rows and columns simultaneously and suits page-level structure: header/sidebar/main/footer shells, image galleries, and dashboard grids where items span multiple columns. Flexbox has 99%+ global browser support in 2026; Grid has 97%+. They work together, not against each other. The practical decision rule: if layout runs in one direction only, use Flexbox. If you need control in both directions at once, or need explicit item placement (like grid-column: span 2), use Grid. Most production UIs use Grid for the outer shell and a flex container inside each region for alignment.

Developers argue about this constantly. I've watched teams ban Grid entirely because "Flexbox does everything" and others go the opposite direction. Both positions are wrong. These two tools were built for different jobs, and knowing which one to reach for first saves real time.

What Problem Does Flexbox Solve?

Flexbox excels at distributing items along a single axis. It's "one-dimensional" - you work in either a row or a column, not both at once. It became the default for component-level alignment because the math it handles automatically used to require float hacks and clearfix.

/* Classic navbar - items in a row with space between */
.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 1rem;
}

/* Push the last button to the far right */
.navbar__actions {
  margin-left: auto;
}

The mental model for Flexbox: you have a flex container and its child elements inside. You set display: flex on the container, then control alignment properties like justify-content and align-items on both axes. That's it. When I first started using it around 2016, it replaced about 40% of my layout CSS immediately.

What Problem Does CSS Grid Solve?

Grid is two-dimensional. You define rows and columns using grid-template-areas or explicit track sizes, then place items anywhere in that matrix. It's the right tool when layout in both directions matters simultaneously.

/* Classic page shell with sidebar */
.page {
  display: grid;
  grid-template-columns: 250px 1fr;
  grid-template-rows: 64px 1fr 48px;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
  min-height: 100vh;
}

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

Try doing this cleanly with Flexbox. You can't - you'd need nested Flexbox containers with carefully managed widths and some fragile height calculations. Grid solves it in 10 lines.

When Should You Use Flexbox?

Reach for Flexbox when you have a list of items that need to line up along a single direction. These patterns are Flexbox territory:

  • Navigation bars and tab lists
  • Button groups and icon-with-label pairs
  • Card content layout (image + title + description stacked vertically)
  • Centering a single element inside a container
  • Distributing items with equal gaps
/* Tag list that wraps naturally */
.tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

/* Vertical card layout */
.card {
  display: flex;
  flex-direction: column;
}

.card__body {
  flex: 1; /* grows to fill available space */
}

.card__footer {
  margin-top: auto; /* pins to bottom */
}

The flex: 1 and margin-top: auto pattern is something I use in almost every card component. It keeps the footer pinned to the bottom regardless of content height, without knowing the card height in advance.

When Should You Use CSS Grid?

Reach for Grid when the layout has meaningful structure in both dimensions. These patterns belong to Grid:

  • Full page layouts (header, sidebar, content, footer)
  • Image galleries and media grids
  • Dashboard card grids with varying column spans
  • Form layouts with label and input alignment
  • Any layout where you want items to align across rows
/* Responsive card grid - no media queries needed */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 1.5rem;
}

That repeat(auto-fill, minmax(280px, 1fr)) pattern is one of CSS's most useful features for responsive layout. Cards automatically reflow from one column to many as space grows. No JavaScript. No breakpoints required.

For the design-side decisions behind these layouts - when bento grids beat traditional cards, how many tiles a dashboard should display before cognitive load flips negative, and where Subgrid changes the alignment math - see CSS Layout Patterns in 2026 on Art of Styleframe. It's the same problem space approached from the design end of the pipeline.

Placing Items Explicitly

Grid also lets you place specific items in specific cells, which Flexbox simply can't do:

/* Featured article spans two columns */
.article-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
}

.article-grid__featured {
  grid-column: span 2;
}

A Practical Decision Rule

Here's the rule I actually use:

One direction only? Use Flexbox. Two directions, or explicit placement? Use Grid.

Layout question One axis or two? one direction two directions Flexbox Navbar, button rows, stacked lists Need explicit placement? grid-area / grid-row no, just sizing yes Grid (auto-fit + minmax) Grid (template-areas)

When you're unsure, start with Flexbox. If you find yourself adding nested Flexbox containers with flex-wrap to fake grid-like behavior, that's the sign to switch to Grid. A responsive layout built with Grid's auto-fill is almost always cleaner than the Flexbox workaround.

They also stack well. A common real-world pattern:

/* Grid for the page skeleton */
.app {
  display: grid;
  grid-template-columns: 240px 1fr;
  grid-template-rows: 60px 1fr;
}

/* Flexbox inside each cell for alignment */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.sidebar-nav {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

Outer structure with Grid. Inner alignment with Flexbox. This combination handles the majority of real UI layouts without reaching for any third tool.

CSS Grid vs Flexbox: Properties Side by Side

Before the pattern list, here's a quick reference for the properties you'll actually use:

Property areaCSS GridFlexbox
AxesTwo (rows + columns simultaneously)One (row or column, not both)
Defining structuregrid-template-columns, grid-template-rowsflex-direction
Item sizingfr units, minmax(), autoflex-grow, flex-shrink, flex-basis
Responsive layoutauto-fill/auto-fit + minmax()flex-wrap
Gapsgap, row-gap, column-gapgap (supported since 2021)
Explicit placementgrid-column, grid-row, grid-areaNot supported, order only
Named regionsgrid-template-areasNot supported
Alignmentjustify-items, align-items, place-itemsjustify-content, align-items
Browser support (2026)97%+ globally99%+ globally

Neither has a meaningful browser support gap anymore. Pick on fit, not on support.

Common Patterns and Which Tool Fits

PatternBest toolWhy
Horizontal nav with logo left, links rightFlexboxSingle axis, space-between
Page shell (header/sidebar/main)GridTwo-dimensional structure
Responsive card gridGridAuto-fill handles reflow
Vertically centered modalFlexbox or GridBoth work; Grid's place-items: center is shorter
Form label + input pairsGridColumn alignment across rows
Chip/tag list with wrapFlexboxNatural wrapping behavior
  • CSS container queries - pair container queries with Grid and Flexbox to make components that respond to their parent's size, not just the viewport
  • React Server Components - layout decisions affect which components need to be Client Components; understanding the split matters for CSS that works across the server/client boundary
  • React Hooks pitfalls - dynamic layout logic often ends up inside hooks; this covers the state and effect patterns that drive it
  • Recharts - composable chart library (Line, Bar, Pie, Area) built on D3; slots into Grid dashboard layouts and handles responsive sizing via the ResponsiveContainer wrapper
  • React Resizable Panels - when Grid and Flexbox aren't enough, this adds drag-to-resize handles between panels; useful for code editors, dashboards, and split-pane layouts
  • Styleframes design process on Art of Styleframe - design principles that shape the visual decisions behind responsive layouts

Frequently Asked Questions

Can I use Grid and Flexbox together?
Yes, and you should. A common pattern is Grid for the outer page layout (header, sidebar, main, footer) with Flexbox inside each region for aligning items. They're designed to complement each other, not compete.
Does CSS Grid replace Flexbox?
No. Grid handles two-dimensional layouts; Flexbox handles one-dimensional layouts. Both are widely supported: Grid at 97%+ and Flexbox at 99%+ globally as of 2026 per MDN data. Use whichever fits the problem.
Which is better for centering a single item?
Either works, but Flexbox is simpler: set display: flex; align-items: center; justify-content: center on the parent. Grid also works with place-items: center, which is even shorter.
When should I use grid-template-areas?
Use grid-template-areas when you have a named layout with distinct regions like a page shell. It makes the visual structure obvious in CSS and is easy to rearrange with media queries by redefining the template.