How to Vertically Align the Text with a Large Font Awesome Icon?



This article provides a complete guide on how to Vertically Align Text with Large Font Awesome Icons with the help of CSS. it is very challenging when aiming for perfect vertical alignment. This article will explore various methods to vertically align text with a large Font Awesome icon using CSS.

Approaches to Vertically Align the Text with a Large Font Awesome Icon

There are three different approaches to vertically align the text content along with large font awesome icon using CSS.

Using Flexbox

Flexbox is a modern layout module in CSS that simplifies the alignment of elements both horizontally and vertically. It is highly effective for aligning text with icons. Here, we use display, align-items and margin-right property to vertically align the text and add spacing between the icon and text.

Example

In this example, CSS Flexbox is used to align the text with Large Font Awesome Icon Vertically.

<!DOCTYPE html>
<html lang="en">

<head>
    <title>Flexbox Alignment</title>
    <link rel="stylesheet" href=
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">

    <style>
        .flex-container {
            display: flex;
            align-items: center;
        }

        .icon {
            font-size: 68px;
            margin-right: 10px;
        }

        .text {
            font-size: 34px;
        }
    </style>
</head>

<body>
    <div class="flex-container">
        <i class="fas fa-star icon"></i>
        <span class="text">Star Rating</span>
    </div>
</body>

</html>

Using Inline-Block and Vertical Align

This method uses display: inline-block and vertical-align to align text with a large icon. It's useful for older browsers or simpler layouts.

Example

In this example, CSS display: inline-block; and vertical-align is used to Vertically align the text with Large Font Awesome Icon.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Inline-Block Alignment</title>
    <link rel="stylesheet" href=
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">

    <style>
        .container {
            font-size: 0;
        }
        .icon, .text {
            display: inline-block;
            vertical-align: middle;
            font-size: 68px;
        }
        .text {
            font-size: 34px;
            margin-left: 10px;
        }
    </style>
</head>

<body>
    <div class="container">
        <i class="fas fa-star icon"></i>
        <span class="text">Star Rating</span>
    </div>
</body>

</html>

Using Grid Layout

CSS Grid is another modern layout tool that provides powerful alignment capabilities. It can be used to align items within a grid cell. The used CSS grid properties are..

Example

In this example, CSS Grid Layout is used to Vertically align the text with Large Font Awesome Icon.

<!DOCTYPE html>
<html lang="en">

<head>
    <title>Grid Container Alignment</title>
    <link rel="stylesheet" href=
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">

    <style>
        .grid-container {
            display: grid;
            grid-template-columns: 100px auto;
            align-items: center;
            gap: 10px;
        }
        .icon {
            font-size: 68px; 
        }
        .text {
            font-size: 34px;
        }
    </style>
</head>

<body>
    <div class="grid-container">
        <i class="fas fa-star icon"></i>
        <span class="text">Star Rating</span>
    </div>
</body>

</html>
Updated on: 2024-08-26T09:37:05+05:30

182 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements