Skip to content

Lesson 4 of 9 · 22m

Flexbox and Grid: When and Why

One-dimensional vs two-dimensional layout, the flex sizing algorithm that bites everyone, and the grid patterns that replace a decade of float hacks.

The question isn't "flexbox or grid" — it's "which problem am I solving?" Flexbox lays out items along one axis and lets them negotiate space. Grid defines a two-dimensional track structure and places items into it. Using the wrong one doesn't fail loudly; it produces CSS that fights you forever.

Flexbox: content-out, one axis at a time

.nav {
  display: flex;
  align-items: center;      /* cross axis */
  justify-content: space-between;  /* main axis */
  gap: 1rem;                /* yes, gap works in flexbox now — use it */
}

Flexbox shines for toolbars, nav bars, button groups, centering things, and distributing space among items of unknown size. The items decide their size; the container negotiates.

The sizing algorithm is where everyone gets burned. flex: 1 is shorthand for flex: 1 1 0% — grow yes, shrink yes, basis zero. That zero basis is what makes three columns actually equal: with the default basis auto, items size by content first and grow from there, so the column with longer text ends up wider.

.col { flex: 1; }            /* equal columns — basis 0 */
.col-auto { flex: 0 1 auto; } /* size by content, may shrink, won't grow */

min-width: auto is the other trap: flex items refuse to shrink below their content's intrinsic size by default. A long URL or a <pre> inside a flex child will blow out your layout. The fix is min-width: 0 (or overflow: hidden) on the item — memorize this, you will use it monthly for the rest of your career.

Grid: structure-in, two dimensions

.page {
  display: grid;
  grid-template-columns: 240px 1fr;
  grid-template-rows: auto 1fr auto;
  min-height: 100vh;
  gap: 1.5rem;
}

Grid shines when the container owns the structure: page shells, card grids, dashboards, form layouts. 1fr means "share of leftover space" — fr units distribute free space after fixed tracks and content minimums are satisfied.

The card-grid pattern that ended media-query breakpoints

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(16rem, 100%), 1fr));
  gap: 1rem;
}

auto-fill + minmax produces as many columns as fit, each at least 16rem, stretching to fill the row. No breakpoints, no media queries — the layout is intrinsically responsive. The min(16rem, 100%) guard prevents overflow when the container itself is narrower than 16rem (a detail older versions of this pattern get wrong).

Field notes

  • gap works in flexbox, grid, and multi-column — stop using margin hacks for spacing between siblings.
  • Flexbox flex-wrap: wrap creates line-by-line layouts; it is not a poor man's grid. If items must align across rows, that's grid's subgrid or actual grid.
  • place-items: center on a grid parent is the shortest correct centering in CSS. The era of vertical-centering hacks is over; act like it.

Was this lesson helpful?

Sign in to rate