Three easy steps to use Font Awesome in Angular
Posted
Updated
Europe’s developer-focused job platform
Let companies apply to you
Developer-focused, salary and tech stack upfront.
Just one profile, no job applications!
This article is based on Angular CLI 15.1.4, Node 18.13.0, npm 8.19.3
FontAwesome is a popular icon library that provides scalable vector icons.
It's easy to use FontAwesome in Angular with the @fortawesome/angular-fontawesome
package,
which provides Angular components for FontAwesome icons. This article is about how to use fontawesome in your Angular project.
💰 The Pragmatic Programmer: journey to mastery. 💰 One of the best books in software development, sold over 200,000 times.
If you want to know how to install, head over to How to install fontawesome in Angular.
After successfully installing fontawesome in your Angular project. Follow these three steps:
FontAwesomeModule
.app.module.ts
Add FontAwesomeModule
to imports
in src/app/app.module.ts
.
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';
@NgModule({
imports: [BrowserModule, FontAwesomeModule],
declarations: [AppComponent],
bootstrap: [AppComponent],
})
export class AppModule {}
Bind the icon you want to use to a variable in the component, where the icon should be used,
for example src/app/app.component.ts
.
import { Component } from '@angular/core';
import { faCoffee } from '@fortawesome/free-solid-svg-icons';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
})
export class AppComponent {
faCoffee = faCoffee;
}
Provide the icon to the icon
property in fa-icon
component.
<fa-icon [icon]="faCoffee"></fa-icon>
Instead of using property binding you can also supply an array with string values directly in your template. This is useful when layering icons, see Fontawesome Docs - Layering.
<fa-layers [fixedWidth]="true">
<fa-icon [icon]="['fas', 'square']"></fa-icon>
<fa-icon
[inverse]="true"
[icon]="['fas', 'spinner']"
transform="shrink-6"
></fa-icon>
</fa-layers>
icon
property you want to use with fa-icon
component in the template.Thanks for reading and if you have any questions, use the comment function or send me a message @mariokandut. If you want to know more about Angular, have a look at these Angular Tutorials.
References (and Big thanks): Angular, fontawesome Angular Component for Font Awesome
Never miss an article.