Inserting icon fonts to your layouts adds fun visual queues for your users and are especially successful as menu links or as replacements for larger graphics. Like standalone SVG files, they can be scaled losslessly and manipulated with CSS for easier customization. Icon fonts are normally displayed by including an icon font stylesheet, then inserting an HTML element where the icon should appear, such as <span class="icon123"> or <i class="xyz"> , but using the HTML method is not always possible. This quick tip will show you how to add icon fonts to any element in your site safely with just CSS.

Install Your Icon Font

Before you jump into adding icons, you’ll need to choose an icon font library to use, such as Font Awesome or Themify Icons.

There are a couple ways to go about adding icon font libraries to your website:

Including a remote library (popular)

Downloading and hosting it yourself (best performance)

Themify WordPress themes already come with both FontAwesome and Themify Fonts installed. Learn more about Font Awesome install options or learn more about Themify Icon install options.

Exercise – Adding Icons to Menu Links

In the following demo, I’ve included the Font Awesome library and an example menu styled three different ways to show how you can add icons before (or after) the link, or replace the link with an icon.

View Full Screen Demo

Download Demo ZIP

Step 1 – Get Your Element’s Selector

The CSS3 pseudo-element ::before will place the icon before the link text.

In this example, the ID selector for the menu’s home link is #menu-link-1 which you can see by right-clicking the link and choosing Inspect in the browser menu.

The combined selector looks like this:

#menu-item-1 a::before{ }

You can also use the ::after pseudo-element to place an icon after the link text.

Here is a look at the menu HTML and CSS together:

... <ul> <li id="menu-link-1"><a href="#">Home</a></li> <li id="menu-link-2"><a href="#">About</a></li> <li id="menu-link-3"><a href="#">Contact</a></li> </ul> </section> <style> /* icon-added icon styles */ #icon-added a::before{ padding-right: 10px; font-family: "FontAwesome" }

#menu-link-1 a::before{ content: "\f015"; } #menu-link-2 a::before{ content: "\f2b9"; } #menu-link-3 a::before{ content: "\f0e0"; } </style>

Step 2 - Drop In The Icon

There are only two critical pieces you need to get an icon font to display: the icon's unicode number, and the font-family , which is always "FontAwesome" for Font Awesome, or "themify" for Themify Icons.

font-family

Since this menu has an ID of "icon-added" we can use that ID to set a font family once for all links inside it that have a ::before pseudo-element.

#icon-added a::before{ padding-right: 10px; font-family: "FontAwesome"; }

content

The icon is rendered in CSS by setting the content property to the icon's Unicode or ISO number. This number is listed at the top of each icon's detail page on the Font Awesome website:

You can also find this number by right-clicking an icon, such as those on the Themify Icons page, expanding the HTML to find the ::before element, then looking at the CSS style:

#menu-link-1 a::before{ content: "\f015"; }

This will place the house before the Home text in the menu link.

That's all there is to it!

Replacing Links With Icons

If you want to hide the text inside the link so only the icon appears, you can use this little trick. This technique can be applied to many different HTML elements and their content where you want to use an icon font, such as replacing images, sprites or spans.

First, we restyle the menu's default links:

#icon-replace a{ color: transparent; overflow: hidden; max-height: 40px; width: 16px; }

The transparent value for color is a safety measure in case the link text peeks out on a specific display.

value for is a safety measure in case the link text peeks out on a specific display. The overflow and width resize the link element so only the icon is visible, and max-height ensures the link element doesn't expand if the link text wraps(set here to the same value as the menu's original height ).

The font family and icon unicode are declared the same as in the last example:

#icon-replace a::before{ font-family: "FontAwesome"; } #menu-link-4 a::before{ content: "\f015"; }

Extra Tip: Icon Hover States

To apply a specific hover color to the icon, you can combine the :hover pseudo-selector with the ::before pseudo-element like this:

#icon-replace a:hover::before{ color: #0CF; }

To learn more about styling icons, or implementing them through custom HTML, see FontAwesome Examples