CSS Pseudo-elements
When changing the HTML on your project is not an option, you can make use of a CSS feature to add icons to a page when using Font Awesome Web Fonts.
About Pseudo-elements
CSS has a powerful feature known as Pseudo-elements that lets you visually add content to the page with just CSS. Font Awesome has leveraged the ::before
pseudo-element to add icons to a page since Font Awesome was just a youngin.
We’ve already learned that Font Awesome uses classes like fa
and fa-user
to show icons in your site. Let’s duplicate the functionality of these classes and write our own using CSS pseudo-elements.
Add an Icon Using CSS Pseudo-elements
1. Define Common CSS for All Icons
First, you’ll need to add some common CSS properties that apply to all icons. It’s best to get this out of the way first to simplify your individual icon CSS. You can make it a separate rule or include the styling in every individual icon rule (in the next step).
2. Reference Individual Icons
There are a few important items to include when referencing any individual icon:
- Set the pseudo-element to match either the
::before
or::after
you used in the previous common set up step. - Set the
font
CSS property to the correct CSS custom property (see the Family and Styles Cheat Sheet below). But note that Duotones need special treatment. - Set the content to the Unicode value of one of our icons.
CSS Pseudo-elements and Duotone
Using CSS pseudo-elements to render duotone icons (both the Duotone family icons and Sharp Duotone family icons) follows a similar setup, but requires the use of both the ::before
and ::after
pseudo-elements along with more styling setup.
1. Define Common CSS for Duotone Icons
There are shared CSS properties unique to the Duotone and Sharp Duotone styles that all duotone icons will need. Again, it’s best to set up a shared CSS rule for all duotone icons first to simplify your individual icon definitions.
- Add styling to elements that will contain the pseudo-element to support positioning.
- Set the
font
to the CSS Custom Property, add the necessary font-style resets, and add positioning styles for the pseudo-element. - Set the default opacity levels and colors for each layer of the duotone icon.
If you’re using Sharp Duotone icons, you use the same CSS rules, except you change the font to font: var(--fa-font-sharp-duotone);
.
2. Reference Individual Icon’s Layers
Referencing individual duotone icons works much like all CSS pseudo-element icon use. Set the content to the unicode value of one of our icons. For Duotone icons, you will have two values — one for each duotone layer. For door-open
, you would have \f52b
for the primary layer and \f52b\f52b
for the secondary layer.
Font Families and Styles Cheat Sheet
Here’s a handy chart of the font styles/weights you can use in your pseudo element CSS rules to define the family and style of icon you want to use.
Style | Availability | @font-face font-family | @font-face weight | CSS Custom Property |
---|---|---|---|---|
Brands | Free Plan | Font Awesome 6 Free Font Awesome 6 Pro | 400 | --fa-font-brands |
Classic Solid | Free Plan | Font Awesome 6 Free Font Awesome 6 Pro | 900 | --fa-font-solid |
Classic Regular | Pro only | Font Awesome 6 Pro | 400 | --fa-font-regular |
Classic Light | Pro only | Font Awesome 6 Pro | 300 | --fa-font-light |
Classic Thin | Pro only | Font Awesome 6 Pro | 100 | --fa-font-thin |
Sharp Solid | Pro only | Font Awesome 6 Sharp | 900 | --fa-font-sharp-solid |
Sharp Regular | Pro only | Font Awesome 6 Sharp | 400 | --fa-font-sharp-regular |
Sharp Light | Pro only | Font Awesome 6 Sharp | 300 | --fa-font-sharp-light |
Sharp Thin | Pro only | Font Awesome 6 Sharp | 100 | --fa-font-sharp-thin |
Custom Icons | Pro Kits only | Font Awesome Kit | - | - |
Duotone icons are a special case which we’ve covered in the CSS Pseudo-elements and Duotone section.
Style | Availability | @font-face font-family | @font-face weight | CSS Custom Property |
---|---|---|---|---|
Duotone Solid | Pro only | Font Awesome 6 Pro | 900 | --fa-font-duotone |
Duotone Regular | Pro only | Font Awesome 6 Pro | 400 | --fa-font-duotone-regular |
Duotone Light | Pro only | Font Awesome 6 Pro | 300 | --fa-font-duotone-light |
Duotone Thin | Pro only | Font Awesome 6 Pro | 100 | --fa-font-duotone-thin |
Sharp Duotone Solid | Pro only | Font Awesome 6 Sharp Duotone | 900 | --fa-font-sharp-duotone-solid |
Sharp Duotone Regular | Pro only | Font Awesome 6 Sharp Duotone | 400 | --fa-font-sharp-duotone-regular |
Sharp Duotone Light | Pro only | Font Awesome 6 Sharp Duotone | 300 | --fa-font-sharp-duotone-light |
Sharp Duotone Thin | Pro only | Font Awesome 6 Sharp Duotone | 100 | --fa-font-sharp-duotone-thin |
Custom Duotone Icons | Pro Kits only | Font Awesome Kit Duotone | - | - |
Pseudo-elements and Sass (SCSS)
Using our @include fa-icon-
mixin utilities, you can add icons with custom pseudo-element CSS easily. These mixins handle the basic rendering that we bundle in our toolkit to make sure icons display perfectly. Along with that, they define the correct icon style to render an icon.
If you’d rather define these style rules manually, that’s an option as well.
Adding a Family Mixin for all but the Classic Family icons
If you’re using our mixins to render an icon in our Duotone family of styles, Sharp family of styles, Sharp Duotone family of styles, you’ll need to reference the extra family mixin for the family you want along with the @include fa-icon-
mixin as shown in the above examples.
Pseudo-elements and Less
Using our .fa-icon-
mixin utilities, you can add icons with custom pseudo-element CSS easily. These mixins handle the basic rendering that we bundle in our toolkit to make sure icons display perfectly. Along with that, they define the correct icon style to render an icon.
If you’d rather define these style rules manually, that’s an option as well.
Adding a Family Mixin for all but the Classic Family icons
If you’re using our mixins to render an icon in our Duotone family of styles, Sharp family of styles, Sharp Duotone family of styles, you’ll need to reference the extra family mixin for the family you want along with the .fa-icon-
mixin as shown in the above examples.
CSS Pseudo-elements with Our SVG+JS Framework
If you’re using our SVG + JS framework to render icons, you need to do a few extra things:
Enable Pseudo-elements
Using CSS Pseudo-elements to render icons is disabled by default when using our SVG + JS Framework. You’ll need to add the <script data-search-pseudo-elements ... >
attribute to the <script>
element that calls Font Awesome.
Set Pseudo-elements to display to none
Since our JS will find each icon reference (using your pseudo-element styling) and insert an icon into your page’s DOM automatically, we’ll need to hide the real CSS-created pseudo-element that’s rendered.
Support for Dynamic Changes
Let’s get a little cute with it by using some JavaScript and jQuery to toggle the class of an element.