Do’s and Don’ts of Icon Fonts

Having worked on icon fonts a lot in this past year, I’ve learned many things about them I did not know before. In some ways they’re more useful than I thought (yes, icons can actually be pixel-perfect), in other ways less so (pretend Firefox doesn’t exist until it reaches version 25). Since icon fonts are booming these days, I thought it useful to share some do’s and don’ts that I’ve picked up over the course.

An icon font is a font that contains symbols and graphics instead of letters and numbers. Remember Wingdings? These symbol fonts are incredibly useful because they allow webdesigners to do things they can’t do with PNG or GIF graphics:

  • it’s HiDPI out of the box
  • they scale well (more on that later)
  • it’s easy to pick colors using only CSS

They’re so pleasant to work with that it’s easy to get carried away by the obvious benefits over PNGs. Unfortunately there are a number of limitations to be mindful of. Most of these can be mitigated by using icon fonts properly.

Scalability at small sizes

Just because icon fonts are technically vector graphics, doesn’t mean they all look great at any size shown. Especially at small sizes, icon fonts start falling apart. This is why good icon fonts are designed according to a pixel grid.

A pixel grid is a minimum resolution at which embedded icons show up crisp. Typically such a resolution is 16x16px (which translates to font-size: 16px) or 20x20px (which translates to font-size: 20px;). If you use the icons outside of their intended grid, the icons will get blurred and details become fuzzy. If you show an icon intended for 16px at 17px, it will look positively atrocious.

We’re used to this from the Nintendo 8-bit days: there’s only so much resolution to work with. Unless you place your pixels precisely, your graphics won’t be clear. Being vector graphics doesn’t change this one bit: at 16x16px it doesn’t matter how scalable your icon is.

Remember, even if you’re used to being able to use many different font sizes for actual fonts, icons are a different thing. Not only are our brains trained to recognize letters, but fonts themselves are designed to render letters for maximum legibility. Fonts rely on the fact that we read horisontally, and hints letters so that subpixel antialiasing ensures horisontal crispness at many arbitrary sizes. This subpixel antialasing does not translate to icons. Icon fonts won’t ever look as good as normal fonts do at arbitrary sizes.

Let there be no doubt in anyones mind: using icon fonts for web graphics is a hack!

It is a glorious, wonderful hack. But it is a hack. We have to be mindful how we use it.

Crossdomain fonts

Firefox blocks a stylesheet from example.com from loading a font hosted on cdn.example.com. It is an intended font piracy/hotlinking measure. The workaround is simple, but easy to forget when you’re pounding your head against the desk unsure why icons show up fine in Chrome but not Firefox.

The easiest workaround is to base64 encode the font file itself and embed it in the stylesheet. By being embedded directly in the stylesheet, no domain hops are necessary, and Firefox loads the font. Most icon font bundles (such as Genericons) come with base64 in tow, but if you’re making your own icon fonts, I recommend using FontSquirrel’s Webfont Generator which will optionally base64 encode for you.

Inserting icons

Because icon fonts are hacks, most icon font packs been through a rather lengthy process to make them as easy to use in webdesigns as possible. Ideally it’s a matter of referencing a stylesheet and copy/pasting a snippet of code.

But don’t get carried away. Behind the scenes numerous decisions have been made in order to make your icon fonts that simple to use. Some of those decisions have consequences outside of just the icons.

This is going to vary from icon font to icon font, but let me go through how the Genericons copy/paste tool works.

Glyphs

Right on the Genericons homepage, the third copy option next to the big icon preview is “Copy Glyph”. This is intended only for use in your Photoshop mockups. You download the icon font and install it on your system. Now in Photoshop you can paste the glyph in a textarea, set it to the Genericons font and the icon will show up.

It is not intended to paste in your actual HTML. Remember, icon fonts is a hack, and a screen-reader wouldn’t know what to do with a weird unicode symbol. Incidentally that means we can only rely on using CSS’s :before selector to insert the glyphs. That means using icon fonts in an accessible manner can never work on IE7.

Copy HTML

For casual users who want to insert Genericons as they would smileys, the “Copy HTML” feature is provided. In effort to make it as easy as possible to get the ball rolling and an icon showing, some helper CSS is provided with Genericons where every icon is mapped to a preset of named classes, for example:

<div class="genericon genericon-calendar"></div>

That’s a lot of CSS classes inside a redundant empty div, don’t you think? Yep. It’s not great code. But it’s really easy to insert, so the casual user doesn’t have to worry about the CSS. It’s perfect if you mean to insert a star icon in the middle of that article you’re writing in your CMS of choice.

Copy CSS

This is the option non-casual webdesigners should use. While also the least easy way to insert an icon (since you’ll effectively be re-writing some of the bundled helper CSS), it’s not actually as hard as I may be presenting it here. You should be using this method because:

  • it’s the most accessible — screen readers don’t even attempt to read the icons aloud because they’re inserted using only CSS
  • it’s the cleanest — there are no extra CSS classes and no redundant markup
  • you have the most control: since you write all the CSS, you don’t have to unstyle some of the bundled Genericons helper CSS you don’t need

The process is to essentially manually attach the icons to the :before selector of the class needed:

.my-element:before {
	content: '\f408';
	display: inline-block;
	-webkit-font-smoothing: antialiased;
	font: normal 16px/1 'Genericons';
	vertical-align: top;
}

The content: '\f408'; part is what you get from the “Copy CSS” tool. You can structure your own CSS as you wish to minimize code duplication.

In summary

Using icon fonts is a wonderful hack that brings many convenient features, but unless you’re using them right, you’ll end up with fuzzy graphics. Knowing is half the battle, and here are the rules:

Rule #1: When showing icons small, do not stray from the font pixel-grid (unless you have a good reason). If the grid is 16×16, show the font at font-size: 16px exactly.

Rule #2: Don’t rely too heavily on the helper CSS, that is intended only for casual use.

Now go use some icon fonts.

Twenty Fourteen, The Magazine Theme

With Twenty Thirteen well out the door, the next default WordPress theme is already being hammered on. Previously a WordPress.com premium theme, Further is being used as the base. Like Twenty Thirteen, it uses Genericons, so recoloring the theme should be a breeze. For the past couple of years, Takashi has blown me away with his theming skills, so I couldn’t be happier with the choice. Check out Twenty Fourteen as it looks today: it’s going to improve even more over the course of development!

Twenty Thirteen: Make It Yours

WordPress 3.6 was released today. With it, the Twenty Thirteen theme into which we’ve poured much energy. One aspect that was particularly important to me, when designing the theme, was to encourage users to customize the theme to their liking. For the very same reason, every icon in Twenty Thirteen is an easily recolorable icon-font glyph. So changing the whole color scheme is basically writing some CSS.

To celebrate the occasion, I made a couple of color themes you can install on your Twenty Thirteen blog.

2013 Green

twentythirteen-green

Green is my favorite color these days. Did you know when danish people have their final high school exams, they sit at a green table, because green has a calming effect on your mind? It’s true. I tried it.

Get 2013 Green (preview)

2013 Green Sequence

twentythirteen-green-sequential

Twenty Thirteen has emphasis on blogging, and post backgrounds are assigned based on post formats. If you don’t use post formats, but want the colors of Twenty Thirteen, get this one — it’ll simply alternate the background colors sequentially.

Get 2013 Green Sequence (preview)

2013 Blue

twentythirteen-blue

I actually threw in a shade of pink, or “salmon”, in this blue mix. So it’s not entirely shades of sky blue, but I guess you could not use the “status” post format if you dislike salmon. Or you could customize it and make it your own!

Get 2013 Blue (preview)

2013 Blue Sequence

twentythirteen-blue-sequential

If you really don’t like salmon but do want an alternating post-format-free blue theme, you’ll want to get this one and customize it. Or you could embrace the salmon. Smoked. On toast. With salt and pepper.

Get 2013 Blue Sequence (preview)

2013 Orange Sequence

twentythirteen-orange-sequence

Having made sequence versions of blue and green, it seems unfair not to complete the triad. So here’s Orange Sequence — that’s the default Twenty Thirteen color scheme, but the colorful post backgrounds are assigned regardless of post format.

Get 2013 Orange Sequence (preview)

How To?

These are child themes. That means they’re WordPress themes that require a parent theme to be present, in this case Twenty Thirteen. So these themes will only work if you also have Twenty Thirteen installed. If you’re on WordPress 3.6 or higher, Twenty Thirteen is preinstalled.

More Options

One of the best things about open source is that you can modify things to your liking, and even redistribute.

Please do so. Make it your own, and share it back.

I would like nothing more than to see you modify and redistribute any of the above child themes. So much, in fact, that if you’d like to re-color the default Twenty Thirteen header, I’ve made you this PSD so you can easily do just that.

Now go play with some colors!