13
votes

I created a Blazor WebAssembly hosted template in .NET Core 3.1. Then right clicked on project.Client/wwwroot/css folder and clicked on Add client side library. Then selected the Font Awesome library and installed it. I added the below line to index.html <head>.

 <link href="css/font-awesome/css/fontawesome.css" rel="stylesheet"/>

I have libman.json of:

{
  "version": "1.0",
  "defaultProvider": "cdnjs",
  "libraries": [
    {
      "library": "[email protected]",
      "destination": "wwwroot/css/font-awesome/"
    }
  ]
}

I added just the below line to the default Blazor template page Counter.razor (Razor component). The IntelliSense finds the font:

@page "/counter"

<h1>Counter</h1>

<span class="fa fa-save"></span>

@code {}

but I only see a square:

enter image description here

4
I've added this script element copied from w3schools to the _Host.cshtml file <script src="https://kit.fontawesome.com/a076d05399.js"></script> And it works. I'm not familiar with these toys, but I guess you need to get a code like a076d05399 to enable it. I'm using here the code from a sample by w3schools. How it should be done when you use the files from wwwroot is not clear to me.enet
@Isaac I added your code to index.html file and worked. But as you said it is not using wwwroot content. This script pointed me to this interesting way of employing fontawesome blog.fontawesome.com/introducing-font-awesome-kitsSorush

4 Answers

12
votes

You also need to include the JavaScript.

<link rel="stylesheet" href="css/font-awesome/css/fontawesome.min.css" />
<script src="css/font-awesome/js/all.min.js"></script>

You can put the <script> tag below the other one at the bottom of the file but I doubt that you'll notice any speed difference.

From a now deleted comment:

The JS is just one option (the preferred option), but CSS only is still an option as well. Also, you don't use both. It's either CSS or JS

In Blazor I could only get the JS version to work. CSS only didn't work (the file was 200-OK).

10
votes

The fa prefix has been deprecated in version 5. The new default is the fas solid style and the fab style for brands. ref

add to _hosts.cshtml (for server side)

  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css">

Use fas as below:

@page "/counter"

<h1>Counter</h1>

<span class="fas fa-save"></span>  <!--fas not fa-->

@code {}

This is tested in blazor Net5

7
votes

You can use libman (or copy the files manually from the zip available at Fontawesome website). Then install/copy only all.min.css and the whole contents of webfonts folder into wwwroot/css/font-awesome subfolder. Like this:

enter image description here

Then put this into Pages/_Host.cshtml (for Blazor Server) or wwwroot/index.html (Blazor Web Assembly) into the head section:

<link rel="stylesheet" href="css/font-awesome/css/all.min.css" />

Or, as an alternative, add this at the beginning of site.css:

@import url('font-awesome/css/all.min.css');

No need for JS. It works.

1
votes

You have to actually reference the stylesheet in your HTML page. This is usually done in the layout (_Layout.csthml). You need to add something like the following in your <head>:

<link rel="stylesheet" href="/css/font-awesome/font-awesome.min.css" />