agyanadda@gmail.com

Tailwind Flexbox

Tailwind Layout 1 min read Updated Dec 1, 2026 Topic 11 of 27

Add flex to a parent to line up its children.

Container classes

Class CSS
flex / inline-flex display: flex
flex-row / flex-col direction
justify-start / -center / -between / -end main axis
items-start / -center / -end / -stretch cross axis
flex-wrap allow wrapping
gap-4 space between items

Item classes

  • flex-1 — grow to fill free space
  • shrink-0 — never shrink (great for images and icons)
  • grow — grow if there is space
  • order-first, order-last
  • self-center, ml-auto (push to the right)

Perfect centre

flex items-center justify-center

Responsive

flex flex-col md:flex-row stacks on phones and lines up on desktops.

Example

Example — Navbar and media object with Flexbox HTML
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>

<div class="p-6 space-y-6">
  <nav class="flex items-center justify-between bg-slate-900 text-white px-4 py-3 rounded-lg">
    <span class="font-bold">AgyanAdda</span>
    <div class="flex gap-4 text-sm">
      <a href="#" class="hover:text-sky-400">Home</a>
      <a href="#" class="hover:text-sky-400">Tutorials</a>
      <a href="#" class="hover:text-sky-400">Contact</a>
    </div>
  </nav>

  <div class="flex gap-4 items-start border rounded-lg p-4">
    <img src="https://picsum.photos/id/64/100/100" class="size-16 rounded-full shrink-0" alt="Student">
    <div class="flex-1">
      <h3 class="font-semibold">Asha Kumari</h3>
      <p class="text-slate-600 text-sm">"The Tailwind lessons helped me build my portfolio in one week."</p>
    </div>
  </div>

  <div class="flex flex-col md:flex-row gap-4">
    <div class="flex-1 bg-emerald-100 p-4 rounded">Stacks on mobile</div>
    <div class="flex-1 bg-emerald-200 p-4 rounded">Row on desktop</div>
  </div>
</div>

Test Yourself With Exercises

Exercise 1 Multiple Choice

Which class vertically centres flex items in a row?

Exercise 2 True / False

flex-1 makes an item grow to fill the available space.

Exercise 3 Fill in the Blank

justify-___ puts equal space between items with none at the edges.

Frequently Asked Questions

Flex items can shrink by default. Add shrink-0 to the image.