Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Font Awesome 5 with Angular

How do I use font-awesome 5 with Angular (2+)?

I've tried adding this inside a component:

import {faChevronLeft, faChevronRight} from '@fortawesome/fontawesome-free-solid';
import fontawesome from '@fortawesome/fontawesome';
...
constructor(){
   fontawesome.library.add(faChevronLeft, faChevronRight);
}

and then in HTML:

<span class="fa" [class.fa-chevron-left]="direction==='left'" [class.fa-chevron-right]="direction==='right'"></span>

But this gives me a blinking question mark in a circle.

like image 788
JeB Avatar asked Dec 29 '17 19:12

JeB


People also ask

Can I use Font Awesome in angular?

On top of this, features like icon font ligatures, an SVG framework, official npm packages for popular front-end libraries, such as React, and access to a new CDN. In this post, I am going to show you how to configure Font Awesome to be used in Angular in only 5 steps.

How to use angular-fontawesome in stackblitz?

angular-fontawesome 1 Installation 2 Usage. Add FontAwesomeModule to imports in src/app/app.module.ts: Tie the icon to the property in your component src/app/app.component.ts: 3 Documentation 4 Examples. Here's a StackBlitz Starter Sample on how to display Solid, Regular, and Brand icons using the Icon Library. 5 Contributing 6 Contributors. ...

What is Font Awesome and how to use it?

Font Awesome is a toolkit for icons and fonts, based on CSS and LESS. Font Awesome was created by Dave Gandy to use with Twitter Bootstrap and later it was incorporated into the Bootstrap CDN.

What makes the Font Awesome icons high-quality?

The icons were created using a scalable vector and inherit CSS sizes and color when applied to them. This makes them high-quality icons that work well on any screen size. Before the release of Angular 5, developers had to install the Font Awesome package and reference its CSS in an Angular project before it could be used.


2 Answers

You have two options:


1. Use angular-fontawesome library

Just follow the instructions on their github page.


2. Use fontawesome 5 directly

Make sure you have installed all the relevant npm packages.
For Pro packages check out this.

  1. Import relevant icons:

    import {faChevronLeft, faChevronRight} from '@fortawesome/fontawesome-free-solid';
    import fontawesome from '@fortawesome/fontawesome';
    
  2. Add the icons to fontawesome library in global scope (not inside the component's constructor):

    fontawesome.library.add(faChevronLeft, faChevronRight);
    
  3. Use it in html:

    <span class="fas" [class.fa-chevron-left]="direction==='left'" [class.fa-chevron-right]="direction==='right'"></span>
    
  4. Mind the prefixes in html:

    • fas for fontawesome-free-solid icons (works also with fa)

      <span class="fas fa-chevron-left"></span>
      
    • fab for fontawesome-free-brands icons

      <span class="fab fa-bitcoin"></span>
      
    • far for fontawesome-free-regular icons

      <span class="far fa-chevron-left"></span>
      
    • fal for fontawesome-free-light icons (pro)

      <span class="fal fa-chevron-left"></span>
      

Important note:

It's fine to use variables to define fontawesome classes as soon as it is done only once (at initialization). However, if the variable changes its value it won't be reflected in html. Consider this example:

<span class="fas fa-chevron-{{direction}}"></span>

This will put the right icon at the initialization time, but if the direction changes afterwards it won't be reflected.
The reason for this is that fontawesome 5 replaces the elements classed with fa ... with appropriate svg and once it is replaced no variable affects this.
If you want the above html to reflect runtime changes you have to change it like this:

<span *ngIf="direction==='right'"><span class="fas fa-chevron-right"></span></span>
<span *ngIf="direction==='left'"><span class="fas fa-chevron-left"></span></span>

The outer span is necessary as the inner span is replaced with svg so you can't put *ngIf on it.

Further reading:

  • Use fontawesome 5 with node.js
  • FontAwesome API
like image 184
JeB Avatar answered Oct 14 '22 23:10

JeB


The simplest way is to install it through npm and then import the styles:

1)

npm i @fortawesome/fontawesome-free --save

2) Import the styles in angular.json

"styles": [
  ...
  "node_modules/@fortawesome/fontawesome-free/css/all.css"
  ...
]

And then you can use it as it is in their documentation

<i class="fas fa-address-card"></i>

like image 27
Dino Avatar answered Oct 14 '22 21:10

Dino