Add Progressive Icons to Your Site Using :after pseudo-element
Preview & Files
Before we get started, check out a preview of what we are trying to accomplish, and grab the files to follow along. Ideally enough, the previous sentence was a preview in its own right.
Overview
Lately, I’ve been getting more and more into designing with progressive enhancements in mind. I think it’s a combination of forward wishful thinking, and a spiteful attitude towards IE. It’s also encouraging that over 67% of this blog’s readership browses through on Firefox, and only 13.5% do so in IE. No doubt that’s shifted my thinking and gives me the confidence to play around with some of the cool new features of CSS3.
In the steps that follow I’ll show you how to add descriptive icons to your links using a CSS pseudo selector element. Pseudo selectors were introduced back in CSS 2.1, but the fine folks at IE still haven’t adopted them. This small progressive enhancement greatly improves a site’s usability by helping the user understand what type of link they would be clicking. In the end I’ll reluctantly show you how to make things work in IE using a background image.
How It’s Done
In the demo, I styled PDFs, Word docs, and external links, but conceivably you could assign an icon to any file extension that you can think of. First, you’ll need an icon. There are lots of places to find cool icons, but my overwhelming favorite has become Iconfinder.
After grabbing the icon that you need, you can assign it to a file type in the CSS. Let’s take a look at the styling for PDF’s…
1 2 3 4 | a[href$='.pdf']:after { content: url(../images/pdf.png); margin-left: 3px; } |
Awesomely enough, this syntax will work in just the way you might imagine. We tell the css to look for any files that end in .pdf and assign an image with a 3px margin on the left.
It’s beautifully simple, and in an ideal world that would be all you’d have to code. I suppose if you consider the very nature of progressive enhancements we could stop there. Afterall, the site will look just fine in IE, and those users will never know the icons should be there at all. But considering the somewhat depressing general browser statistics for June 2009 we should probably push further.
Getting IE to Behave
So how do we get things working in IE? Well, first we have to offer up a new stylesheet if the browser is determined to be IE. To do so, we can drop this handy little if statement in the head of our document.
1 2 3 | <!--[if IE]> <link rel="stylesheet" type="text/css" href="css/ie.css" /> <![endif]--> |
The CSS is also pretty simple; we just use a background image instead of a pseudo selector. Take a look…
1 2 3 4 | a[href$='.pdf'] { background: transparent url(../images/icons/pdf.png) no-repeat center right; padding: 0 20px 0 0; } |
The IE Issue
The main drawback with using a background image with a position ‘right’ attribute is how the styling breaks down if the link extends to more than one line. Despite this setback, the icons look good most of the time in IE and will only really be an issue on skinny columns. Perhaps this is best described with an illustration.
Showing A Little Class
With the way the selectors are set up for external links, the css will drop an icon in for any outgoing link. Which looks great on text links, but looks ugly on image links. Check out the example below…
To fix this, you could make a class for your images that overrides the selectors. Then you’d have to assign that class to all your outgoing image links. Like so…
1 2 3 4 | .noicon:after { content: ""!important; margin-left: 0px !important; } |
And in your IE specific stylesheet you’d use.
1 2 3 4 | .noicon { background: none !important; padding: 0 !important; } |
Lastly, you’d add this to your image links.
1 | <a href="yourlink" class="noicon" ><img src="yourimage" /></a> |
Final Thoughts
Are you starting to use progressive enhancements on your site? What CSS3 changes are you most excited about?
-
http://twitter.com/digideth B. Moore
-
http://www.myinkblog.com Andrew Houle
-
http://www.insidethewebb.com/ Jake Rocheleau
-
Jason
-
http://www.gigglecomputer.com Phaoloo
-
http://ad1987.blogspot.com AbhiTechBlog
-
http://www.cryode.com Eric Roberts
-
http://www.myinkblog.com Andrew Houle
-
http://maddon.net/blog Madeline Ong
-
http://www.magmag.biz Brant
-
http://www.3232design.com Richard
-
http://fuelyourcoding.com Doug Neiner
-
http://www.kristofcreative.com Kristof
-
http://www.myinkblog.com Andrew Houle
-
http://www.webdesignbooth.com Dicky
-
http://tutorijali.hdonweb.com/ Ivan Mišić
-
http://www.perigee-art.com Rob W
-
James
-
http://www.tomascamusso.com Tomás
-
http://www.arpia.be Peter Craddock
-
http://www.imsethstevenson.com Seth Stevenson
-
http://bradfordsherrill.com Bradford Sherrill
-
http://www.corlu.org haberler
-
Rounin
-
Rounin
-
http://www.cryode.com Eric Roberts
-
http://gandrwebdesign.com/ Garrett Winder
-
http://indigospot.com Ian W. Parker
-
http://www.perigee-art.com Rob W
-
Mats
-
Mats
-
http://www.codemyconcept.com/ CodeMyConcept


