Lesson 6 / 1250%

Lesson 6

Flexbox with Tailwind

25 min

Align, distribute and order elements along an axis with Flexbox.

Definition

Flexbox ("Flexible Box Layout") is a CSS layout module that aligns, distributes and sizes elements along a single axis , horizontal or vertical. An element becomes a flex container withdisplay: flex;, which Tailwind boils down to the simple classflex. Its direct children then become flex items .

Flexbox with Tailwind

Flexbox is probably the most widely used layout tool day to day: navigation bars, aligned cards, grouped buttons, vertical centering... Tailwind exposes all the main Flexbox properties as simple utility classes.

Where classic CSS requires you to write several properties (display, justify-content,align-items...), Tailwind lets you chain them in a few short words directly in your HTML. Once you understand the vocabulary, a complete layout can be written in a few seconds.

The Flexbox vocabulary

Before going further, a few terms come up all the time and are worth getting clear:

  • Flex container : the parent that carries the flex class.
  • Flex items : the direct children of the container (not the grandchildren).
  • Main axis : the direction in which items are aligned (horizontal by default).
  • Cross axis : the axis perpendicular to the main axis.

Enabling Flexbox

Everything starts with the flex class on the parent container, which turns its direct children into flexible items lined up in a row by default:

<div class="flex">
  <div>Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
</div>

There is also inline-flex, which creates a flex container that behaves like an inline element: it only takes up the width of its content instead of the full available width. This is handy for a button with an icon.

Axis direction: flex-row and flex-col

  • flex-row: items aligned horizontally (default)
  • flex-col: items stacked vertically
  • flex-wrap: allows items to wrap onto a new line if they do not fit

The flex-row-reverse and flex-col-reverse variants reverse the display order, and flex-nowrap (the default behavior) prevents wrapping. Be careful: when switching toflex-col, the main axis becomes vertical. Thejustify-* classes then act from top to bottom, anditems-* from left to right.

Alignment: justify-* and items-*

Two families of classes control the alignment of items: one for the main axis (justify-*), one for the perpendicular axis (items-*).

  • justify-start / justify-center / justify-end
  • justify-between: first item pushed to the start, last item to the end, with the space distributed between them
  • items-start / items-center / items-end
  • items-stretch: items stretch to fill the full available height (default behavior)

Two other distributions exist for the main axis:justify-around places equal space around each item, and justify-evenly distributes the space strictly identically between all the items and the edges. For the cross axis,items-baseline aligns items on the baseline of their text.

A classic navigation bar
<nav class="flex items-center justify-between px-6 py-4">
  <span class="font-bold text-lg">MyWebsite</span>
  <div class="flex items-center gap-6">
    <a href="/">Home</a>
    <a href="/blog">Blog</a>
    <a href="/contact">Contact</a>
  </div>
</nav>

Result

Preview of a navigation bar with the site name on the left and the links on the right

Spacing items with gap

To separate the items of a flex container, use thegap-* classes rather than margins on each child. You can also target a single axis with gap-x-* (horizontal space) orgap-y-* (vertical space), which is very useful withflex-wrap.

Perfectly centering an element

The most sought-after combo in CSS — centering an element horizontally and vertically — becomes trivial with Flexbox and Tailwind:

<div class="flex items-center justify-center h-screen">
  <p class="text-2xl font-bold">Perfectly centered!</p>
</div>

Note the role of h-screen: without a defined height, the container is no taller than its content, so the vertical centering would have no visible effect.

Controlling the size of each flexible item

The flex-1, flex-auto andflex-none classes control an item's ability to grow or shrink to fill the available space:

A layout with a fixed sidebar and expandable content
<div class="flex">
  <aside class="w-64 flex-none bg-slate-100 p-4">
    Side menu (fixed width)
  </aside>
  <main class="flex-1 p-6">
    The main content takes up all the remaining space.
  </main>
</div>

For finer control, grow allows an item to expand, grow-0 prevents it, and shrink-0prevents it from shrinking (useful so an image or icon keeps its size in a narrow container).

Aligning a single item with self-*

Sometimes a single child needs to depart from the group's alignment. Theself-start, self-center,self-end and self-stretch classes go on the item concerned and override the parent's items-*.

Ordering items with order-*

You can visually change the order of items without touching the HTML, thanks to order-1, order-2, etc. Very useful for displaying an element differently on mobile and desktop (we will see this in detail in the lesson on responsive design).

Example: a row of cards that wrap onto new lines

By combining flex-wrap, gap-4 and a minimum width on each card, you get a flexible grid that adapts to the screen size:

<div class="flex flex-wrap gap-4">
  <div class="flex-1 min-w-48 p-4 rounded-lg bg-slate-100">Card 1</div>
  <div class="flex-1 min-w-48 p-4 rounded-lg bg-slate-100">Card 2</div>
  <div class="flex-1 min-w-48 p-4 rounded-lg bg-slate-100">Card 3</div>
</div>

Each card stretches thanks to flex-1 but never goes below 12rem (min-w-48). When space runs out, the cards naturally wrap onto the next line.

A common use case: a button with an icon

inline-flex and items-center work very well together to neatly align an icon and a text inside a button:

<button class="inline-flex items-center gap-2 px-4 py-2 rounded-lg bg-indigo-600 text-white">
  <svg class="size-5 shrink-0" viewBox="0 0 20 20" fill="currentColor"></svg>
  Download
</button>

The icon and the text are vertically centered and 8px apart, andshrink-0 prevents the icon from being squashed if the button runs out of space.

Common beginner mistakes

  • Forgetting flex on the parent : thejustify-* and items-* classes have no effect withoutflex (or grid) on the container.
  • Applying classes to the wrong element : alignment is set on the container, whereas flex-1 orself-* go on the children.
  • Mixing up justify and items : in flex-row,justify is horizontal; in flex-col, it is the opposite.

Flexbox excels at aligning items along a single axis. For layouts with rows and columns at the same time, Tailwind also offers CSS Grid, which you will discover in the next lesson.

Practical exercise

Recreate a product card with an image on the left and text content on the right, using flex, gap-4 anditems-center. Then add flex-col to see the layout switch to a column, then go back toflex-row.