Tailwind CSS Handbook v4.3

Flexbox

One-dimensional layout with Tailwind — direction, alignment, wrapping, grow and shrink — and the handful of patterns that cover most real interfaces.

Flexbox lays out children along a single axis and distributes the free space between them. It is the right tool for toolbars, navigation, button groups, form rows and card internals.

Overview#

Loading preview…

That is flex items-center justify-between gap-4 — four classes for a complete header.

The two axes#

Everything in Flexbox depends on which direction is "main".

DirectionMain axisCross axisControlled by
flex-row (default)HorizontalVerticaljustify-* / items-*
flex-colVerticalHorizontaljustify-* / items-*
  • justify-* distributes along the main axis.
  • items-* aligns along the cross axis.

Container utilities#

Direction#

UtilityCSS
flex-rowflex-direction: row
flex-row-reverserow-reverse
flex-colcolumn
flex-col-reversecolumn-reverse

Justify — main axis#

UtilityResult
justify-startPacked at the start
justify-centerPacked in the middle
justify-endPacked at the end
justify-betweenFirst and last at the edges, equal gaps between
justify-aroundEqual space around each item
justify-evenlyEqual space between and at the edges
justify-center-safeLike center, but falls back to start when overflowing v4.1
Loading preview…

Items — cross axis#

UtilityResult
items-startAligned to the cross-axis start
items-centerCentred
items-endAligned to the end
items-baselineText baselines line up
items-baseline-lastAligned on the last baseline v4.1
items-stretchDefault — fill the cross axis
items-center-safeCentre, but do not push content out of view v4.1

items-baseline is worth knowing: it aligns text of different sizes on the line they sit on, rather than by box.

Loading preview…

Wrapping#

UtilityCSS
flex-wrapItems wrap onto new lines
flex-nowrapDefault — items shrink instead of wrapping
flex-wrap-reverseWrap upwards
content-*Aligns the lines when wrapped (content-start, content-between, …)

Gap#

gap-4, gap-x-4, gap-y-2 set spacing between items.

Item utilities#

UtilityCSSMeaning
growflex-grow: 1Absorb free space
grow-0flex-grow: 0Do not grow (default)
shrinkflex-shrink: 1May shrink (default)
shrink-0flex-shrink: 0Never shrink
flex-1flex: 1 1 0%Equal share, ignoring content width
flex-autoflex: 1 1 autoGrow and shrink, starting from content width
flex-initialflex: 0 1 autoShrink only
flex-noneflex: noneFixed size
basis-*flex-basisStarting size along the main axis
order-*orderVisual reordering
self-*align-selfOverride items-* for one item

flex-1 vs flex-auto#

Loading preview…

Use flex-1 for equal columns; flex-auto when content should influence the split.

Common patterns#

Media object#

Loading preview…

Two critical classes here:

  • shrink-0 on the avatar so it never squashes.
  • min-w-0 on the text so truncate can work.

Push one item to the end#

HTML
<div class="flex items-center gap-4">
  <span>Logo</span>
  <nav class="flex gap-4">…</nav>
  <button class="ml-auto">Sign in</button>
</div>

ml-auto absorbs all free space to the left of the button — more flexible than justify-between when there are three or more groups.

HTML
<body class="flex min-h-screen flex-col">
  <header>…</header>
  <main class="flex-1">…</main>
  <footer>…</footer>
</body>

flex-1 on <main> pushes the footer to the bottom on short pages.

Responsive stack#

HTML
<div class="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">…</div>

Equal-height cards in a row#

HTML
<div class="flex gap-4">
  <article class="flex flex-1 flex-col rounded-xl border p-4">
    <h3 class="font-semibold">Title</h3>
    <p class="text-sm text-gray-600">Varying amounts of text.</p>
    <button class="mt-auto pt-4 text-sm text-indigo-600">Read more</button>
  </article>
  …
</div>

items-stretch (the default) makes cards equal height; mt-auto pins each footer to the bottom.

Common mistakes#

  • justify-center for vertical centring in a row. Use items-center.
  • Missing min-w-0 when truncating. The number one reason truncate "doesn't work".
  • Missing shrink-0 on icons and avatars. They get squashed when siblings are long.
  • Using space-x-* where gap fits. gap is simpler and behaves correctly with wrapping.
  • flex-wrap without gap. Wrapped rows will collide.
  • Reordering with order-* and forgetting keyboard users. Visual order changes; tab order does not. Only reorder when they still agree.
  • Grid — when you need two dimensions
  • Display — flex vs inline-flex
  • Spacing — gap and the spacing scale
  • Sizing — basis, min-w-0, max-w-*