Skip to content

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).

/* Step 1: Common Properties
These styles are required to make icons render reliably */
.icon::before {
display: inline-block;
text-rendering: auto;
-webkit-font-smoothing: antialiased;
}

2. Reference Individual Icons

There are a few important items to include when referencing any individual icon:

/* Step 1: Common Properties: All required to make icons render reliably - we did this above but it's included here for the full demo */
.icon::before {
display: inline-block;
text-rendering: auto;
-webkit-font-smoothing: antialiased;
}
/* Step 2: Reference Individual Icons */
/* Note: Make sure to include the correct weight and Unicode value for the icon */
/* an example rule targeting any element with the "account" class to render fa-user icon in the Solid style */
.account::before {
font: var(--fa-font-solid);
content: '\f007';
}
/* an example rule targeting any element with the "warning" class to render fa-triangle-exclamation icon in the Regular style */
.warning::before {
font: var(--fa-font-regular);
content: '\f071';
}
/* an example rule targeting any element with the "updates" class to render fa-newspaper icon in the Light style */
.updates::before {
font: var(--fa-font-light);
content: '\f1ea';
}
/* an example rule targeting any element with the "contact" class to render fa-message icon in the Thin style */
.updates::before {
font: var(--fa-font-thin);
content: '\f27a';
}
/* an example rule targeting any element with the "delete" class to render fa-trash icon in the Sharp Solid style */
.delete::before {
font: var(--fa-font-sharp-solid);
content: '\f1f8';
}
/* an example rule targeting any element with the "add" class to render fa-circle-plus icon in the Sharp Regular style */
.add::before {
font: var(--fa-font-sharp-regular);
content: '\f055';
}
/* an example rule targeting any element with the "gaming" class to render fa-discord Brand icon */
.gaming::before {
font: var(--fa-font-brands);
content: '\f392';
}
/* an example rule targeting any element with the "custom-icon" class to render a custom icon (uploaded to a kit) */
.custom-icon::before {
font-family: 'Font Awesome Kit'; /* note that you need to use "font-family" for custom icons */
content: '\[CUSTOM*ICON_UNICODE]'; /* replace with custom icon's unicode value \*/
}

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.

  1. Add styling to elements that will contain the pseudo-element to support positioning.
  2. Set the font to the CSS Custom Property, add the necessary font-style resets, and add positioning styles for the pseudo-element.
  3. Set the default opacity levels and colors for each layer of the duotone icon.
/* Step 1: Common Duotone positioning properties: All required to make icons render reliably */
.icon-duotone {
position: relative;
padding-left: 1.25em; /* make space for the width of the absolutely positioned icon */
}
/* Step 2: Set the font styles for Duotone */
.icon-duotone::before,
.icon-duotone::after {
font: var(--fa-font-duotone);
/* include the basic Font Awesome font style settings if you haven't already */
display: inline-block;
text-rendering: auto;
-webkit-font-smoothing: antialiased;
/* position both layers of the icon to the left, set our fixed-width width, horizontally center layers, and then vertically align them so they flex with different line heights */
position: absolute;
left: 0;
top: 50%;
transform: translateY(-50%);
width: 1.25em;
text-align: center;
}
.custom-icon-duotone::before,
.custom-icon-duotone::after {
font-family: 'Font Awesome Kit Duotone'; /* note that you need to use "font-family" for custom icons */
}
/* Step 3: Set the default opacity levels and colors for each layer */
.icon-duotone::before {
color: var(--fa-primary-color, inherit);
opacity: 1;
opacity: var(--fa-primary-opacity, 1);
}
.icon-duotone::after {
color: var(--fa-secondary-color, inherit);
opacity: var(--fa-secondary-opacity, 0.4);
}

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.

/* Step 4: Reference an individual icon's layers using unicodes */
/* This rule renders the primary duotone for the door-open icon */
.icon-login::before {
content: '\f52b';
}
/* This rule renders the secondary duotone for the door-open icon */
.icon-login::after {
content: '\f52b\f52b';
}
.custom-icon-duotone::before {
content: '\[CUSTOM*ICON_UNICODE]'; /* replace with custom icon's unicode value \*/
}
.custom-icon-duotone::after {
content: '\[CUSTOM*ICON_UNICODE]\[CUSTOM_ICON_UNICODE]'; /* replace with custom icon's unicode value, twice \*/
}

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.

StyleAvailability@font-face font-family@font-face weightCSS Custom Property
BrandsFree PlanFont Awesome 6 Free or
Font Awesome 6 Pro
400--fa-font-brands
Classic SolidFree PlanFont Awesome 6 Free or
Font Awesome 6 Pro
900--fa-font-solid
Classic RegularPro onlyFont Awesome 6 Pro400--fa-font-regular
Classic LightPro onlyFont Awesome 6 Pro300--fa-font-light
Classic ThinPro onlyFont Awesome 6 Pro100--fa-font-thin
Sharp SolidPro onlyFont Awesome 6 Sharp900--fa-font-sharp-solid
Sharp RegularPro onlyFont Awesome 6 Sharp400--fa-font-sharp-regular
Sharp LightPro onlyFont Awesome 6 Sharp300--fa-font-sharp-light
Sharp ThinPro onlyFont Awesome 6 Sharp100--fa-font-sharp-thin
Custom IconsPro Kits onlyFont Awesome Kit--

Duotone icons are a special case which we’ve covered in the CSS Pseudo-elements and Duotone section.

StyleAvailability@font-face font-family@font-face weightCSS Custom Property
Duotone SolidPro onlyFont Awesome 6 Pro900--fa-font-duotone
Duotone RegularPro onlyFont Awesome 6 Pro400--fa-font-duotone-regular
Duotone LightPro onlyFont Awesome 6 Pro300--fa-font-duotone-light
Duotone ThinPro onlyFont Awesome 6 Pro100--fa-font-duotone-thin
Sharp Duotone SolidPro onlyFont Awesome 6 Sharp Duotone900--fa-font-sharp-duotone-solid
Sharp Duotone RegularPro onlyFont Awesome 6 Sharp Duotone400--fa-font-sharp-duotone-regular
Sharp Duotone LightPro onlyFont Awesome 6 Sharp Duotone300--fa-font-sharp-duotone-light
Sharp Duotone ThinPro onlyFont Awesome 6 Sharp Duotone100--fa-font-sharp-duotone-thin
Custom Duotone IconsPro Kits onlyFont 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.

// Solid style of user
.user {
@include fa-icon-solid($fa-var-user);
}
// Regular style of triangle-exclamation
.triangle-exclamation {
@include fa-icon-regular($fa-var-triangle-exclamation);
}
// Light style of newspaper
.newspaper {
@include fa-icon-light($fa-var-newspaper);
}
// Thin style of paintbrush-fine
.paintbrush-fine {
@include fa-icon-thin($fa-var-paintbrush-fine);
}
// Duotone style of camera-retro
.camera-retro {
@include fa-icon-solid($fa-var-camera-retro);
@include fa-family-duotone(); // define Duotone family
}
// Sharp Solid style of trash
.trash {
@include fa-icon-solid($fa-var-trash);
@include fa-family-sharp(); // define Sharp family
}
// Sharp Regular style of trash
.trash {
@include fa-icon-regular($fa-var-trash);
@include fa-family-sharp(); // define Sharp family
}
// Sharp Duotone Solid style of trash
.trash {
@include fa-icon-solid($fa-var-trash);
@include fa-family-sharp-duotone(); // define Sharp Duotone family
}
// explicitly set Classic Solid style of trash
.trash {
@include fa-icon-solid($fa-var-trash);
@include fa-family-classic(); // explicity setting Classic family (optional since this is Font Awesome's default)
}
// Threads brand icon
.threads {
@include fa-icon-brands($fa-var-threads);
}

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.

// Solid style of user
.user {
.fa-icon-solid(@fa-var-user);
}
// Regular style of triangle-exclamation
.triangle-exclamation {
.fa-icon-regular(@fa-var-triangle-exclamation);
}
// Light style of newspaper
.newspaper {
.fa-icon-light(@fa-var-newspaper);
}
// Thin style of paintbrush-fine
.paintbrush-fine {
.fa-icon-thin(@fa-var-paintbrush-fine);
}
// Duotone style of camera-retro
.camera-retro {
.fa-icon-solid(@fa-var-camera-retro);
.fa-family-duotone(); // define Duotone family
}
// Sharp Solid style of trash
.trash {
.fa-icon-solid(@fa-var-trash);
.fa-family-sharp(); // define Sharp family
}
// Sharp Regular style of trash
.trash {
.fa-icon-regular(@fa-var-trash);
.fa-family-sharp(); // define Sharp family
}
// Sharp Duotone style of camera-retro
.camera-retro {
.fa-icon-solid(@fa-var-camera-retro);
.fa-family-sharp-duotone(); // define Sharp Duotone family
}
// Classic Solid style of trash
.trash {
.fa-icon-solid(@fa-var-trash);
.fa-family-classic(); // explicity setting Classic family (optional since this is Font Awesome's default)
}
// Threads brand icon
.threads {
.fa-icon-brands(@fa-var-threads);
}

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.

<html>
<head>
<script data-search-pseudo-elements defer src="/your-path-to-fontawesome/js/all.js"></script>
<style>
.icon::before {
display: none;
}
.login::before {
font: var(--fa-font-solid);
content: '\f007';
}
.tps::before {
font: var(--fa-font-solid);
content: '\f1ea';
}
</style>
</head>
<body>
<ul style="margin: 0;">
<li><span class="icon login"></span> Login</li>
<li><span class="icon tps"></span> TPS Reports</li>
</ul>
</body>
</html>

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.

<script>
setInterval(function () {
$('.ninety-four').toggleClass('arrow')
}, 1000)
</script>
<style>
.ninety-four::after {
margin-left: 0.25em;
font-size: 2em;
vertical-align: middle;
font-family: 'Font Awesome 6 Free';
font-weight: 900;
content: '\f0c8';
}
.ninety-four.arrow::after {
content: '\f152';
}
</style>
<a class="ninety-four" href="https://en.wikipedia.org/wiki/Blink_element"
>Hey, do you remember the blink tag?</a
>