Font Awesome is an icon font widely used across various websites, applications, and projects. I’ve been utilizing it for years to develop editor tools for my Unity projects. It’s truly awesome.
One of the first decisions I made as I started building this site with Hugo, a popular open-source static site generator, was to integrate Font Awesome to handle my icon needs. I specifically wanted to use SVGs due to their numerous advantages over traditional font icons. Many articles detail the advantages of SVG icons over font icons, so I won’t repeat those here.
The Font Awesome documentation offers various setup methods, from kits to self-hosted options, using web fonts or SVGs with JavaScript. However, I prefer embedding SVGs directly to avoid extra network dependencies and processing delays, as highlighted in a 2018 article by Nick Glabreath. This method results in slightly larger page loads but more efficient performance.
A more recent article by Micah R Ledbetter builds on Galbreath’s work with additional enhancements, which I will further develop.
Quick and Easy
Download Font Awesome
I have an active subscription to Font Awesome Pro, so I downloaded the v5.15.4 release from their website. You can download the latest release or the free icon set from the GitHub repository. My version includes 7,864 pro icons and 1,608 free icons.
Copy the SVGs
Some articles suggest placing SVG (scalable vector graphics) files inside your theme folder. However, Hugo’s layer-like site generation workflow allows customization without altering the theme code or folder structure. Ideally, nothing should be changed inside the theme folder, so we don’t want to place the Font Awesome SVGs there.
Instead, store the SVGs under assets
in the the project root. These files are treated like other assets and only used when the site is generated. The hierarchy looks like this:
If you’re using the free version of Font Awesome, you may only have a few of those folders, such as brands
and solid
.
Add the partials
Processing the SVG
Create a partial file:
<root>/layouts/partials/fontawesome.html
|
|
Note that this partial differs from Ledbetter’s in a few ways.
This partial was placed outside the theme, so the theme folder path is no longer needed in the
$fname
variable.The icon style and name are passed to the partial separately and accessed using
.style
and.icon
, respectively.Optional size and color (with opacity) settings may now be passed through the shortcodes, and the input is validated with a call to the
getFontAwesomeSettings
partial, which returns values that are accessed with$settings
.The size is applied directly to
<svg>
using inline styles, which seems to improve layout and sizing with adjacent text.There’s now special handling of duotone icons, which have primary and secondary colors and opacities, and those need to be applied to the appropriate
<path>
in the SVG. The<defs><style>
combo is also removed from the SVG, as its no longer needed and results in a smaller image file size.There’s a check to see if the file exists before rendering it. If it doesn’t exist, a build warning will be logged and the replacement glyph (�) will be embedded into the page instead. The
span
wrapping the replacement character also has a tooltip that includes the icon and style names being used, which may be useful for debugging the missing icon. Most importantly, this check allows Hugo to successfully rebuild the page, otherwise the build will fail whenreadFile
fails to load the missing SVG file.If you actually want the build to fail when a missing icon is detected, replace the
warnf
with anerrorf
call. That will still stop the build, but produce an error that includes the missing icon information.
Parsing Settings
Create a new partial file in the func
sub-directory, because this partial will return a value, and it helps to keep partials that return values separate from those that don’t.
<root>/layouts/partials/func/getFontAwesomeSettings.html
|
|
This may seem complex because both the size and color/opacity settings can be provided in any order. This flexibility simplifies shortcode usage. Additionally, the color data is a compound string containing up to two color and two opacity values, which are parsed and separated before being returned.
I’ll run through it quickly.
- A regex pattern is used to identify the size argument, and all length types defined in the CSS specification are included. I only tested with
em
,rem
, andpx
, but they should all work as expected. - A scratch is used to store potential results as the partial arguments are being processed. Default values are established and will be used unless the setting is overridden by an argument.
- Both arguments are examined, and if the size is identified, it’s used, otherwise color parsing is performed. Parsing calls the
getFontAwesomeColorOpacity
partial, which takes aslice
and returns astring
, and if parsing fails a default value will be applied. - The results are returned in a
dict
using named parameters.
Parsing Color and Opacity
Create another func
partial for separating color and opacity components.
<root>/layouts/partials/func/getFontAwesomeColorOpacity.html
|
|
This partial is really simple. It takes a slice
containing a string
and an index
. The string may contain both color and opacity data, so it’s split into color and opacity. The index
just tells the function which setting is requested, and if it exists, it’ll be returned, otherwise the function will return 0
as an failure condition.
That’s it! On to the shortcodes!
Add the shortcodes
For each shortcode you want to use, create a file at: <root>/layouts/shortcodes/
. I created a shortcode for each of the Font Awesome styles (brands, duotone, light, regular, solid). If you’re using the free version, you probably won’t need them all.
Start with the shortcode that will handle Font Awesome solid (fas
) icons:
<root>/layouts/shortcodes/fas.html
|
|
To handle the brand icons, just change the style
parameter to brands
in the next shortcode.
<root>/layouts/shortcodes/fab.html
|
|
If you’re using the Pro version of Font Awesome, be sure also create the fad
, fal
, and far
shortcode files with the duotone
, light
, and regular
styles, respectively.
Shortcode Usage
Syntax
{{< <style-shortcode> <icon> [color] [size] >}}
Parameters
Notation | Description |
---|---|
<style-shortcode> | One of the Font Awesome shortcodes created earlier: fab , fad , fal , far , or fas . |
<icon> | A Font Awesome icon code such as rocket-launch . |
[color] | A color pattern, which may include a single color value using standard CSS color notation: A color and an opacity: Two colors with optional opacity: To set opacity without affecting color, leave the color value blank or set it to To set secondary color without affecting the primary, leave the primary color value blank or set it to If the value includes any non-alphanumeric characters, wrap it in quotes. |
[size] | The size value using standard CSS length notation. If the value includes any non-alphanumeric characters, wrap it in quotes. |
Examples
To use in markdown content, just use one of the new shortcodes followed by the icon name.
fab
brandsfad
duotonefal
lightfar
regularfas
solid
You may specify a color after the icon name using standard CSS color notation, such as a hex, rgb, or hsl value or a color keyword.
{{< fas jack-o-lantern DarkOrange >}}
{{< fas jack-o-lantern "#ff8c00" >}}
{{< fas jack-o-lantern "rgb(255, 140, 0)" >}}
{{< fas jack-o-lantern "hsl(33, 100%, 50%)" >}}
The color opacity may be changed by appending a /
and a numeric or percentage value. Whitespace surrounding the /
is ignored.
Do not specify opacity directly in the color value, like you can when using a 4-byte hex value or rgba
or hsla
with alpha channels. Append it instead.
#6699FF80
(4 bytes)rgba(102 153 255 / 50%)
hsla(220 100% 70% / 50%)
#6699FF / 50%
rgb(102 153 255) / 50%
hsl(220 100% 70%) / 50%
{{< fas jack-o-lantern "DarkOrange/0.5" >}}
{{< fas jack-o-lantern "DarkOrange/50%" >}}
{{< fas jack-o-lantern "DarkOrange / 50%" >}}
{{< fas jack-o-lantern "rgb(255, 140, 0) / 50%" >}}
The size may also be specified using standard CSS length notation.
{{< fas jack-o-lantern 3rem >}}
The order of the optional color and size parameters doesn’t matter, as long as they follow the icon parameter.
{{< fas jack-o-lantern DarkOrange 3rem >}}
{{< fas jack-o-lantern 3rem DarkOrange >}}
Duotone icons have a slightly different color syntax, because they have both a primary and secondary color. Just separate the colors with a ;
. Whitespace surrounding the ;
is ignored.
By default the secondary color has an opacity of 40%, so you may want to override that as well.
{{< fad jack-o-lantern "yellow;DarkOrange/1" 3rem >}}
{{< fad jack-o-lantern "yellow ; DarkOrange / 100%" 3rem >}}
Have fun with icons!
Elevate your Font Awesome game!
Check out Using <use> with Font Awesome SVGs for advanced SVG optimization techniques, reducing load times, and boosting performance.