Welcome to the David Walsh Blog! This website was created to reach every audience possible. Please contact me at david@davidwalsh.name to let me know if I can do anything to further improve your experience on this website.

The Article


Bookmark and Share

Advanced CSS Links - Spice Up Your Links

Remember the days where links were just a standard underline? Neither do I, thankfully. CSS allows us to add background color, text decoration, color swapping, and numerous other formats to denote links before and during hover/active events. We can even change the color of the link based on whether the user has visited the destination previously. Do these links really provide as much information with as much flare as they could be though?

One suave developer created a Firefox plugin called Link Alert. Link Alert analyzes a link’s HREF upon mouseover to change your mouse cursor to a lock if the link takes you to a secure URL, to an Adobe icon if you are opening a PDF file, to a double box icon if the link will open in a new window, and so on. Wouldn’t it be great if we could convey that to our website visitors without needing them to install a new plugin? We can, but why wait until they’ve moused the link to let them know what it is?

Using advanced CSS tactics and some free icons, we can make our links say a lot more about themselves.

The Files

PDF Icon (16px x 16px)

The Code

There are two ways to go about creating special links — designating classes for each type of link or use a method that is NOT IE6 friendly. Since our goal is to enhance and not to ensure cross-browser capabilities, we will go the more dynamic, classless route which will be much easier to maintain. Also note that this could be done using javascript but isn’t CSS so much easier to use?

Here’s the CSS code:

a[href$='.pdf'] { padding:0 20px 0 0; background:transparent url(/graphics/icons/pdf.gif) no-repeat center right; }

The code is pretty self-explanatory. The $= directs the browser to match any anchors that end in “.pdf” (find all PDF files), pad the link 20 pixels on the right, and insert a small PDF icon. You can use this code for any extension, but I recommend doing this for javascript (.js), images (.jpg, .gif), new windows (you’d match “target$=’_blank’”), PDF files (.pdf), email links (”href^=’mailto:’”), and RSS feeds (”href$=’whatever.xml’”).

The Result

Link

11 Quality Comments

8-09-07
2:54 am

Andrea says:

This is ok in Firefox but - as you said - doesn’t work in IE6, because the latter doesn’t support the attribute selection in CSS.

Unfortunately, this approach breaks in IE7 - precisely because IE7 *does* support the attribute selection!

Indeed, if your link wraps over two lines, you’ll have an unpleasant surprise.

I think the way IE works is as follows: for the background’s dimensions calculation, you will not have a slim box of the height of a line wrapping like a snake over different lines, but a single box that holds them all. As a consequence:
+ the background’s right will be the rightmost point of any of the lines involved (not of the last, as in Firefox);
+ the background’s height center will be the center of the height of all the lines combined (again, not only of the last);
+ however, the right padding will be added only to the last line;
= Disaster: the image will display to the right of the block of text (not of your link’s last line) and somewhere centered between all the lines of the link (usually you’ll only see a small part of it at the right of the first line, covered by the link’s text).

The same applies to IE6 if you extend this technique by using classes to mark the links you want to apply such a rule to.

I met this problem a couple of years ago when I tried to implement exactly the same solution, and had to use an ugly workaround which would only work under certain conditions - which were met in the specific context pf my problem. Maybe somebody has a general solution for it?

8-09-07
6:37 am

Dave says:

General solution to IE wrapping problem:

Use CSS :after speudo selector with the content attribute for browsers that support it, or use JavaScript to loop though the links and add the image.

8-09-07
8:30 am

Andrea says:

Dave,

true. I was hoping for a solution that would work in IE6 and without javascript, but after all your second option would work fine in most scenarios, so as long as it isn’t critical to have the links properly ‘dressed up’, why not.

8-09-07
2:23 pm

daniel says:

surely you could use a {white-space:nowrap;} to avoid the problem above. It might make really long links and paragraphs containing them a bit ugly, so the solution is to not use long links :)

i would see no special reason to dress up IE6 specifically. im all for progressive enhancement.

davidwalsh83
8-09-07
6:20 pm

davidwalsh83 says:

Thank you for posting Dave, Daniel, and Andrea! The idea in this situation is to use CSS to enhance — these “spiced” up links aren’t meant as a necessity. Great solution too Daniel!

8-10-07
8:55 am

Andrea says:

David,

it’s a pleasure!

I’m all for progressive enhancement, too, but if a solution fails ‘ungracefully’ in an A-grade browser such as IE7, the term doesn’t apply.

In my view Daniel’s - clever - solution may save the day most of the time but would prove unacceptable in still too many cases, as it means that a design constraint limits your editorial choice (in particular if you have relatively narrow text columns) - and as he said, where the rule applies the result tends to be a bit ugly (and sometimes maybe even *very* ugly :o))

From this point of view, Dave’s :after solution seems more reliable: the following works in Firefox and doesn’t break neither in IE6 nor in IE7 (haven’t tried Opera or Safari, though):

a[href$='.pdf']:after {content: ” “url(/graphics/icons/pdf.gif)}

Thanks to all for the tips.

davidwalsh83
8-11-07
7:24 am

davidwalsh83 says:

Thanks for the ‘:after’ tidbit.

One rule I have for designing my sites is that I don’t place links within content paragraphs. I place the conversion link after the paragraph to make it stand out, much like my example link.

6-09-08
9:41 pm

Bert says:

As well as Andreas’ “:after” tip you can also use “:before” , for example

a[href$='.pdf']:before { content: url(/icons/pdf.gif); padding: 0 5px 0 0; }

Also, as well as defining file extensions you can also distinguish links to individual websites;

a[href*='youtube']:before { content: url(/icons/youtube.gif); padding: 0 5px 0 0; }
a[href*='wikipedia']:before { content: url(/icons/wiki.gif); padding: 0 5px 0 0; }

Now, can anyone tell me if it’s possible to have a default icon when all other rules fail?

Be Heard!

Your email is never published nor shared. Required fields are marked *.

Name*:
Email*:
Website:  


 

 

 

comments twitter