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 spaceshrink-0— never shrink (great for images and icons)grow— grow if there is spaceorder-first,order-lastself-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>