Flexbox vs Grid

One axis or two: from normal flow to flex to grid. Play with a container and watch the CSS and the Tailwind that do the same thing side by side, then compare three layouts you meet every day as the screen gets narrower.

Level: BasicWatch: 9 steps · ~1 minuteCSS and Tailwind side by sideRecall: 3 questions · 8 cardsCompare 3 layouts × 3 waysHTML · CSS · Tailwind

What you'll take away

  • Pick flex for one line of things, and grid for rows and columns that must line up
  • Tell justify-content from align-items, even once the main axis runs down
  • Use repeat(auto-fit, minmax(…, 1fr)) for columns that drop without a media query
  • Write the same layout in Tailwind, with square brackets for a value that has no class

The 1-minute version

Tap a point and the picture below plays that step.

4Compare them all
No CSS yet: blocks stack downSix divs, no CSS

.box420 × 270 pxnormal flow ↓

no CSS yet
box 1
box 2
box 3
box 4
box 5
box 6
CSS
.box > * { flex-basis: 5.5rem; }.box {  display: block;}
Tailwind v4
<div class="block">
  <div class="basis-22">1</div>
  <div class="basis-22">2</div>
  …
</div>
Six divs, no CSS → …
Step 1 / 9 · step by step
blockdisplay
–axis
–flex-wrap
–gap
1Tailwind class

The picture: a big screen in the square shows one container and its six boxes, one CSS declaration per step. When it lands, each box glides from where it was to where the browser now puts it, and the CSS and Tailwind beside it mark the line that changed. Every box in this lesson is laid out by the browser's real CSS. The Tailwind here is v4, and every class the buttons can make is checked in a test to write the same declarations as the CSS beside it.

Step 4 · Every option at once, on one clock

Compare them all: block · flex · grid

A nav bar done with block (the default), flex and grid. Every tile gets narrower together, 300 → 250 → 200 → 150 px, so you can see which one wraps and which one overflows.

300 px
block (default)Stacks down one row at a time: no horizontal bar unless you lean on inline-block or floatno axis: stacks down

Use it when A vertical list of links

Skip it when A horizontal nav bar

Every tile is laid out by the browser's real CSS. The numbers under each picture are measured from this page. Press any tile to see it large above, with when it fits.

One axis, or two

  • normal flow: blocks stack down, inline things line up within a line
  • flex: lines things up along one axis, sized by their content; grow / shrink / basis share out the space; with wrap, each line is laid out on its own
  • grid: sets the columns and rows at once, so every cell lines up across the whole table; fr, auto-fit, minmax, areas, span

How to choose

JobUse
Nav bars, rows of buttons, tagsflex
Galleries, cards of equal widthgrid + auto-fit
The whole page layoutgrid + areas
What's inside a grid cellflex (they work together)

Tailwind

Each class is one or two declarations: flex = display: flex, gap-4 = gap: 1rem (one unit is 0.25rem). For a value with no class, use square brackets, like grid-cols-[repeat(auto-fit,minmax(10rem,1fr))]. Tailwind writes the CSS for you; it isn't a new kind of layout.

In real code

nav.html
<!doctype html>
<html lang="th">
<head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="nav.css">
</head>
<body>
  <!-- A nav bar: one row while it fits, the links drop to a second row when it does not. -->
  <nav class="nav">
    <a class="logo" href="/">Mindloop</a>
    <div class="links">
      <a href="/topics">หัวข้อ</a>
      <a href="/lessons">บทเรียน</a>
      <a href="/review">ทบทวน</a>
    </div>
  </nav>
</body>
</html>
Run: open in a browser (checked in headless Chrome: nav.expect.json)It printed: 640 px: logo and links in one row · 240 px: links drop to a second row, nothing overflows

Tailwind v4 really compiles at build time, and its declarations are compared element by element with nav.css: they match. Both versions are opened in Chrome at 640 and 240 px and checked: one row when it fits, a second row when it's narrow, and no overflow.

Common questions

Flex or grid: when do I use which?

Things in one line, as wide as their own content (nav bars, buttons, tags): flex. Things that must line up in rows and columns (galleries, page layouts): grid. They work together: grid for the page, flex for what's inside a cell.

Table: how to choose

What's the difference between justify-content and align-items?

justify-content aligns along the main axis, align-items along the cross axis. In a row, the main axis runs across. Switch to flex-direction: column and the main axis runs down, so justify-content now works vertically.

What does flex: 1 mean?

It's short for grow 1, shrink 1, basis 0%. basis is the starting size, grow is its share of the space that's left, shrink is how much it gives way when there isn't enough. Give every item flex: 1 and they share the space evenly.

Why are the cards on the last row wider when flex wraps?

Because flex lays out one line at a time, and each line shares out its own space. The last row has fewer cards, so each one stretches further. For the same width on every row, use grid with auto-fit.

What do auto-fit and minmax do?

repeat(auto-fit, minmax(10rem, 1fr)) fits as many columns of at least 10rem as there's room for, then shares what's left evenly. When the space gets narrower the columns drop by themselves, no media query needed.

What is fr, and how is it different from %?

1fr is one share of the free space in a grid. 1fr 2fr splits it into three shares and gives the second column two. Unlike %, it's worked out after the gaps are taken off, so adding a gap doesn't push the grid out of its box. Columns that add up to 100% plus a gap do.

What is grid-template-areas good for?

You name the areas and draw the page as text, like 'head head' 'side main'. A child just says grid-area: main and goes there. To rearrange it on a narrow screen, rewrite the areas in one media query.

Tailwind has no class for the value I want. Now what?

Put the value in square brackets, like grid-cols-[repeat(auto-fit,minmax(10rem,1fr))], writing any spaces as _. Tailwind writes that declaration exactly as you gave it: it only writes CSS for you, it isn't a new kind of layout.

Next lesson

CSR · SSR · SSG · ISR · RSCLevel: Intermediate

The page is laid out; next, where its HTML gets put together: in the browser, on the server for each request, or at build time.