Ch 11 / 45
Chapter 11 Intermediate 9 min read Tailwind CSS

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.

Advertisement
Google Ad Placeholder · 728×90 Leaderboard
What you'll learn
What display: flex and inline-flex do, and how they differ from block/inline-block
Controlling layout direction with flex-row, flex-row-reverse, flex-col, flex-col-reverse
Aligning children along the main axis with justify-content
Aligning children along the cross axis with align-items and align-content
Wrapping flex children onto multiple lines with flex-wrap
Real layout patterns — navbars, centering, sidebars, and button groups

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.

Block layout vs Flexbox layout
<!-- 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>
flex
display: flex — a block-level flex container. Takes up the full width available, like a normal <div>.
inline-flex
display: inline-flex — an inline-level flex container that only takes up as much width as its content needs.
flex-row
Children flow left-to-right. This is Tailwind's default direction — you rarely need to write it explicitly.
flex-col
Children flow top-to-bottom instead — turns the main axis vertical and the cross axis horizontal.
Tip 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.

Justify-content classes — a quick reference
justify-start
Default. Packs children toward the start of the main axis, leaving empty space at the end.
justify-end
Packs children toward the end of the main axis, leaving empty space at the start.
justify-center
Groups children together in the centre of the main axis, with equal space on both sides.
justify-between
First child at the very start, last child at the very end, remaining space split evenly between the rest. The classic navbar pattern.
justify-around
Equal space around each child — so gaps at the edges end up half the size of the gaps between items.
justify-evenly
Every gap — including the outer edges — is exactly the same size.
justify-content in practice
<!-- 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.

Align-items classes — a quick reference
items-start
Aligns children to the start of the cross axis — the top edge in a row layout.
items-end
Aligns children to the end of the cross axis — the bottom edge in a row layout.
items-center
Vertically centres children — by far the most-used align utility in real UIs.
items-baseline
Aligns children by their text baseline — useful when mixing different font sizes in one row.
items-stretch
Default. Stretches children to fill the container's cross axis — every card in a row ends up the same height.

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.

align-items in practice
<!-- 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.
A wrapping tag list
<!-- 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>
Note 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:

Live Preview
Brand
Home Pricing Contact
Sign Up

flex items-center justify-center — perfectly centred content:

Live Preview
Centred on both axes

flex flex-wrap gap-2 — items wrapping onto new lines:

Live Preview
Tailwind CSS Flexbox Responsive Design Utility-First Dark Mode Grid

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.

Responsive Navbar
flex justify-between items-center pins the logo left and links right, vertically centred.
Centered Modal / Hero
flex items-center justify-center h-screen centres content on both axes — no margin math required.
Sidebar + Main Content
flex on the wrapper + w-64 shrink-0 on the sidebar keeps it a fixed width while main content flexes.
Button Group
flex gap-2 keeps action buttons evenly spaced and aligned in a toolbar.
Card Row / Product Grid
flex flex-wrap gap-4 lets product cards reflow naturally as the viewport shrinks.
Sticky Footer Layout
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
Featured Course
Master Tailwind CSS with Real Projects(React Course)
Go from utility-class beginner to building production-grade UIs. Our structured course covers Tailwind v4, Vite integration, custom themes, component patterns, and 5 real projects including a full SaaS landing page and dashboard.
45 Chapters 10+ Hours of Content 5 Real Projects Certificate Included Live Mentoring
🎨
⭐ 4.9 · 1,200+ students
Tailwind CSS Flexbox Cheat Sheet
Download our free one-page cheat sheet covering every flex-direction, justify-content, align-items, align-content, and flex-wrap utility class with CSS equivalents.
Flexbox Justify-Content Align-Items Flex-Wrap Gap Layout
Chapter Summary
  • Flexbox is a one-dimensional layout model with a main axis and a cross axis — add flex or inline-flex to any element to turn it into a flex container.
  • Direction is controlled with flex-row (default) and flex-col, plus their -reverse variants.
  • 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, order and align-self for individual flex children.

Quick Check — Questions & Answers

Test your understanding of this chapter. Click a question to reveal the answer.

Both create a flex container and turn direct children into flex items — the difference is how the container itself behaves in the surrounding layout. 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.
Combine three classes on the parent: 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.
They align along different axes. justify-content controls spacing along the main axis — the direction items flow in (horizontal for 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.
Add 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.

Question 1 of 4
Which class turns an element into a flex container while keeping the container itself inline-level (sized to its content)?
Question 2 of 4
Which class puts equal space between children only — with no space at the very start or end of the container?
Question 3 of 4
Which class stacks flex children vertically, top to bottom, instead of the default horizontal row?
Question 4 of 4
Which class allows flex children to move onto a new line when they no longer fit in one row?

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel