Skip to main content

How to Set Gradient Background in Font Awesome Icons

How to Set Gradient Background in Font Awesome Icons

Font Awesome icons are widely used in web development to enhance UI design. By default, these icons are single-colored, but with CSS, we can apply gradient backgrounds to make them visually appealing. In this article, we'll explore how to set gradient backgrounds in Font Awesome icons using CSS.


01. Setting Up Font Awesome

Before applying a gradient background, ensure that Font Awesome is included in your project. You can use a CDN to include it in your HTML file:

<!-- Include Font Awesome CDN -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">

Now, you can use Font Awesome icons in your HTML:

<!DOCTYPE html> 
<html lang="en">
<head>
    <title>Font Awesome Solid Star Icon</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" rel="stylesheet">
</head>
<body>
  
    <i class="fa-solid fa-star"></i>

</body>
</html>

Output


02. Applying Gradient Background to Icons

To apply a gradient, we need to:

  • Set the icon's background to a gradient.
  • Use -webkit-background-clip: text; to apply the gradient to the text.
  • Set color: transparent; to hide the original color.

CSS for Gradient Background

.gradient-icon {
    font-size: 50px;
    background: linear-gradient(45deg, #ff0000, #ff7300, #ffeb00);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

Applying to an Icon

<i class="fa-solid fa-star gradient-icon"></i>

Output


03. Adding Animation to Gradient Icons

We can enhance the effect by animating the gradient background.

CSS for Animated Gradient

@keyframes gradientShift {
    0% { background-position: 0% 50%; }
    50% { background-position: 100% 50%; }
    100% { background-position: 0% 50%; }
}

.animated-gradient-icon {
    font-size: 50px;
    background: linear-gradient(45deg, #ff0000, #ff7300, #ffeb00, #ff0000);
    background-size: 300% 300%;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    animation: gradientShift 3s infinite linear;
}

Applying to an Icon

<i class="fa-solid fa-heart animated-gradient-icon"></i>

Output


04. Conclusion

By leveraging CSS properties like -webkit-background-clip and animations, you can create stunning gradient effects on Font Awesome icons. These techniques help improve the visual appeal of your web designs while keeping performance optimized.

Comments