Auto-matic Link Icons

CSS 2.1 has many new wonderful capabilities, and I’m going to show you one of my latest tricks I’ve picked up – automatic link icons.

First, a look at the end result :-



Now, you may have seen similar things on a few websites already, wikipedia certainly makes use of the technique to highlight external links. The astute of you may notice that the ‘external’ link isn’t actually external – think of it more as ‘absolute’. How do we achieve these results automatically? Well, that’s with the power of the new CSS selectors. 456BereaSt. has a nice article on them, but the stuff we’re concerned with is the attribute selectors.

Attribute selectors are done with [square brackets], and can include simple conditional statements, with some options for wildcards thrown in to boot. The easiest way for me to explain is to show you…

Here’s a simple ‘absolute’ link selector – a[href="http:"] – this will target all the ‘a’ tags, with the attribute ‘href’ which is equal to “http:”. Interesting, but its not very useful, because the attribute would need to be exactly “http:” for it to work. So, we add the ^ operator, which means ‘starts with’. So ( a[href^="http:"] ) means any link with a href attribute that starts with ‘http:’ – thus, every external link.

Another operator we can use is $, which means ‘ends with’. So a[href$='.pdf'] is any links that end with .pdf. Now we know how to automatically treat different links differently. Oh, and it works fine in all the most recent browsers (IE7, Firefox) And won’t have any effect on browsers that don’t understand the selectors, so its safe to use.

Now, styling the links is really a matter of personal preference, (and your design), but I the infamous famfamfam silk icon set will help you out here. Once you’ve got your icons, a little clever implimentation of styles will enable you to indent your link slightly to add you icon in as a background image. Thus;

And you’re away – limited only by your icon supply, and the volume of conditional statements you want to make! Here’s mine – covering external links, mailto links, and common document formats…

,

122 Responses to “Auto-matic Link Icons”

  1. Jonathan Nicol 18th July, 2007 at 10:46 pm # Reply

    Very nice tip James! I’ve bookmarked this one. It’s one of those nice little tricks that will display as intended in all modern browsers, and gracefully degrade in IE 6.

  2. James 19th July, 2007 at 10:42 am # Reply

    Glad it helps!

    In the event that you’ve requirements that can’t be met like this – or you need more compatability with older browsers, you can always take the base CSS that generates the iconified effect, and apply it manually as ‘normal’ styles.

  3. Pensador 19th July, 2007 at 6:20 pm # Reply

    Great article—I’ll use it on my blog! :)

  4. 22nd July, 2007 at 5:07 am # Reply

    谢谢。

  5. kisPocok 2nd August, 2007 at 8:25 pm # Reply

    test: http://www.google.hu/test.zip

  6. James 2nd August, 2007 at 9:32 pm # Reply

    Hey hey, it works! An interesting effect though with the combination of external linked files… I wonder wether I can find a fix for that…

  7. James 3rd August, 2007 at 11:46 am # Reply

    Hmm, I bet the external link+file type issue could be fixed with the :before and :after selectors?

  8. James 9th August, 2007 at 10:20 pm # Reply

    Nope… I was wrong. I guess that external link icons need a little more thought…

  9. john 23rd August, 2007 at 11:18 am # Reply

    Im a bit new to css but would a roll over effect still work on image backgrounds using your automated stylesheet if used

    a:hover [href$='.doc']

    ?????

  10. James 23rd August, 2007 at 1:46 pm # Reply

    That’s really dependant on your setup – Elements can only have one image background. So, if your links elements already have a background image either of the CSS styles could be overwritten.

    If however, the parent element of your link has the background image, they should both display their respective backgrounds.

    Does that help?

  11. Hajcc 14th October, 2007 at 7:44 pm # Reply

    hello

    i have an idea …

    add Internet explorer icon to the normal links

  12. James 15th October, 2007 at 8:40 pm # Reply

    Indeed – adding an Internet Explorer Icon for .htm pages is possible, and I’ve seen it on other websites, like those created in Sharepoint Server (Microsofts’) – but I decided to steer clear of it, so-as not to offend users of other browsers.

    It does however, give me another idea…

    Combine this CSS with another script – such as the css browser selector, and you can tailor the .htm icons to your visitors browser… i like it.

  13. Website Design Belfast 4th November, 2007 at 11:44 pm # Reply

    Good read

  14. hbadorties 10th December, 2007 at 4:20 pm # Reply

    In Dreamweaver CS3 the CSS code;

    a[href$='.pdf'] {
    display:inline-block;
    padding-left:20px;
    line-height:18px;
    background:transparent url(Images/PDFIcon.gif) center left no-repeat;
    }

    generates the error “Error parsing styles Affects: Internet Explorer 6.0″

    Any advice?

  15. James 10th December, 2007 at 4:33 pm # Reply

    @hbadorties -

    I can’t be sure without having Dreamweaver CS3 to hand, but it’s probably throwing the warning because of the a[href$='.pdf'] selector.

    IE6 doesn’t support these attribute selectors fully, meaning the styles wouldn’t be recognised by that browser, the upshot being that IE6 won’t display the custom styles for your links.

    On the plus side, it wouldn’t break anything.

    The only way around this is to manually apply the link styles with classes. So it’s not a critical warning, IE6 is just (unfortunately) incompatable with this method!

  16. rdmey 7th January, 2008 at 3:02 am # Reply

    If you’re looking for a way to do this that will work in IE6, you can do it with Javascript — users who don’t have Javascript enabled won’t see it, but that’s likely a smaller population than users who are using IE6. I’ve written about a way to do it with jQuery here:

    http://blog.rebeccamurphey.com/2008/01/06/unobtrusive-cross-browser-add-filetype-icon-link-javascript-jquery/

  17. David Jacques-Louis 23rd February, 2008 at 3:54 pm # Reply

    It works on WP as well.

  18. marketing and web design 17th March, 2008 at 1:24 am # Reply

    Great tips thanks

  19. Robin Hak 23rd June, 2008 at 5:24 am # Reply

    Nice trick. I didn’t realize I could do that. I wish more websites did this. Although the icons used in this example do look a little big.

  20. James 25th June, 2008 at 3:50 pm # Reply

    The icons in the example are standard favicon size, although I take your point.

  21. Ross B 12th July, 2008 at 12:30 pm # Reply

    Hi. Good post, James – although a bit advanced for me to follow entirely at the stage I’m at with Dreamweaver CS3. Will revisit when I know more.

    Just one query that I’m sure is simple, but that I can’t figure out. How do you add a title to an email link?

    It’s simple to add an email link and to have the onsite text link read any way you want, but how do I have the subject line of the email already typed in, so the site visitor clicking on the email link doesn’t have to type a title in themselves before sending their email?

    Would be appreciative of any help re this if it is not too time-consuming!

    Cheers
    Ross

  22. James 12th July, 2008 at 2:58 pm # Reply

    Ross – you’ll want to check out this page, “Using the mailto link”.

  23. Ross B 13th July, 2008 at 5:51 am # Reply

    Brilliant! Thanks a lot, James!

    Cheers
    Ross

  24. manas 6th August, 2008 at 9:47 pm # Reply

    Indeed useful! Thanks.

  25. Rajnikant 2nd September, 2008 at 2:25 pm # Reply

    IE 6 not support this code :-(

  26. James 2nd September, 2008 at 2:36 pm # Reply

    @Rajnikant – No, unfortunately not. You can achieve the same effect however, with javascript. Check our Rebecca Murphey’s article: http://blog.rebeccamurphey.com/2008/01/06/unobtrusive-cross-browser-add-filetype-icon-link-javascript-jquery.

  27. Wetter 16th October, 2008 at 3:09 pm # Reply

    Nice article. It was very helpful for me.

  28. Brian 21st October, 2008 at 1:06 am # Reply

    I’m having trouble with the icons showing in IE. This works great in FF, however. I’m wondering if IE needs a specific doc type statement or if there’s another reason why these won’t show? I’m using your code almost verbatim.

    a[href$='.pdf'] {
    display:inline-block;
    padding-left: 32px;
    padding-top: 5px;
    background: transparent url(/images/pdf_icon.gif) no-repeat center left;
    }

    On this page: http://www.calcounties.com/buyer-seller.php

    The icons show in FF but not IE. Ideas on this?

  29. James 21st October, 2008 at 10:54 am # Reply

    @Brian – Internet Explorer 6 and below don’t support these CSS selectors. I’ve looked at your page in IE7 and it works, but IE6 doesn’t.

    The only solution I can think of for IE6 would be using javascript (http://blog.rebeccamurphey.com/2008/01/06/unobtrusive-cross-browser-add-filetype-icon-link-javascript-jquery) which gets you the same effect by modifying the link classes.

  30. Thierry 3rd November, 2008 at 4:33 pm # Reply

    It is a great way to add icons to links, but I have a problem :

    a[href^="http:"] {
    display:inline-block;
    padding-right:14px;
    background:transparent url(/Images/ExternalLink.gif) center right no-repeat;
    }

    using this, I have an icon before all links but I don’t want to have it when I link an image , I just want it to work with TEXT. Is it possible without using classes ?

  31. James 4th November, 2008 at 10:34 am # Reply

    You can combine several CSS selectors to get it only targeting text links – try using p a[href^="http:"] {} to only select links inside a paragraph or overwrite the styles on images with img a[href^="http:"] { or a[href^="http:"] img { to reset the background image, padding and line-height elements.

  32. kevin 4th November, 2008 at 1:25 pm # Reply

    CSS 2.1 is compatible with every type of browser like IE and Firfox….

  33. css 11th November, 2008 at 9:57 pm # Reply

    Cascading Style Sheets (CSS) web design lessons
    Css link Properties Attributes – examles
    Css Link
    Exanmples 1

    Css Link Exanmples
    2

  34. Mehdi 12th November, 2008 at 12:40 am # Reply

    Awesome !

  35. Matt 2nd December, 2008 at 1:49 pm # Reply

    I do like this idea, but has anyone noticed that if you have the link in the middle of a paragraph then zoom in just once in IE7 everything goes haywire – i’ve played around trying to fix it but have been unable to.

  36. James 2nd December, 2008 at 2:04 pm # Reply

    @Matt : I never noticed that before in Internet Explorer – but I’ve tested it and I see what you mean. I wonder wether the zoom property of CSS could fix that…

  37. Colin 22nd December, 2008 at 11:54 pm # Reply

    I tried to click the underlined “actually”.

  38. James 28th December, 2008 at 11:59 am # Reply

    @Colin – the underlined “actually” isn’t really a link, just underlined text.

  39. Lee 15th January, 2009 at 5:01 pm # Reply

    I never noticed that before in IE – but I’ve tested it and I see what you mean. I wonder wether the zoom property of CSS could fix that…

    Lee

  40. Mahbub 23rd April, 2009 at 10:07 am # Reply

    These are only for CSS3 compliant browsers right?

  41. James 23rd April, 2009 at 9:06 pm # Reply

    Actually, these selectors are part of the CSS2 spec, so they don’t need CSS3 compliant browsers. Most major browsers from IE6 and up support the selectors on links.

  42. raj 11th May, 2009 at 5:26 am # Reply

    nice Auto-matic Link Icons great tutorial thanks

  43. aurrutia 11th May, 2009 at 6:15 pm # Reply

    Hi!, nice trick!

    It is possible to use something like this for all my downloads (www.mydomain.com/download.php?id=10, http://www.mydomain.com/download.php?id=11, http://www.mydomain.com/download.php?id=12 …)?

    I tried this but doesn’t work:

    a[href $=' .php?id=#'] {
    display:inline-block;
    padding-right:14px;
    background:transparent url(/Images/zip-icon.gif) center right no-repeat;
    }

    any idea?

  44. Tom 13th June, 2009 at 11:34 am # Reply

    Thanks a lot, James

  45. Yunus 18th June, 2009 at 7:47 pm # Reply

    Thanks a lot. love it.

  46. Mick 25th July, 2009 at 10:42 pm # Reply

    Thanks. You saved me a lot of grief!

  47. Thom 1st August, 2009 at 5:17 am # Reply

    Great tips thanks

  48. pdf 6th August, 2009 at 3:09 pm # Reply

    http://www.pdf.com/PDF.pdf

  49. Carola}i{ 26th August, 2009 at 4:21 pm # Reply

    Hi, thanks for this tutorial, very very useful, but I’m having a bit of a problem…
    Is there any code I could use to block the external link image appearing for all my links? Now it’s displaying in every link I have in my blog, which is upsetting my layout a bit (like links for comments and categories are all displaying my external link image :-s).
    Thanks :)

  50. James 26th August, 2009 at 7:50 pm # Reply

    Carola – the best way to limit the effects is using more specific CSS – so instead of:

    a[href^="http:"] {}

    you could do

    .content a[href^="http:"] {}

    which would only affect the links inside the content container, and leave everything outside it alone.

Leave a Reply

Note: This post is over 4 years old. You may want to check later in this blog to see if there is new information relevant to your comment.