Unicode For A Multi-Device World

Advertisement

A while ago, I was working on a website that required a number of icons. “No problem,” I thought. “I know how to handle this. I’ll use an @font-face icon set for high-resolution screens. It’ll be a single file, to reduce HTTP requests, and I’ll include just the icons that I need, to reduce file size.”

“I’ll even use a Unicode character as the base of the icon, so that if @font-face isn’t supported, then the user will still see something like the intended icon.” I felt pretty pleased with myself.

That is, until I ran the page in the device lab.

On some devices, a number of the icons weren’t showing. Yet on the same devices, others were, so clearly it wasn’t an @font-face issue. It must have been the underlying Unicode.

What To Do?

  • I could have used ligatures for the @font-face icon. These are great for accessibility but aren’t supported in Internet Explorer 9 or earlier.
  • I could have used the default “blank” Unicode characters (the PUA ranges1), which apps such as IcoMoon default to. But, again, Internet Explorer has issues2, and if the user has set a default font (if they are dyslexic, for example), then they wouldn’t see anything meaningful.
  • There are some good arguments for using SVG3, but I like the flexibility of @font-face: Changing the color on hover is very easy, for example, and @font-face icons will render with subpixel anti-aliasing, whereas SVG uses grayscale.

I was happy with the underlying Unicode characters because they gave a nice fallback for users on Windows Phone 7, Opera Mini and old BlackBerrys that don’t support @font-face. Instead, I wanted to determine which Unicode characters I could safely use.

So, I started doing some testing. And that led to some more testing, and then some more.

So far, I’ve looked at 107 Unicode characters on 78 devices and 5 screen readers. I’ll be honest — it’s not the most fun I’ve ever had. So, I’ll save you the trouble and share some of the problems and potential solutions that I’ve come across.

Why Bother?

But before we get to that, let’s just make sure that this is all worthwhile.

Why you should bother about unicode.4
Left to right: @font-face icon using a ligature in Chrome; @font-face icon using a ligature in Internet Explorer 9 (yep, nothing there); @font-face icon using a Unicode fallback in Opera Mini; @font-face icon using a PUA code point in Opera Mini (yep, nothing there). (Large version5)

SVG and @fontface subpixel rendering.6
Left: SVG with grayscale anti-aliasing. Right: @font-face icon with subpixel rendering. (Both in Firefox). (Large version7)

(Oh, and if I hear murmurings of “Opera Mini? No one uses that,” then consider that there are 70 million more Opera Mini users than iPads8 in the world.)

To my mind, using the underlying Unicode character seems the most robust approach, offering a visual icon to the greatest number of devices and browsers.

How Do You Do It, Then?

If you’ve not used Unicode characters with @font-face icons, don’t worry. Tools like IcoMoon9 make it simple. When you’ve selected the icons that you want to use and clicked to generate the font, you’ll see a page that shows all of your chosen icons, with their name and the Unicode code point that they will be mapped to. This will usually be something like e600, and this number is what you’ll need to replace with the hexadecimal value of your chosen Unicode character.

Tools like IcoMoon make it simple to generate icon fonts.10
Tools like IcoMoon make it simple to generate icon fonts. (Large version11)

So, how do you know which Unicode character to use? You could go through one of the many resources that list all 110,000 characters, but you’ve probably got better things to do. Shapecatcher4312 makes life a bit easier: Draw the shape you’re after, and it will return a list of similar-looking characters.

Shapecatcher will return a selection of similar looking icons for you.13
Shapecatcher will return a selection of similar looking icons for you. (Large version14)

Pick the one you like best, copy the Unicode hexadecimal value, and paste it into IcoMoon. Once you’ve done this for all of your icons, download the @font-face package as normal.

So, Which Characters Should You Use?

Well, I’ll have to defer to Jeremy Keith’s answer for everything about the Web: “It depends15.” In my testing, support for any given Unicode character varies hugely (and by “support,” I mean that either the @font-face icon appears or that the underlying Unicode character appears, rather than the user seeing an empty rectangle or a question mark or nothing at all). Even Unicode characters in the same “block16” of related symbols often have quite different levels of support.

But I can provide a little help. Once you know which Unicode character you want to use, run it through Unify17, which will give you a support score based on the devices that I’ve tested with so far.

If you’re happy with either the @font-face icon or the Unicode character being displayed, you should see very high levels of support. Some icons work on everything that I’ve tested so far.

The big exception is Emoji characters18, which I would stay away from. Around half of the devices that I’ve tested on don’t show the @font-face icon, and many don’t show anything meaningful at all. Additionally, iOS replaces them with fixed-size colored graphics, so trying to scale them with font-size won’t work.

Variation in Emoji characters.19
Variation in Emoji characters. Left to right: Firefox (Windows 7), Chrome (Windows 7), Chrome (Android 4.2.1), Opera (Windows 7) and Safari (iOS). (Large version20)

Also, if a device can render a Unicode character, it will usually also be able to render an @font-face icon that uses that character. So, you’ll need to decide on the tradeoff: Using an icon font gives you more control over the design, but then you’d be adding to the page’s weight and delaying the rendering of the icon until the font has loaded.

If you’re just using a Unicode character directly, then be prepared for a variety of visual styles. Even on the same computer, browsers can render the same character in quite different ways.

Unicode character 260E shown in Firefox, Chrome, Internet Explorer and Opera on the same Windows 7 machine, and on iOS.21
Unicode character 260E shown in Firefox, Chrome, Internet Explorer and Opera on the same Windows 7 machine, and on iOS. (Large version22)

In many projects, the consistency and control you get from using an @font-face icon is worthwhile. Putting only the icons that you need in the font file will minimize downloading time.

Saying anything more about @font-face icons is pointless because Zach Leatherman has written such an excellent and well-researched article23.

Accessibility

Zach looks in depth at the accessibility implications of using icons in this way, and I’d like to add a few thoughts.

There are two main problems here: The screen reader will either completely ignore the icon (which would leave the user not knowing that there is something there to interact with) or announce something seemingly irrelevant. For example, if you used Unicode character 2261 for your “burger” icon, it would either be ignored or be announced as “Equal” or “Identical to”, depending on the screen reader. Neither of these would help the user understand what this element does.

The best approach depends on what the icon is for. If it merely adds visual flourish to a text label or heading, then I’d recommend hiding the icon from screen readers. In this case, rather than adding icon fonts directly to the element in the standard way (i.e. by using the :before or :after CSS pseudo-element), add it to a span inside the element. This way, you can add aria-hidden="true" to the span, which will prevent it from being announced in a reasonable number of screen readers24.

<span aria-hidden="true">≡</span> Menu

However, if the icon will be used on its own, without anything else to provide meaning or context, then I’d recommend hiding the icon as before but adding a text label that is hidden visually. This will provide a cue that there is something to interact with.

<style>
    .visiblyHidden {
    /* http://css-tricks.com/places-its-tempting-to-use-display-none-but-dont/ */
        position: absolute;
        overflow: hidden;
        clip: rect(0 0 0 0);
        height: 1px; width: 1px;
        margin: -1px; padding: 0; border: 0;
    }
</style>

<span aria-hidden="true"> ≡ </span><span class="visiblyHidden">menu</span>

Does font-family Help?

I’ve heard the recommendation that you should include fonts like Arial Unicode MS in a font-family stack that is being used to display Unicode symbols, but that doesn’t guarantee that the symbol will appear for everyone. If a browser can’t find a font with the required Unicode character from those you’ve listed in the font-family declaration, then it will try to find any font in the OS that will display it.

Consistency is hard to come by, though, because it will depend on the fonts available on that particular device and on the browser being used. Browser manufacturers are free to look for any matching fonts however they think best — and for the sake of performance, the browser might stop searching if it’s taking too long.

By all means, include Arial Unicode MS or Segoe UI Symbol in a font-family declaration if you like how they render the symbols you’re using, but you’ll be relying on that font being installed on the device. If you want to control how the symbol looks, you’re best off with an @font-face icon.

Detecting Unicode Support

So, can JavaScript come to the rescue? Well, Modernizr does have a couple of tricks up its sleeve. One approach25 compares the width of a commonly supported character against one that’s likely to produce a fallback character and, if different, determines that the first character has rendered as expected.

Another focuses on trying to determine Emoji support26 by drawing the character onto a canvas element and then checking a single pixel to see whether it’s been filled in by the character. The problem, as we’ve seen, is that Unicode support isn’t a boolean; just because a browser displays one character doesn’t mean that it will display another. So, I wouldn’t rely on these methods generally, but they might be useful in some circumstances.

Encoding Your Unicode

Mentioning encoding here would probably be worthwhile if you are working with Unicode. Encoding tells a browser how to interpret any Unicode characters on a page. If the encoding used by your text editor differs from the settings of your end user and there are no instructions to help the browser make sense of it, then the user will see a lot of seemingly random characters or question marks.

An example of the variation in glyphs from a single Unicode code point with different page encodings.27
An example of the variation in glyphs from a single Unicode code point with different page encodings. (Large version28)

Fortunately, pretty much everything on the Web these days uses UTF-8, so this isn’t the headache it used to be. However, it doesn’t hurt to make sure, and this is why you’ll hear of the recommendation to include <meta charset="utf-8"> in the <head> of your documents. If you’ve specified the encoding in the HTML file, then you don’t need to specify it again in any linked CSS files — it will be assumed to be the same.

If you’ve done this and you’re still seeing strange characters, then check that your HTTP headers are set to UTF-8 as well. Any encoding set there will override the meta charset declaration.

However, if you do have to work with a different encoding type, then avoid “embedded” characters (i.e. just pasting the single character shape directly into the text editor). If you use the full numerical reference29, then the character will be unaffected by the document’s encoding type30.

In other words, if you want to use 2665 for a heart icon, then use &#x2665, not ♥. Embedding symbols in this way is the safest approach, but adding accents and so on in body copy is not recommended31 because it will make the code harder to read and maintain. In this instance, getting the encoding right is better.

Hand-Picking Fonts

One feature of @font-face that’s often overlooked is unicode-range32. This allows you to specify that a particular font is being used for just a few Unicode characters.

But why would you want to?

Perhaps you have found the perfect font for your body copy, but you would prefer one or two punctuation marks from a similar font, or perhaps you want to use old-style figures33 but your body font doesn’t include them. Using unicode-range lets you use that alternate font for just those characters that you want to swap out.

You may specify34 a single character, a comma-separated list or an entire range of characters to be rendered in the alternate font.

It has a slightly different syntax from other methods of working with Unicode. You’ll need to add U+ before the character’s hexadecimal number. So, if you wanted a different font for all of your numbers (which are represented in Unicode between 0030 and 0039), you’d use something like this:

@font-face {
   font-family: 'oldStyleFigures';
   src: url(fonts/old-style-figures.ttf) format("opentype");
   unicode-range: U+30-39;
}

Then you would add this:

p {
   font-family: oldStyleFigures, Arial, sans-serif;
}

This means that all paragraph text will use Arial, except for numbers, which will use the oldStyleFigures font.

The downside? Of course, there’s a downside. It still35 doesn’t work in Firefox — the alternate font will be applied to all characters, not just the ones you’ve specified in the range.

It still doesn’t work in Firefox36
Top to bottom: no unicode-range used; unicode-range added for numerals; unicode-range for numbers viewed in Firefox. (Large version37)

However, Drew McLellan has a workaround for the lack of support in Firefox38.

Unicode Syntax Cheat Sheet

How you add a Unicode character will determine the method that you need to refer to it. Here’s a quick summary:

Where Syntax Example
HTML (hexadecimal number) &#x????;
<span>&#x2665;</span>
HTML (decimal number) &#????;
<span>&#9829;</span>

(Note: no x as in the hexadecimal number)

CSS content (number must be in hexadecimal) \????
.heart:before { content: '\2665'; }
unicode-range (number must be in hexadecimal) U+????
@font-face {
font-family: heartFont;
src: url(fonts/heart-font.ttf) format("opentype");
unicode-range: U+2665;
}

It Doesn’t End There

That’s it for now, but plenty of useful articles and tools are out there. Here are a few that might help you out:

(ds, il, al, ml)

↑ Back to topShare on Twitter

John is a front end developer at Linney Design, and has been building web content for well over a decade. Creating rich, accessible sites is his focus, but any spare time left over after playing with his two daughters is dedicated to his very long running game development project. He's often heard repeating "Yes, but what happens when JavaScript is disabled?", and wondering whose turn it is to make the tea.

  1. 1

    I was a big proponent of @font-face for icons for a while, mainly because of their ease of use in changing sizes, colors using simple CSS. Unfortunately, I’ve found that Mac’s generally make the icon fonts “obese” (like Macs do with pretty much all webfonts, compared to the same webfont on a PC) and it’s gotten to the point where I’ve had to revert to use plain old image sprites.

    It’s been a very disappointing, but grunt with tinypng and spritegenerator tasks have at least made it somewhat less painful. I’d love to hear about any body else’s solutions to the “fat-icon-fonts-on-macs-issue”. Even -webkit-font-smoothing hasn’t helped me out.

    3
    • 2

      Icon fonts also have other issues rarely addressed in articles:
      - Sharpness issues on different sizes. You have to select a size where it looks smooth, and that size is not consistent across browsers and OS’s.
      - Line height and centering is problematic because of cross-OS and cross-browser inconsistencies
      - Different shapes of icons have different dimensions, so when you need the same icon height/width for all icons, you need to define specific font-size for all icons, which is, again, not consistent across browser/os. With png’s, you can just set the desired height for all icons, and the width will adapt if the aspect ratio is different.

      4
  2. 3

    Jonathan de Jong

    May 8, 2014 7:09 am

    Just a heads-up!
    There’s a sneaky little quotation mark at the end of the post URL on the image link (in the archive). Might wanna get that fixed :)

    0
  3. 5

    Mathias Bynens

    May 8, 2014 9:54 am

    IE does not have issues with @font-face and PUA symbols if you send the X-UA-Compatible: IE=Edge header with the HTML response, which you should do anyway.

    1
    • 6

      John Holt Ripley

      May 8, 2014 3:27 pm

      Thanks for confirming that. Yes, I would have thought that was pretty much a default.

      0
  4. 7

    Thanks so much for this John. I really appreciate people like you not only going through the pain of discovering solutions but taking an extra step to compile it to a well-written resource for others to benefit from. I wish I was as good as you.

    Cheers,
    -d.

    0
  5. 8

    Thanks a lot for the resource, will try these in next project.

    0
  6. 9

    Never had considered Unicode characters until I read this.

    Amazing post, incredibly helpful John.

    “…consider that there are 70 million more Opera Mini users than iPads in the world”, indeed all the resources talking about this seem very reliable.

    However, my analytics say that I have no visitors using Opera Mini, so no, I don’t plan to support that browser.

    Moral of that statement? Look at your analytics data and decide upon that.

    1
  7. 10

    Contribute some typefaces with the greatest Unicode compatibility I have ever known:
    Cambria Math
    Segoe UI Symbol
    Arial Unicode MS
    SimSun
    Among many others…

    0

Leave a Comment

Yay! You've decided to leave a comment. That's fantastic! Please keep in mind that comments are moderated and rel="nofollow" is in use. So, please do not use a spammy keyword or a domain as your name, or else it will be deleted. Let's have a personal and meaningful conversation instead. Thanks for dropping by!

↑ Back to top