Lesson 6
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 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.
Before going further, a few terms come up all the time and are worth getting clear:
flex class.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.
flex-row: items aligned horizontally (default)flex-col: items stacked verticallyflex-wrap: allows items to wrap onto a new line if they do not fitThe 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.
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-endjustify-between: first item pushed to the start, last item to the end, with the space distributed between themitems-start / items-center / items-enditems-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.
<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>
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.
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.
The flex-1, flex-auto andflex-none classes control an item's ability to grow or shrink to fill the available space:
<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).
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-*.
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).
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.
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.
flex on the parent : thejustify-* and items-* classes have no effect withoutflex (or grid) on the container.flex-1 orself-* go on the children.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
flex, gap-4 anditems-center. Then add flex-col to see the layout switch to a column, then go back toflex-row.