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.
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.
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.
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.
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:
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.
The transparent value for color 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: