Flexbox in Tailwind CSS
Flexbox is the single most useful layout tool in modern CSS — it powers navbars, card rows, centered modals, sidebars, and button groups with just a handful of classes. Tailwind's Flexbox utilities map every CSS flex property directly to a class name, so you can build complex one-dimensional layouts without writing a single line of custom CSS. In this chapter you'll learn how to turn any element into a flex container, control its direction and wrapping, and align children along both the main and cross axes.
display: flex and inline-flex do,
and how they differ from block/inline-block
What is Flexbox in Tailwind CSS?
Flexbox is a one-dimensional layout model — it arranges children in a single row or column and gives you precise control over spacing and alignment along that line. Every flex layout has a main axis (the direction items flow) and a cross axis (perpendicular to it). Tailwind turns every flexbox concept into a utility class you apply directly to the container.
To start, add
flex or
inline-flex to an element —
this makes it a flex container, and every direct
child automatically becomes a flex item that can
be aligned, reordered, and resized using the utilities covered in
this chapter and the next.
<!-- Without flex — each div is a block, stacked vertically -->
<div class="bg-gray-100 p-4">
<div class="bg-cyan-500 p-4 rounded-lg">One</div>
<div class="bg-purple-500 p-4 rounded-lg">Two</div>
<div class="bg-orange-500 p-4 rounded-lg">Three</div>
</div>
<!-- With flex — children sit in a row, aligned with gap -->
<div class="flex gap-4 bg-gray-100 p-4">
<div class="bg-cyan-500 p-4 rounded-lg">One</div>
<div class="bg-purple-500 p-4 rounded-lg">Two</div>
<div class="bg-orange-500 p-4 rounded-lg">Three</div>
</div>
display: flex — a
block-level flex container. Takes up the full width available,
like a normal <div>.
display: inline-flex
— an inline-level flex container that only takes up as much
width as its content needs.
flex-row is the default, so
you only need to write it when overriding
flex-col at a larger
breakpoint — e.g.
flex-col md:flex-row stacks on
mobile and goes horizontal on tablets and up.
Justify Content — Aligning the Main Axis
justify-* classes control how
children are spaced along the main axis —
horizontally in a
flex-row container, or
vertically in a flex-col
one.
<!-- Navbar pattern: logo left, links right -->
<nav class="flex justify-between items-center p-4">
<span class="font-bold text-xl">Logo</span>
<div class="flex gap-6">
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Contact</a>
</div>
</nav>
<!-- Centered call-to-action row -->
<div class="flex justify-center gap-4">
<button class="bg-cyan-500 text-white px-6 py-2 rounded-lg">Get Started</button>
<button class="border border-gray-300 px-6 py-2 rounded-lg">Learn More</button>
</div>
Align Items & Align Content — the Cross Axis
items-* classes control how
children are aligned along the cross axis —
vertically in a
flex-row container. It's the
single most common fix for "why isn't my icon centred next to my
text" layout bugs.
When a flex container
wraps onto multiple lines (with
flex-wrap),
align-content-* classes (content-start, content-between,
content-center, etc.) control
how those wrapped lines are spaced — the multi-line
equivalent of justify-content.
<!-- Icon + text, vertically centred -->
<div class="flex items-center gap-2">
<svg class="w-5 h-5">...</svg>
<span>Verified account</span>
</div>
<!-- Perfectly centred modal / hero content -->
<div class="flex items-center justify-center h-screen">
<div class="bg-white rounded-2xl p-8 shadow-xl">
Centred both horizontally and vertically
</div>
</div>
Flex Wrap — Multi-Line Layouts
By default, flex children try to squeeze onto a
single line, shrinking if necessary. When you
have more items than fit — a row of tags, a grid of cards on
mobile — flex-wrap lets them
flow onto additional lines instead of overflowing or crushing
together.
| Class | CSS Equivalent | Behaviour |
|---|---|---|
| flex-nowrap | flex-wrap: nowrap | Default. Children stay on one line, shrinking to fit. |
| flex-wrap | flex-wrap: wrap | Children wrap onto new lines top-to-bottom once space runs out. |
| flex-wrap-reverse | flex-wrap: wrap-reverse | Same as wrap, but new lines stack bottom-to-top instead. |
<!-- Tags wrap onto new lines instead of overflowing -->
<div class="flex flex-wrap gap-2">
<span class="bg-cyan-100 text-cyan-700 px-3 py-1 rounded-full text-sm">Tailwind</span>
<span class="bg-purple-100 text-purple-700 px-3 py-1 rounded-full text-sm">Flexbox</span>
<span class="bg-orange-100 text-orange-700 px-3 py-1 rounded-full text-sm">Responsive</span>
<span class="bg-green-100 text-green-700 px-3 py-1 rounded-full text-sm">Utility-First</span>
<span class="bg-pink-100 text-pink-700 px-3 py-1 rounded-full text-sm">Dark Mode</span>
</div>
gap-* works perfectly with
flex-wrap — it adds equal
spacing both between items on the same line and between
wrapped lines, without any of the extra margin-collapsing hacks
the old space-x-*
approach needed.
Live Examples — See Flexbox in Action
These boxes are rendered with real Tailwind utility classes right on this page — resize your browser to see the wrap behaviour change.
flex justify-between items-center
— the classic navbar pattern:
flex items-center justify-center
— perfectly centred content:
flex flex-wrap gap-2
— items wrapping onto new lines:
Common Real-World Use Cases
Flexbox shows up in nearly every component you'll build. Here are the layout patterns you'll reach for most often.
flex justify-between items-center
pins the logo left and links right, vertically centred.
flex items-center justify-center h-screen
centres content on both axes — no margin math required.
flex on the wrapper +
w-64 shrink-0 on the
sidebar keeps it a fixed width while main content flexes.
flex gap-2 keeps action
buttons evenly spaced and aligned in a toolbar.
flex flex-wrap gap-4
lets product cards reflow naturally as the viewport shrinks.
flex flex-col min-h-screen
on <body> pins the footer to the bottom
even on short pages.
Quick Reference — Flexbox Classes
| Class | CSS Equivalent | Description |
|---|---|---|
| flex | display: flex | Block-level flex container |
| inline-flex | display: inline-flex | Inline-level flex container |
| flex-row | flex-direction: row | Default — children flow left to right |
| flex-row-reverse | flex-direction: row-reverse | Children flow right to left |
| flex-col | flex-direction: column | Children flow top to bottom |
| flex-col-reverse | flex-direction: column-reverse | Children flow bottom to top |
| flex-wrap | flex-wrap: wrap | Children wrap onto new lines as needed |
| flex-nowrap | flex-wrap: nowrap | Default — children stay on one line |
| justify-start | justify-content: flex-start | Pack children at the start of the main axis |
| justify-center | justify-content: center | Group children in the centre |
| justify-between | justify-content: space-between | Equal space between children only |
| justify-evenly | justify-content: space-evenly | Every gap — including edges — is equal |
| items-center | align-items: center | Centre children on the cross axis |
| items-stretch | align-items: stretch | Default — children fill the cross axis |
| content-between | align-content: space-between | Spaces wrapped lines evenly (multi-line only) |
| gap-4 | gap: 1rem | Spacing between flex children, both axes |
| shrink-0 | flex-shrink: 0 | Prevents an item from shrinking below its size |
-
Flexbox is a one-dimensional layout model with
a main axis and a cross axis —
add
flexorinline-flexto any element to turn it into a flex container. -
Direction is controlled with
flex-row(default) andflex-col, plus their-reversevariants. - justify-content aligns children along the main axis; align-items aligns them along the cross axis — the two together can centre anything.
-
align-content only matters once a container has
wrapped onto multiple lines with
flex-wrap. -
flex-wrap lets children flow onto new lines
instead of shrinking or overflowing — pair it with
gap-*for clean, even spacing. - Real-world patterns include navbars, centred modals, sidebar layouts, button groups, card rows, and sticky footers.
-
Next chapter continues in the Flexbox &
Grid group — Flex Items — covering
flex-grow,flex-shrink,flex-basis,orderandalign-selffor individual flex children.
Quick Check — Questions & Answers
Test your understanding of this chapter. Click a question to reveal the answer.
flex behaves like a block element, taking the
full width available and starting on its own line.
inline-flex behaves like an inline element,
shrinking to fit its content and sitting alongside adjacent
text or elements — useful for things like a tag or badge
with an icon and label inside it.
flex items-center justify-center. Give the
parent a height (like h-screen or a fixed
height) so there's room to centre within.Example:
<div class="flex items-center justify-center
h-screen"><div>Centered!</div></div>
— justify-center handles the horizontal
centring, items-center handles the vertical.
flex-row, vertical for
flex-col).
align-items controls alignment along the
cross axis — perpendicular to the main
axis.In a default
flex-row container,
justify-content moves things left/right and
align-items moves things up/down. In a
flex-col container, those roles swap.
flex-row-reverse (or
flex-col-reverse for a column layout) to the
parent. The DOM order — and so the
reading/accessibility order — stays exactly
the same; only the visual, on-screen order flips.This is handy for things like putting an image on the right on desktop while keeping it first in the markup for mobile, without duplicating HTML.
flex-wrap only controls
whether children wrap onto new lines — it
says nothing about spacing between those lines or between
items on the same line. Without any spacing utility, wrapped
items can end up touching each other.The fix is adding
gap-* (e.g. gap-4) to the same
container. Unlike the old space-x-* /
space-y-* approach, gap applies
consistently to both the horizontal spacing between items
and the vertical spacing between wrapped lines.
MCQ Quiz — Test Yourself
Select the best answer for each question. Correct answers are revealed after you click.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.