CSS
How to Align Items and Space with Flex in Tailwind CSS
Flexbox is one of the most powerful layout systems in CSS, and with Tailwind CSS, using it is incredibly easy and intuitive. Whether you’re building navbars, cards, buttons, or layouts, flex utilities allow you to align and space items precisely the way you want.
In this post, you’ll learn how to:
- Use Tailwind’s flex utilities
- Align items vertically and horizontally
- Add spacing between flex items
- Create responsive, clean layouts
🚀 Step 1: Enable Flex Container
Start by applying flex
to a container element:
<div class="flex">
<div>Item 1</div>
<div>Item 2</div>
</div>
Now the child elements are flex items.
📐 Step 2: Align Items Vertically (Cross Axis)
Tailwind uses items-*
utilities to control vertical alignment:
Class | Meaning |
---|---|
items-start | Align items to the top |
items-center | Align items to the center |
items-end | Align items to the bottom |
items-stretch | Stretch to fill container height |
items-baseline | Align to text baseline |
Example:
<div class="flex items-center h-24">
<div class="bg-blue-500">Center</div>
</div>
📏 Step 3: Align Items Horizontally (Main Axis)
Use justify-*
classes to control horizontal alignment:
Class | Meaning |
---|---|
justify-start | Align items to the left |
justify-center | Center items |
justify-end | Align items to the right |
justify-between | Space between items |
justify-around | Equal space around each item |
justify-evenly | Equal space between and around items |
Example:
<div class="flex justify-between">
<div>Left</div>
<div>Right</div>
</div>
🔄 Combine Alignment and Spacing
You can mix both items-*
and justify-*
for full control.
Example:
<div class="flex items-center justify-between h-20 px-4 bg-gray-100">
<span>Logo</span>
<nav class="flex space-x-4">
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Contact</a>
</nav>
</div>
✅ This layout horizontally spaces the nav items and centers them vertically inside the header.
🧱 Adding Gaps Between Flex Items
Instead of using margins, Tailwind provides space-x-*
and space-y-*
utilities to add consistent spacing.
Horizontal Spacing:
<div class="flex space-x-4">
<div>Item A</div>
<div>Item B</div>
<div>Item C</div>
</div>
Vertical Spacing (for flex-col
):
<div class="flex flex-col space-y-4">
<div>Item 1</div>
<div>Item 2</div>
</div>
📱 Responsive Flex Layouts
Tailwind makes it easy to switch layouts at different breakpoints:
<div class="flex flex-col md:flex-row items-center md:justify-between">
<div>Logo</div>
<div class="flex space-x-4">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
</div>
</div>
🧠
md:flex-row
changes layout to horizontal only on medium screens and above.
📝 Conclusion
Tailwind CSS simplifies the process of aligning and spacing items using Flexbox. Whether you’re centering content, distributing space evenly, or building responsive navigation bars, Tailwind gives you expressive, utility-first tools to create elegant and maintainable layouts.
🔑 Quick Cheatsheet
Task | Tailwind Class |
---|---|
Make element a flexbox | flex |
Vertical alignment | items-start/center/end |
Horizontal alignment | justify-start/center/end |
Add horizontal space | space-x-* |
Add vertical space | space-y-* |
Column direction | flex-col |
Responsive flex direction | sm:flex-row , md:flex-col , etc. |