Open In App

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

Last Updated : 14 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

TailwindCSS is a utility-first CSS framework that offers a wide range of classes to help style elements quickly and efficiently. It provides various classes to align text and icons on the same line, ensuring that both elements are visually harmonious and well-positioned.

These are the following approaches:

Using Flexbox

In this approach, we are using flex to create a flex container that aligns items on the same line. The items-center class vertically centers the icon and text, while space-x-2 provides horizontal spacing between them. The icon size is adjusted with text-3xl, and the text-blue-500 class colors the icon.

Example: This HTML document sets up a simple webpage using Tailwind CSS for styling and Font Awesome for icons, which include a centered title, a subtitle, and a Twitter follow button.

HTML
<!DOCTYPE html>
<html>
<head>
    <title>Using Flexbox</title>
    <link href=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet">
    <script src=
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/js/all.min.js"></script>
</head>

<body class="flex flex-col items-center 
justify-center min-h-screen bg-gray-100">
    <h1 class="text-green-500 
    text-4xl font-bold mb-4">
        GeeksforGeeks
    </h1>
    <h3 class="text-gray-700 
    text-2xl mb-6">
        Approach 1: Using Flexbox
    </h3>

    <div class="flex items-center space-x-2">
        <i class="fab fa-twitter
         text-blue-500 text-3xl">
        </i>
        <span class="text-lg 
        text-gray-700">
            Follow us on Twitter
        </span>
    </div>
</body>

</html>

Output:

OP1
Output

Using inline-block with align-middle

In this approach, we are using inline-block to make both the icon and text inline elements that respect their width and height. The align-middle class vertically aligns them to the middle of the line. The ml-2 class adds spacing between the icon and the text, and text-3xl adjusts the icon size.

Example: The below example uses inline-block with align-middle to Text and icon on the same line with Tailwind CSS.

HTML
<!DOCTYPE html>
<html>
<head>
    <title>Using Inline-Block and Align-Middle</title>
    <link href=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet">
    <script src=
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/js/all.min.js"></script>
</head>

<body class="flex flex-col 
items-center justify-center
 min-h-screen bg-gray-100">
    <h1 class="text-green-500
     text-4xl font-bold mb-4">
        GeeksforGeeks
    </h1>
    <h3 class="text-gray-700 text-2xl mb-6">
        Approach 2: Using Inline-Block and Align-Middle
    </h3>

    <div>
        <i class="fab fa-github
         text-gray-800 text-3xl 
         inline-block align-middle">
        </i>
        <span class="text-lg
         text-gray-700 inline-block 
         align-middle ml-2">
            Contribute on GitHub
        </span>
    </div>
</body>

</html>

Output:

OP2
Output

Next Article

Similar Reads