CSS Trick for link icons
The great thing about CSS is that, if done correctly, it can make your life much easier. Here’s a little trick that will label links based on their attributes without you having to add additional HTML when posting the link. This trick hinges on the fact that you can identify parameters in your CSS following the a beyong the usual :hover, :visited, etc.
Note: Doesn’t seem to be working in IE8… I’ll check for other version of IE later, but FireFox, Chrome, etc. are fine.)
Suppose that you’d like to have an icon next to any link that would identify it as being a mailto link, or a link that will open in a new window.
Setup your CSS as follows:
a[href^="mailto:"] {
padding-left:20px;
background: transparent url(icons/mail.gif) no-repeat center left;
}
a[target $='_blank'] {
padding-left:20px;
background: transparent url(icons/newWin.gif) no-repeat center left;
}
Now, what about file types? No problem, setup your CSS as follows:
a[href$='.doc'] {
padding-left:20px;
background: transparent url(icons/word.png) no-repeat center left;
}
a[href$='.xls'] {
padding-left:20px;
background: transparent url(icons/excel.png) no-repeat center left;
}
a[href$='.jpg'] {
padding-left:20px;
background: transparent url(icons/pic.png) no-repeat center left;
}
You can see it in action here.
If you want to apply the same formatting to several file types, e.g, the same image icon for .jpg, .jpeg, .gif, etc., you can avoid retyping the CSS for each by setting up your CSS as follows:
a[href$='.jpg'],
a[href$='.jpeg'],
a[href$='.gif'] {
padding-left:20px;
background: transparent url(icons/pic.png) no-repeat center left;
}
If you’d prefer that your icons appear following the text as opposed to preceeding it, change your CSS to read:
padding-right:20px; background: transparent url(icons/pic.png) no-repeat center right;
Depending on the size of your icons, you may need to play with the padding. Here, I’ve used 20px as my icons are 16px wide so, given that we are centering the icon within the padding, I get a nice 2px buffer on either side. Also, if you have tall icons, or you’d like to use a smaller font size, you can add some line-height to your CSS so that the icon doesn’t get cropped.
The icons that I’m using in this example can be found here. They are licensed under a Creative Commons Attribution 2.5 License, so you can use them anywhere, for any purpose, and make whatever alterations to them that you would like.










