Back to CSS
CSS
easy
mid

Where do Flexbox and CSS Grid actually shine in real product UIs?

Flexbox is one-dimensional (lay out items along a single row OR column); Grid is two-dimensional (rows AND columns together). Flexbox is content-driven and great for components (toolbars, nav, centering); Grid is layout-driven and great for page-level structure. They compose — Grid for the macro layout, Flexbox inside the cells.

5 min read·~8 min to think through

Flexbox and Grid are both modern CSS layout systems, but they solve different problems. The core distinction: dimensionality.

The fundamental difference

  • Flexbox is one-dimensional — it lays items out along a single axis: a row or a column. Wrapping creates multiple lines, but each line is still managed independently.
  • Grid is two-dimensional — it controls rows and columns simultaneously, as a true grid where items can span and align in both directions at once.

Content-driven vs. layout-driven

  • Flexbox is content-driven. You give it items and it distributes space based on their content and flex properties. You don't define the structure up front.
  • Grid is layout-driven. You define the grid structure (grid-template-columns/rows) first, then place items into it.

When to use which

Use Flexbox for…Use Grid for…
Navigation bars, toolbarsPage-level layout (header/sidebar/main/footer)
Centering a thingCard galleries, dashboards
Distributing items in a rowAny 2D arrangement
Components where content size drives layoutLayouts where you want explicit structure
"Push this item to the end"Overlapping items, named areas

They compose — it's not either/or

The common real-world pattern: Grid for the macro layout, Flexbox for the micro.

css
.page {
  display: grid;
  grid-template-columns: 240px 1fr;
  grid-template-rows: auto 1fr auto;
}
.card {
  display: flex;          /* Grid placed the card; Flexbox lays out its insides */
  justify-content: space-between;
  align-items: center;
}

Quick syntax reference

css
/* Flexbox */
.flex { display: flex; justify-content: center; align-items: center; gap: 1rem; }

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

repeat(auto-fill, minmax(200px, 1fr)) is the famous responsive-grid one-liner — no media queries needed.

Senior framing

The senior answer resists "Grid is newer/better." It's 1D vs 2D, content-driven vs layout-driven, and the mature take is that they compose: reach for Grid when you're describing a structure, Flexbox when you're distributing items along a line. Knowing the minmax/auto-fill responsive idiom and gap (now supported in both) signals current knowledge.

Follow-up questions

  • How do you build a responsive card grid without media queries?
  • What does `flex: 1` actually mean (grow/shrink/basis)?
  • When would Grid's named template areas be the right call?

Common mistakes

  • Using Flexbox for 2D layouts and fighting it with manual widths.
  • Thinking it's either/or rather than composing them.
  • Forgetting `gap` works in Flexbox now, not just Grid.
  • Using Grid for a simple one-row toolbar where Flexbox is simpler.

Edge cases

  • Flexbox `flex-basis` vs `width` interactions; `min-width: auto` preventing shrink.
  • Grid `fr` units vs percentages with gaps.
  • Subgrid for aligning nested grids to the parent's tracks.

Real-world examples

  • App shells with Grid, nav bars and cards with Flexbox, responsive galleries with auto-fill.

Related questions