Flexbox & Grid Layout

Beginner · 34 min

How Flexbox and Grid replaced float-based layout hacks, and how to tell which one a given layout problem actually calls for.

Before thisThe Box Model & the Cascade

Kevin Powell · 34 min

Flexbox & Grid Layout

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Kevin PowellReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Flexbox lays elements out along a single axis (row or column) and is built for distributing or aligning items in that one dimension.
  • Grid lays elements out across rows and columns at once, and is built for two-dimensional layouts defined up front.
  • In Flexbox, justify-content works along the main axis and align-items works along the cross axis — swapping flex-direction swaps which is which.
  • flex-grow, flex-shrink, and flex-basis control how individual flex items expand, shrink, or start, and together they decide how leftover space gets divided.
  • Grid's fr unit divides leftover space proportionally between tracks, and grid-template-areas lets you name regions of the layout and place elements by name instead of by index.
  • The practical rule of thumb: reach for Flexbox when you're arranging items in a line, and reach for Grid when you're defining the overall page or component skeleton.

The problem these two were built to solve

For a long time, CSS had no real layout system. People laid out pages using float, which was designed for wrapping text around an image, not for building navigation bars and card grids. It mostly worked, but it came with constant side effects — collapsing parent heights, awkward clearfix hacks, and layouts that broke the moment content changed size. Flexbox and Grid exist because the web needed layout tools that were actually designed to be layout tools.

They solve different shapes of problem, and the fastest way to internalize the difference is dimensional: Flexbox is one-dimensional, Grid is two-dimensional. Flexbox arranges a set of items along a single line — a row or a column — and is extremely good at questions like “space these evenly,” “push this one to the end,” or “let these items wrap onto new lines when they run out of room.” Grid arranges content across rows and columns at the same time, which makes it the right tool when you’re defining a structure — a page skeleton, a card layout, a dashboard — where things need to line up both horizontally and vertically.

Neither replaced the other. Most real interfaces use both: Grid for the overall page or component shape, Flexbox for arranging things inside one row of that grid, like a toolbar or a button group.

Flexbox: main axis, cross axis

The single idea that unlocks Flexbox is realizing it has two axes, and which one is “main” depends on flex-direction:

  • flex-direction: row (the default) makes the main axis horizontal.
  • flex-direction: column makes the main axis vertical.

Once you set the direction, two properties do almost all the work:

  • justify-content aligns items along the main axis — think of it as “spread these out left-to-right” in row mode.
  • align-items aligns items along the cross axis — the perpendicular direction — think “line these up top-to-bottom” in row mode.

This is exactly why beginners get tripped up switching between row and column: the same property name now does the opposite physical thing, because the axis it’s tied to rotated. Once that clicks, values like justify-content: space-between (push the first and last item to the edges, distribute the rest evenly) or align-items: center (center everything on the cross axis) stop feeling like magic incantations and start feeling like consequences of the axis model.

Controlling individual items: grow, shrink, basis

Beyond alignment, Flexbox lets each item say how it behaves when space is tight or abundant, through three properties usually combined into the shorthand flex:

  • flex-basis — the item’s starting size before growing or shrinking kicks in.
  • flex-grow — how eagerly the item claims leftover space, relative to siblings.
  • flex-shrink — how eagerly the item gives up space when the container is too small.

flex-grow: 1 on every item in a row means “split whatever’s left over equally.” Give one item flex-grow: 2 and it claims twice as much of that leftover space as a sibling with flex-grow: 1 — not twice the total width, just twice the share of what’s left after every item’s base size is accounted for. This distinction (leftover space, not total space) is the part people usually get wrong when first predicting flex-grow’s output.

Grid: naming the structure instead of describing one item at a time

Where Flexbox reasons item-by-item, Grid reasons about the whole container up front. You declare the structure once:

.layout {
  display: grid;
  grid-template-columns: 200px 1fr 1fr;
  grid-template-rows: auto 1fr auto;
}

That single declaration says: a fixed 200px sidebar, two equal flexible columns next to it, and three rows where the header and footer size to their content while the middle row fills whatever’s left. The fr unit — short for “fraction” — is doing the key work here. It divides whatever space remains after fixed-size tracks (like that 200px sidebar) are subtracted, and splits it proportionally among the fr tracks. Two 1fr tracks split the leftover 50/50; a 2fr next to a 1fr splits it 2:1. This is more predictable than percentages, because fr tracks only compete with each other, while a percentage is always a slice of the full container regardless of what else is taking up space.

Grid’s other standout feature is grid-template-areas, which lets you name regions and place elements by name instead of counting rows and columns:

.layout {
  display: grid;
  grid-template-columns: 200px 1fr;
  grid-template-areas:
    "sidebar header"
    "sidebar content"
    "sidebar footer";
}

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

The CSS literally reads like an ASCII diagram of the page. That readability is a real advantage over counting grid-column: 2 / 3 by hand, especially once a layout changes shape at a breakpoint — you can redefine grid-template-areas for a mobile layout and every named element just reflows into its new slot, with no changes needed to the elements themselves.

Choosing between them

A practical heuristic: ask whether you’re laying out a line or a grid of things. A nav bar, a button group, a row of tags, a list of items that should wrap — that’s a line, reach for Flexbox. A page shell, a card grid where items need to line up in both directions, a dashboard of panels — that’s a two-dimensional structure, reach for Grid. And don’t be surprised when a real layout nests both: a Grid-based page shell with a Flexbox toolbar inside the header is an extremely common, perfectly idiomatic pattern, not a sign you picked the wrong tool.

Check yourself

  1. 01What's the core difference between what Flexbox and Grid are each designed to solve?

    Flexbox distributes and aligns items along one axis at a time, like a single row or column. Grid defines a two-dimensional structure of rows and columns simultaneously, which is what a full page or card layout usually needs.

  2. 02In a flex container with flex-direction: column, does justify-content control vertical or horizontal alignment?

    Vertical. justify-content always follows the main axis, and setting flex-direction: column rotates the main axis to run top-to-bottom, so justify-content now controls vertical spacing instead of horizontal.

  3. 03Why does setting flex-grow: 1 on every item in a flex container make them all end up the same width?

    flex-grow distributes leftover space proportionally after the items' base sizes are accounted for. If every item has the same flex-grow value and no other size constraints, they each claim an equal share of that leftover space, so they end up equal.

  4. 04What does the fr unit represent in a CSS Grid template, and how is it different from a percentage?

    fr represents a fraction of whatever space is left over after fixed-size tracks (like px) are subtracted. Unlike a percentage, which is always relative to the full container, fr tracks only compete for space among themselves and automatically shrink if a fixed track takes more room.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark