Using Icon Fonts with the CSS content property and a simple Sass Mixin

Although I’m not the biggest fan of Icon Fonts and am having a closer look at svg at the moment, I used Icon Fonts a lot in the past. Mostly custom ones created by a Designer for me. At superReal we created a pretty nice Sass Mixin which helps using icons across your project in the same way and having an overview over all your icons in one place.

Every icon in an Icon Font is basically a character of the font. In our example writing a t with the Icon Font as the font-family will result in a twitter icon. But because icons are just graphics that would leave screen readers and search engines clueless because t doesn’t mean anything to them. So you should also provide a text version of every icon which can be indexed by search engines and used by screen readers. So to accomplish that I use pseudo elements with the content property to display the icons and hide the standard text with CSS.
The HTML looks like follows:

<a class="twitter-icon" href="http://twitter.com/_martinwolf">
    <span>Twitter</span>
</a>

With CSS we can than hide the span and only show the icon.
Our Mixin has three options. The first one is the $type and is the most important one and is mandatory because it defines which Icon is used from the if statement below. The second one is the pseudo element which by default is :before and the third is the display option which by default is block. We also define the font size (in the case of an icon font the size of the icon) and the line height in the Mixin for each icon individually within the if statement.
At last we hide the span element.

@mixin icon($type, $pseudo: before, $display: block) {
    &:#{$pseudo} {
        color: inherit;
        display: $display;
        font-family: 'Icons';
        -webkit-font-smoothing: antialiased;

        @if $type == burger {
            content: 'b';
            font-size: 70px;
            line-height: 37px;
        } @else if $type == facebook {
            content: 'f';
            font-size: 24px;
            line-height: 24px;
        } @else if $type == twitter {
            content: 't';
            font-size: 24px;
            line-height: 24px;
        } @else if $type == youtube {
            content: 'y';
            font-size: 24px;
            line-height: 24px;
        }
    }

    span {
        display: none;
    }
}

With this Mixin in place we can just call it and create our Twitter icon like that:

.twitter-icon {
    @include icon(twitter);
}

Be default the before pseudo element and display block is used. If we would want to change that we can do it like this:

.twitter-icon {
    @include icon(twitter, after, inline-block);
    margin-left: 10px;
}

This would result in the following CSS code:

.twitter-icon {
    margin-left: 10px;
}

.twitter-icon:after {
    color: inherit;
    display: inline-block;
    font-family: 'Icons';
    -webkit-font-smoothing: antialiased;
    content: 't';
    font-size: 24px;
    line-height: 24px;
}

.twitter-icon span {
    display: none;
}

If you want to use Entypo for example, a very popular Icon Font, you have to watch out for a little thing. Entypo and a lot of other Icon Fonts don’t give you a simple text for the content property, they instead give you a Unicode. But you can still use this Mixin you just have to escape the code it.

This is what you get from Entypo:

Screen-Shot-2014-01-04-at-14.19.42

And this is how you need to write your SCSS Mixin it to make it work:

content: '\1F50D';

I hope this helps you using Icon Fonts in your projects and makes your life a little bit easier.