Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 37 ยท Layout with Flexbox and Grid

Flexbox With Tailwind CSS

Build one-dimensional layouts with Tailwind v4 flexbox utilities: direction, justify and items alignment, wrapping, gap, grow, shrink, basis and order.

Flexbox arranges items along one line, either a row or a column, and distributes the free space between them. It is the right tool for navbars, button groups, toolbars, a card's header and footer, and centering things. Tailwind turns every flexbox property into a short class, so a whole layout reads like a sentence: flex items-center justify-between gap-4.

The container: flex

Add flex to a parent and its direct children become flex items, laid out in a row:

HTML
<div class="flex gap-3">
  <div>One</div><div>Two</div><div>Three</div>
</div>

Use inline-flex when the container should only be as wide as its content, such as a button with an icon.

Direction and the two axes

Everything in flexbox is described relative to two axes. The main axis runs in the flex direction, and the cross axis is perpendicular. justify-* works on the main axis, items-* on the cross axis.

Two axes

Main axis Direction of the items.
Cross axis Perpendicular to it.

flex-row, flex-col, flex-row-reverse and flex-col-reverse set the main axis.

1 / 2
Class Effect
flex-row (default) main axis left to right
flex-col main axis top to bottom
flex-row-reverse, flex-col-reverse reversed order
flex-wrap items move to the next line when they run out of room
flex-nowrap (default) items shrink and never wrap

Aligning: justify and items

justify-* distributes items along the main axis:

  • justify-start (default), justify-center, justify-end
  • justify-between (first and last at the edges, equal gaps between)
  • justify-around, justify-evenly

items-* aligns them on the cross axis: items-start, items-center, items-end, items-stretch (default) and items-baseline (aligns text baselines). For a multi-line (wrapped) container, content-* aligns the lines themselves, and a single item can override with self-start, self-center, self-end, self-stretch.

Here is a playground. Click the buttons to swap classes on the box, and watch which classes change what:

Flexbox playground

Note how justify-* follows the direction: with flex-col, "justify" moves items vertically, because the main axis became vertical. That is the single most important flexbox idea.

Growing, shrinking and sizing items

Flex items have three controls that decide how they use free space:

  • grow lets an item take extra space (grow-0 stops it). flex-1 is the shorthand flex: 1 1 0%: the item grows and shrinks from zero, so several flex-1 items share the space equally.
  • shrink-0 stops an item from being squeezed (useful for avatars and icons), while shrink (default) allows it.
  • basis-* sets the starting size: basis-1/3, basis-64, basis-full.
  • flex-auto, flex-initial and flex-none are other shorthands. flex-none means "exactly my size, no growing or shrinking".

A classic: a row with a fixed icon, flexible text, and a fixed button:

shrink-0, flex-1 and ml-auto

The middle block has min-w-0 because a flex item will not shrink below its content width by default; without it, truncate would never trigger and the long title would stretch the row. This is the most common flexbox bug in practice.

Ordering and margins

  • order-1, order-last, order-first change visual order without touching HTML. Use sparingly: screen readers and keyboard focus still follow the HTML order.
  • Auto margins consume free space on that side: ml-auto pushes an item (and everything after it) to the end, so ml-auto is the simple way to separate a logo from a nav.

Centering recipes

HTML
<!-- Center one child both ways -->
<div class="flex min-h-40 items-center justify-center">...</div>

<!-- A vertical stack with spacing -->
<div class="flex flex-col gap-4">...</div>

<!-- Wrapping chips -->
<div class="flex flex-wrap gap-2">...</div>

<!-- Space between two groups -->
<div class="flex items-center justify-between">...</div>

place-content-center and place-items-center are shorthands that also work on grid; see Grid.

Flexbox or grid?

Use flexbox when you think in one line (a row of buttons, a header), and the content decides each item's size. Use grid when you think in rows and columns at the same time, and the layout decides each item's size. They combine well: a grid of cards, each card a flex column.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In a flex-col container, which class centers items horizontally?

    In a column, the main axis is vertical, so items-* (cross axis) controls horizontal alignment.
  2. 2. What does flex-1 do?

    flex-1 is flex: 1 1 0%, so several flex-1 items divide free space equally.
  3. 3. How do you push one item to the far end of a flex row?

    An auto left margin absorbs all the free space, pushing the item to the right end.
  4. 4. Why add min-w-0 to a flex item that contains a truncate text?

    Flex items default to min-width: auto, which prevents them from becoming narrower than their content; min-w-0 lifts that.

Summary

  • flex makes a container; flex-row and flex-col set the main axis; flex-wrap allows lines.
  • justify-* aligns along the main axis, items-* along the cross axis; gap-* spaces items.
  • flex-1, grow, shrink-0, basis-* control sizing; ml-auto pushes items apart.
  • Add min-w-0 when text inside a flex item must truncate or wrap.

Next: Grid