Using CSS 3 selectors to apply link icons

I mentioned earlier this year a tutorial which shows how to use CSS 3 selectors to enhance hyperlinks. Of course, you don’t need to limit yourself to just the href attribute; with CSS 3 selectors, you can use the same technique for any tag which has an attribute. I’m going to give a couple of quick examples, which will output the following result (of course, you’ll need a browser better than IE6 to see them!):

All icons are from the fantastic collection at famfamfam.com.

The first list item uses the general attribute selector to look for any tags with an accesskey attribute and display an icon to alert users to its presence:

#test_selectors a[accesskey] {
background: url(’icon_key.gif’) no-repeat 0 50%;
text-indent: 20px;
}

The second item looks for links with a type attribute value of ‘application/pdf’ (you do mark up links to documents using the type attribute, right?) using the exact attribute selector, then inserts an icon advising you of the destination document:

#test_selectors a[type='application/pdf'] {
background: url(’file_acrobat.gif’) no-repeat 0 50%;
text-indent: 20px;
}

Finally, if you have a multi-language site you can link to another version using the lang attribute with the value of that language, then use the language attribute selector to apply that country flag:

#test_selectors a[lang|='fr'] {
background: url(’fr.gif’) no-repeat 0 50%;
text-indent: 20px;
}

As always there is much more you can do with the selectors; if you use or develop this idea on your site, post links here for us to see.

  • Digg
  • del.icio.us
  • Facebook
  • Print this article!
  • StumbleUpon
  • TwitThis
  • E-mail this story to a friend!
July 15th, 2007 by Peter Gasston in Declarations, Modules, Tutorials. You can leave a response, or trackback from your own site.

23 Comments to “Using CSS 3 selectors to apply link icons”

  1. Vakio Says:

    “Flag as a symbol of language - stupidity or insult?” by Jukka Korpela:

    http://www.cs.tut.fi/~jkorpela/flags.html

  2. Peter Gasston Says:

    Debates on what icons to use aside, the technique still stands.

  3. Navarr Barnier Says:

    Lovely Article… *starts marking the type attribute on his links*

  4. Arjan Eising Says:

    A similar point as Vakio: the accesskey icon isn’t good… as user I might think that there is a secure HTTP connection used when I click on that link.
    But sure, the technology stands, and I use it already on my web log.

    Of course, you don’t need to limit yourself to just hyperlinks.

    You give examples using hyperlinks ;)

  5. Peter Gasston Says:

    You’re right, Arjan, that sentence wasn’t as clear as I wanted it to be; I’ve changed it now.

    Also, the icons were just what was available to me; I wanted to use a keyboard key instead, but there wasn’t one!

  6. kalikiana Says:

    Unfortunately I cannot use these neat tricks because most of my users wouldn’t see them. This is especially annoying as the semantics of using actual images in these cases are questionable.

    I would love to see more examples and maybe with no hyperlinks. :)

  7. nemeseri Says:

    It’s nice, but you are using CSS 2 selectors in this article.. :p

    http://www.w3.org/TR/REC-CSS2/selector.html

  8. Peter Gasston Says:

    Well, I can’t really argue with that, nemeseri; however, there are a few different CSS2.1 declarations we’ve ‘adopted’ into CSS 3 as they don’t yet have widespread (or any) browser support; attribute selectors are one example of this.

  9. Jake Archibald Says:

    You may run into problems with IE…

    IE positions backgrounds on inline elements differently to other browsers. It’s not doing it wrong, it just wasn’t in the CSS2 spec. If your link wraps across lines, you may find your icon won’t show in IE.

  10. Robin Says:

    On your French link you should be using the hreflang attribute and styling off that (although the lang attribute still holds as the text content of the link in in French).

    Also, CSS3 has the :lang() pseudoclass, which is better as it’s more general in its applicability besides being a byte shorter :)

  11. David Mead Says:

    Nice article Peter.

    I think the comments about icon choice are valid but ultimately it’s the method that’s important. I’ve been using this on my blogroll showing XFN and it’s a great way show information without a ton of classes.

    Also, I hadn’t thought of the type attribute on links.

  12. Sebastian Winkler Says:

    Well, if IE6 doesn’t render this either, I see no reason why not to use the more sophisticated

    selector:before { content: url(your/path/goes/here.extension) }

    All set in one declaration.

  13. Peter Gasston Says:

    @ Sebastian: IE doesn’t support generated content, and perhaps never will; at least this way, IE7 and above will show the icons.

  14. fearphage Says:

    In most cases, I prefer:

    #test_selectors a[href$=".pdf"]

    over

    #test_selectors a[type='application/pdf']

    because I don’t have to add extra properties. Another handy one is

    a[href^="http://"], a[href^="https://"]

    to flag external links assuming the rest of your page is relatively linked.

  15. Peter Gasston Says:

    Using type=”application/pdf” is semantically better, and therefore better in terms of accessibility.

  16. Mail Says:

    @ “Debates on what icons to use aside, the technique still stands.”

    Negative. Accesskeys and flag use is actually being dropped.
    Blind/non-blind people agree that accesskeys are a nuissance, due to different system implementation and (different) browser conflicts with shortcuts, et caetera.
    And flag use… is not used. Text works pretty well for everybody.

  17. Peter Gasston Says:

    Mail: The technique does still stand; you can debate the examples I provided all you like, but the technique is still perfectly usable.

  18. Blog WebNatal - Divulgando os Padroes Web - XHTML, CSS, Microformats, Design, Usabilidade e Acessibilidade - Natal RNWebNatal - Divulgando os Padrões Web » Usando Seletores CSS3 para aplicar ícones em links Says:

    [...] Clique e veja o exemplo implementado [...]

  19. Belinda Says:

    Nice article!

    Does anyone know a solution or workaround for IE so the icons are visible if wrapping over a line?

  20. Nick Presta Says:

    Neat example! I understand the benefits of the TYPE attribute over the extension match (a[href$='.pdf'] { /* */ }) but I think both have their place.

  21. digiTalized » Blog Archive » CSS3 attribute selectors and links Icons Says:

    [...] After completing my experiment, I ran across a similar one at css3.info. [...]

  22. 20 Very Useful CSS3 Tutorials | Speckyboy - Wordpress and Design Says:

    [...] 8.Using CSS 3 selectors to apply link icons [...]

  23. werutzb Says:

    Hi!

    I would like extend my SQL knowledge.
    I red so many SQL books and would like to
    read more about SQL for my work as db2 database manager.

    What would you recommend?

    Thanks,
    Werutz

Leave a Comment