Whether you’re building a button, a notification, or a navigation link, aligning text and an icon on the same line is a common UI pattern. Tailwind CSS makes this task incredibly simple and flexible, thanks to its utility-first classes.
In this blog, weβll explore how to align text and icons inline using Tailwind CSS, including different layout options and best practices for spacing, sizing, and alignment.
π― Goal: Text and Icon on the Same Horizontal Line
Hereβs what we want to achieve:
[π] Notifications
The icon should sit neatly next to the text, vertically aligned in the center.
β
Method 1: Flexbox with flex items-center
Best For: Buttons, nav links, badges, and labels.
π§© Example:
<button class="flex items-center space-x-2 text-sm text-blue-600">
<svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20">
<path d="..." />
</svg>
<span>Notifications</span>
</button>
π Key Tailwind Classes:
flex
: Puts the icon and text in a horizontal row.items-center
: Vertically aligns icon and text in the center.space-x-2
: Adds consistent horizontal space between the icon and text.w-5 h-5
: Ensures consistent icon sizing.
β Result: Perfect alignment and spacing on the same line, with full control over styling.
β Method 2: Inline-Flex for Inline Elements
Best For: When placing inline items (e.g., inside text paragraphs or inline links).
<a href="#" class="inline-flex items-center text-gray-700 hover:text-black">
<svg class="w-4 h-4 mr-1" fill="currentColor" viewBox="0 0 20 20">
<path d="..." />
</svg>
<span>Learn More</span>
</a>
π Notes:
inline-flex
: Makes the element behave like inline text but still use Flexbox.mr-1
: Adds space between the icon and text manually (useful for small inline use cases).
β Method 3: Using Icon Libraries (e.g., Heroicons, Font Awesome)
When using libraries like Heroicons:
<div class="flex items-center text-gray-800">
<span class="material-icons text-xl mr-2">check_circle</span>
Success
</div>
Tips:
- Control vertical alignment with
items-center
. - Set icon size using
text-*
classes (text-xl
,text-sm
, etc.) if it’s a font icon. - Use
mr-1
,ml-1
, orspace-x-*
for spacing.
βοΈ Bonus: Reversing Icon and Text
If you want the text to come before the icon:
<div class="flex items-center space-x-reverse space-x-2">
<span>Download</span>
<svg class="w-4 h-4" fill="currentColor" viewBox="0 0 20 20">
<path d="..." />
</svg>
</div>
Use:
space-x-reverse
: Reverses the direction of spacing when the order is swapped.- Place icon after the text.
π¨ Customization Tips
Requirement | Class to Use |
---|---|
Vertical alignment | items-center |
Horizontal spacing | space-x-* or mr-1 |
Icon sizing | w-4 h-4 , text-xl , etc. |
Inline usage | inline-flex |
Icon before or after | Change DOM order + use space-x-reverse |
β Common Mistakes to Avoid
- β Using plain
inline
withoutinline-flex
β alignment wonβt work as expected. - β Forgetting to size the icon β which can make it misaligned or oversized.
- β Not using
items-center
β can cause vertical misalignment between text and icon.
β Summary
Tailwind CSS makes aligning text and icons simple, clean, and responsive. Here’s what to remember:
- Use
flex
orinline-flex
withitems-center
for alignment. - Add spacing with
space-x-*
,mr-*
, orml-*
. - Size icons using
w-*
,h-*
, ortext-*
.
π¬ Final Thoughts
Aligning icons and text is a tiny detail β but it makes a big impact on the overall polish of your UI. With Tailwind CSS, itβs just a matter of combining the right utilities.