Using CSS selectors to show hyperlink cues

January 28, 2009

I fail at CSS. I was trying to learn how to apply some CSS rules to links on this site so it will show that friendly little “this link will open in a new window” icon whenever there is a hyperlink with the attribute target=”_blank”, and well, this is my story.

Ask the CSS Guy (<-- Look, an icon indicating this link will pop up in a new window! I hope.) has a great article on showing hyperlink cues with CSS, so you can indicate to readers whether the link is a popup, external link (new window), or a download of a certain file type.

I figured out how to get the external link one working, but was having trouble with indicators where the hyperlink leads to a file download. Ask the CSS Guy’s verbatim examples simply didn’t work, or appear valid CSS according to Visual Studio and the fact that no icon displayed next to my links. However, Brian Wilson's excellent and thorough overview of the Selector specification helped me fully understand the proper syntax and options available. And voilà, CSS that actually works.

a[target="_blank"]{
    background: url(images/ExternalLink.gif) right top no-repeat;
    padding-right: 14px;
    white-space: nowrap;
} 

a[href$=".zip"]{
    background: url(images/zip.gif) right center no-repeat;
    padding-right: 14px;
    white-space: nowrap;
} 

The above CSS applied to a basic link, a link with attribute target=”_blank” and a link with href=”somefile.zip” are displayed as shown below.

image

blog comments powered by Disqus

About me

I'm a consultant with Headspring in Austin, TX. My passion is creating web-based applications that are well crafted and solve real problems for real people. Want to know more? Check out my about page.

WTF is all this code? I came here for food!

My wife made a new year's resolution to try out at least one new recipe each week. Want to know what she's been feeding me? resolutionfood.blogspot.com