How to add an icon to an HTML element using the ::before pseudo class in CSS

In this article, I'll cover all the different ways of doing this

By: Ajdin Imsirovic 27 March 2023

Adding an icon using the before CSS class

To add an icon using the CSS ::before pseudo-class, you can follow these steps:

1. Find the icon you want to use, and save it as an image file

This image file can be in a format like PNG or SVG.

2. Create a CSS class for the element that you want to add the icon to

For example, if you want to add an icon to a link, you could use the following CSS:

a.icon:before {
  /* CSS rules for the ::before pseudo-class go here */
}

This creates a class named icon that you can use to add the icon to any link element.

3. Add the content property to the CSS rules for the ::before pseudo-class

This property sets the content that should be displayed before the element. For example, if you have a PNG image file named icon.png, you could use the following CSS:

a.icon:before {
  content: url('icon.png');
}

This sets the content of the ::before pseudo-class to the image file, which will be displayed before the link.

4. Add any additional CSS rules to style the icon as desired

You can, for example, add width and height to set the size, or margin to position the icon.

Here’s an example that puts it all together:

a.icon:before {
  content: url('icon.png');
  width: 20px;
  height: 20px;
  margin-right: 5px;
}

This will add a 20x20 pixel icon to the left of any link with the icon class, with 5 pixels of margin between the icon and the link text.

5. Alternative approaches

There are different approaches you can use to add an icon using CSS, depending on the specific requirements of your project. Here are a few other approaches you could try:

5.1 Use an icon font

Icon fonts are fonts that contain vector icons as glyphs, which can be styled using CSS like any other text. You can use CSS to set the font-family property to the name of the icon font, and then use pseudo-elements like ::before or ::after to display the desired icon. There are many icon font libraries available, such as Font Awesome or Material Icons.

Here’s an example using Font Awesome:

a.icon:before {
  font-family: 'Font Awesome 5 Free';
  content: '\f007';
  margin-right: 5px;
}

This sets the content of the ::before pseudo-element to the icon glyph for the Font Awesome “fa-user” icon, and adds 5 pixels of margin to the right of the icon.

5.2 Use SVG sprites

SVG sprites are a collection of SVG icons that are combined into a single file, which can be loaded and displayed using CSS. You can use CSS to set the background-image property to the URL of the SVG sprite file, and then use pseudo-elements to display the desired icon by specifying the position of the icon within the sprite.

Here’s an example:

a.icon:before {
  content: '';
  display: inline-block;
  width: 16px;
  height: 16px;
  margin-right: 5px;
  background-image: url('icons.svg');
  background-position: 0 0;
}

This sets the content of the ::before pseudo-element to an empty string, and sets the display, width, height, margin, and background-image properties. The background-position property specifies the position of the icon within the SVG sprite file.

5.3 Use CSS shapes

Use CSS shapes - CSS shapes allow you to create non-rectangular shapes using CSS, which can be used to display icons or other graphics. You can use CSS to create a shape using the ::before or ::after pseudo-elements, and then style the shape using CSS properties like border-radius, border, or background-color.

Here’s an example using CSS shapes:

a.icon:before {
  content: '';
  display: inline-block;
  width: 16px;
  height: 16px;
  margin-right: 5px;
  border-radius: 50%;
  background-color: #333;
}

This sets the content of the ::before pseudo-element to an empty string, and sets the display, width, height, margin, border-radius, and background-color properties to create a circular shape. You can use different CSS properties to create other shapes as desired.

Feel free to check out my work here: