HTML for Icon Font Usage
5 stars based on
By Chris Coyier On May 24, Where are we at right now in terms of the best markup for using icon fonts? Let's cover some options I think are currently the best. This ground has been treaded beforebut I think the following techniques are a small step forward. Let's say we have a header like "Stats" and we want option data-icon set it apart from other headers on the page and emphasize it's meaning.
So to get that icon in there remember we're talking font icons here, we can't just pad the left and option data-icon a background we'll need to insert some content. Using a pseudo element option data-icon tempting because 1 they aren't read by most screen readers 2 we don't need dedicated markup for the icon which is a semantic ideal. Pseudo elements just aren't in the DOM and thus that probably makes it harder for third-party apps to do. The option data-icon news is that if we use a bit of markup, we can use aria-hidden attribute to prevent it from option data-icon spoken.
We can still win here. If option data-icon combine the markup technique and pseudo element technique, we can insert the icon with no VoiceOver weirdness. And as a double-win freebie, this combined technique is ideal for keeping our CSS lean and mean as it requires no class-name-bloat and works well with the next use case we need to cover.
Holy cow that's easy eh? Notice we aren't using a specific class name for the icon e. In our icon font, we map those special characters to the icon we want to use. I find this perfectly semantic and even future proof you could always select uniquely down the line even if you change the character.
But if you prefer option data-icon names, more power to you, that's fine and doesn't change this technique drastically. Say we have an icon that is a link or in some option data-icon functional, but it isn't accompanied by any text. For instance, a shopping cart icon that you can click to go to your shopping cart.
Hopefully that functionality is option data-icon visually, with some kind of rollover state or general design obviousness. But that functionality also needs to be audibly obvious. Instead of crafting a totally new technique to deal with this which I've dabbled with in the past let's lean on what we've already started. A span that inserts the character with a pseudo element, and text that option data-icon right option data-icon to option data-icon that we kick off the page visually.
We need very little in additional CSS, just a little usability fix applied via class, and a toolbox class for hiding the text but leaving it accessible. Option data-icon made a more proper demo comparing icon font usage with different techniques. Would be great to hear from people who use other screen readers how this holds up. One "issue" in the world of option data-icon fonts right now is that the majority of them available come pre-mapped to letters. Used with improper markup, the letters become "content" and a part of the semantics of the document.
Or worse, read outloud by screen readers. It almost never makes sense for an icon to be mapped to a letter. Instead, I recommend mapping icons to the closest unicode symbol you can find. The meaning of symbols is rather relative anyway, so close counts and will be a semantic improvement option data-icon. As I understand it, this is exactly why it exists, to use for your own special characters.
Mapped this way, you're option data-icon no danger of the character being spoken by a screen reader. Unfortunately at the time of this writing icons mapped this way don't work in even the latest Safari, so it's not yet recommended.
Here's the scoop on the Private Use Area issues paraphrased from Keyamoon: Option data-icon difference is explained a bit here. In my testing, PUA mapping works perfectly in all browsers. You can option data-icon use Pictos Server to do the icon-picking and mapping, but it only works with Pictos and doesn't help you with choose symbols you could copy and paste from here. It is a very nice set though and hosts for you and makes it very easy to existing sets without manually swapping out font files.
In option data-icon future, hopefully we can option data-icon on speak: I love your semantics, but never understand why you bother so much about screen readers. Do you have any stats about option data-icon from screen readers? So much so that in the not-too-distant future, some folks may never own desktop computers and have their entire online experience through a mobile device….
Most require some sort of compliance, so even if they never get substantial traffic from screen readers they need accessibility standards. Screen readers are another way for option data-icon to interact with the content option data-icon our sites. Apart from the clear accessibility implications, we should be writing our code so that it caters for all people regardless of how they access it keyboard, option data-icon etc.
Funnily enough, the things you do to optimize for screen readers are, in most cases, also good practice in general. He bothers about screen readers because any website by a government or publicly funded institution such as a college or museum must have an accessible website.
Here in Canada, the government is requesting that private business also make their websites accessible. It will mostly likely soon be law. So, why build a site and have to do it option data-icon as accessible? Why not just design it that way to begin with?
Great topic, I always wanted to use font icons. You can do a lot of animations with them by only using CSS3. If anyone option data-icon of more please let me know. Service itself looks very familiar. Just for the record, I must say that the older version of my tool was available even before the pictos server. And it was the first to allow building custom option data-icon fonts. You can look at the changelog page in my site.
It would appear the Icomoon fonts are hinted against 16 and 32px sizes at least… See the option data-icon justification and page-type icons for the clearest example. It works fine on: But I just wanted to add that I certainly prefer being able to define the content in the style sheet rather than in the HTML. I must clear up some confusion about Safari not supporting PUA. See this Wikipedia page: PUA does not have any issues at all. Thanks Keyamoon, your font generator works fine on every browser and every option data-icon with the PUA encoding.
What a perfect timing! But the [data-icon] selector seems to work quite well 60ms increase. If you want to invite ie7 to the party you can put the character code right in the tag like: Great article, option data-icon have me some new things to think about on future sites. Thanks for your hard work. I work with a large app that uses dozens of icons; maintenance can be a real pain and implementation is harder for anyone not intimately familiar with the CSS. This solution would solve both of those problems — the stylesheet is terse, scalable and accessible.
Have you considered reaching out to a specialist in accessibility? A little over a year ago Refresh Baltimore had Reinhard Stebner give a talk on his experiences on the web as a blind individual.
His presentation was truly fascinating, and he offered his services to better option data-icon web. This is a summary of a fellow attendees thoughts — http: Why do screen readers read generated content in the first place? IMO, generated content should never be used for actual page content in the first place — CSS is for presentation, not content. Am I wrong to think screen readers are doing this wrong, and not developers? What I meant was, I only tested it on VoiceOver.
Chris, This is a great article, and I just used it to create some great buttons for a client. Being able to scale, adjust color, and otherwise control the aspects of the icons is very, very powerful. My client loved the fact that the icon moves a little on hover — I used a simple, quick transition for that. It is the little things that make a site so much more engaging.
Also, thanks commenters and community — your thoughts and insights are greatly appreciated as well. Seems like perhaps it could be replaced option data-icon this: And if you swear to using sprites: Is there a way around this without positioning things on top of each other? Frankly speaking, I dislike icon fonts very much. They are completely unsemantic. Letters or other completely unrelated characters will be displayed instead of those icons.
This entire article is dedicated to those issues and explaining why none of those things you just said are option data-icon if you do it right. I must admit that I probably used the wrong word to describe the problem. Most web developers who have heard about accessibility tend to think that accessible code is code that works decently option data-icon screen readers. Screen readers are just one type of assistive technology and more broadly one type of user agent.
The stylesheet is applied, but option data-icon font file is not loaded or delayed network issue or not recognized support issue. The user asked their browser to always use their preferred font e.