Connect with us

CSS

How to Align Text and Icon on the Same Line with Tailwind CSS

Spread the love

Aligning text and icons on the same line is a common UI pattern—used in buttons, headers, navigation links, and feature lists. With Tailwind CSS, you can easily achieve this with utility classes that control layout, spacing, and alignment.

In this guide, you’ll learn how to:

  • Place icons and text side-by-side
  • Vertically align them
  • Add spacing between them
  • Keep them responsive and clean

✅ Basic Alignment Using Flexbox

Flexbox is the go-to solution for aligning inline elements like icons and text.

📌 Example:

<div class="flex items-center space-x-2">
  <svg class="w-5 h-5 text-blue-500" fill="currentColor" viewBox="0 0 20 20">
    <path d="..." />
  </svg>
  <span class="text-gray-700">Download</span>
</div>

🔍 Explanation:

  • flex: Arranges icon and text in a row
  • items-center: Vertically aligns icon and text
  • space-x-2: Adds horizontal spacing between them
  • w-5 h-5: Sets icon size
  • text-blue-500: Colors the icon

✅ Perfect for buttons, links, tooltips, and navigation.


🎯 Example Use Case: Button with Icon

<button class="flex items-center space-x-2 bg-blue-600 text-white px-4 py-2 rounded">
  <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
    <path d="..." />
  </svg>
  <span>Submit</span>
</button>

💡 Wrap text and icon in a flex container and use space-x-* to control spacing.


🪄 Alternative: Using inline-flex for Inline Elements

If you want the icon-text pair to behave like a single inline element (e.g., inside a paragraph or list), use inline-flex.

📌 Example:

<p>
  Click here 
  <span class="inline-flex items-center space-x-1 text-blue-600 underline ml-1">
    <span>Learn more</span>
    <svg class="w-4 h-4" fill="currentColor" viewBox="0 0 20 20">
      <path d="..." />
    </svg>
  </span>
</p>

✅ Best for links or inline messages with icons.


🔁 Flip Order (Icon After Text)

Simply reverse the HTML order or use flex-row-reverse.

<div class="flex items-center space-x-reverse space-x-2 flex-row-reverse">
  <svg class="w-5 h-5" ...></svg>
  <span>Next</span>
</div>

🔍 Tailwind Tips for Text + Icon Alignment

GoalTailwind Utility
Align verticallyitems-center
Arrange horizontallyflex or inline-flex
Add spacingspace-x-*
Control icon sizew-4 h-4 (or any size)
Change order (icon after)flex-row-reverse or reorder HTML
Inline layout inside textinline-flex

📝 Conclusion

Tailwind CSS makes it effortless to align text and icons on the same line, vertically and horizontally. Whether you’re building buttons, headers, or lists, flex utilities offer clean, reusable layout solutions that adapt beautifully across screen sizes.


Spread the love
Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *