TExcelTAD
Home › Tailwind CSS › Tailwind Flexbox

Tailwind Flexbox

Flexbox arranges items in a single row or column. In Tailwind, you switch it on with flex and control it with short utilities that map directly to the CSS flex properties.

Direction and wrapping

Class CSS
flex display: flex
flex-row / flex-col main axis horizontal / vertical
flex-row-reverse / flex-col-reverse reversed order
flex-wrap / flex-nowrap allow items to wrap onto new lines
gap-4 / gap-x-4 / gap-y-2 space between items
html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="space-y-4 p-4 text-sm text-white">
  <div class="flex gap-2 rounded bg-sky-100 p-2">
    <div class="rounded bg-sky-500 p-2">1</div><div class="rounded bg-sky-500 p-2">2</div><div class="rounded bg-sky-500 p-2">3</div>
  </div>
  <div class="flex flex-row-reverse gap-2 rounded bg-sky-100 p-2">
    <div class="rounded bg-sky-600 p-2">1</div><div class="rounded bg-sky-600 p-2">2</div><div class="rounded bg-sky-600 p-2">3</div>
  </div>
  <div class="flex w-48 flex-col gap-1 rounded bg-sky-100 p-2">
    <div class="rounded bg-sky-700 p-1">flex-col 1</div><div class="rounded bg-sky-700 p-1">flex-col 2</div>
  </div>
</body>
</html>

Alignment

Main axis (justify-*) Cross axis (items-*)
justify-start items-start
justify-center items-center
justify-end items-end
justify-between items-stretch (default)
justify-around / justify-evenly items-baseline

For a single item, self-start, self-center, self-end override items-*.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="space-y-3 p-4 text-xs">
  <div class="flex h-14 items-center justify-between rounded bg-violet-100 px-2">
    <span class="rounded bg-violet-500 p-2 text-white">A</span><span class="rounded bg-violet-500 p-2 text-white">B</span><span class="rounded bg-violet-500 p-2 text-white">C</span>
    <span class="ml-1 text-violet-700">justify-between items-center</span>
  </div>
  <div class="flex h-14 items-end justify-center gap-2 rounded bg-violet-100 px-2">
    <span class="rounded bg-violet-500 p-2 text-white">A</span><span class="self-start rounded bg-pink-500 p-2 text-white">self-start</span><span class="rounded bg-violet-500 p-2 text-white">C</span>
  </div>
  <div class="flex h-14 justify-evenly rounded bg-violet-100">
    <span class="rounded bg-violet-500 p-2 text-white">stretch</span><span class="rounded bg-violet-500 p-2 text-white">evenly</span>
  </div>
</body>
</html>

Growing and shrinking

Class CSS Meaning
flex-1 flex: 1 grow and shrink, ignoring content size
flex-auto flex: 1 1 auto grow and shrink, based on content size
flex-none flex: none never grow or shrink
grow / grow-0 flex-grow: 1 / 0
shrink-0 flex-shrink: 0 keep its size (icons, avatars)
basis-1/3 / basis-64 flex-basis starting size
order-first / order-last / order-2 order visual order

Note: v4 removed the deprecated flex-grow-* and flex-shrink-* names. Use grow and shrink-0.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="space-y-3 p-4 text-xs text-white">
  <div class="flex gap-2">
    <div class="w-16 shrink-0 rounded bg-emerald-700 p-2">fixed</div>
    <div class="flex-1 rounded bg-emerald-500 p-2">flex-1 takes the rest</div>
    <div class="w-16 shrink-0 rounded bg-emerald-700 p-2">fixed</div>
  </div>
  <div class="flex gap-2">
    <div class="flex-1 rounded bg-orange-500 p-2">flex-1</div>
    <div class="flex-1 rounded bg-orange-500 p-2">flex-1</div>
    <div class="flex-[2] rounded bg-orange-700 p-2">flex-[2] (twice as wide)</div>
  </div>
  <div class="flex gap-2">
    <div class="order-last rounded bg-slate-600 p-2">First in HTML, order-last</div>
    <div class="rounded bg-slate-800 p-2">Second in HTML</div>
  </div>
</body>
</html>

A real component: media object

The classic "image beside text" layout uses shrink-0 on the avatar and min-w-0 on the text so long words can truncate instead of overflowing.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="bg-gray-100 p-4">
  <div class="flex max-w-sm items-center gap-3 rounded-xl bg-white p-3 shadow">
    <div class="flex size-12 shrink-0 items-center justify-center rounded-full bg-amber-300 text-xl">🦊</div>
    <div class="min-w-0 flex-1">
      <p class="truncate font-semibold">Felix the very-long-named fox developer</p>
      <p class="truncate text-sm text-gray-500">felix.fox@a-really-long-domain-example.dev</p>
    </div>
    <button class="shrink-0 rounded-full bg-gray-900 px-3 py-1 text-sm text-white">Follow</button>
  </div>
</body>
</html>

Tip: For perfect centring of anything, the trio is flex items-center justify-center (or grid place-items-center in a grid).

Test yourself

Which classes put items in a column and centre them horizontally?

  1. flex flex-col items-center
  2. flex flex-col justify-center
  3. flex items-center
  4. grid flex-col

An avatar keeps getting squashed in a flex row. Which class fixes it?

  1. grow
  2. shrink-0
  3. flex-1
  4. basis-full

What is the v4 replacement for flex-grow?

  1. grow
  2. flex-g
  3. expand
  4. flex-grow still works