I would be happy to hear from you!

Phone847.220.7429

Email

LocationChicago, IL

AIMletscounthedays

Resume / CV HTML PDF Word

Profiles LinkedIn

Say Hello! hello@shayhowe.com  –  847.220.7429

letscounthedays - The Online Portfolio of Shay Howe

/ NotebookWords, ideas, press, and communications.

Hyperlink CSS Attribute Selectors

Document Links Styled with CSS Attribute Selectors

Document Links Styled with CSS Attribute Selectors

With CSS3 gaining popularity so are CSS attribute selectors. CSS attribute selectors have been around for some time however CSS3 adds more selectors to the mix bringing new light to subject. Increased browser support, with nearly every major browser currently supporting attribute selectors (Internet Explorer 6 still struggles), has also added to the popularity.

So what benefit do CSS attribute selectors have to adding style to document links? CSS attribute selectors allow you to quickly and easily add styles to document links while keeping your code clean and clutter free. Ultimately, the more attractive looking your links are the more clicks they will receive.

PDF Hyperlink Example

HTML Code

<a href="#.pdf" title="PDF File Link">PDF File</a>

CSS Code

a[href$=".pdf"] {
	background: url(acrobat.gif) no-repeat left center;
	padding-left: 20px;
}

Output

PDF File

Breaking Down the Attribute Selector

You start your CSS just as if your were to style any other hyperlink. Directly following the a element comes the attribute selector, in this case [href$=".pdf"]. The attribute selector will always be wrapped in [] brackets and will always sit right next to the element to which it applies to without any spaces in-between the two.

Within the [] brackets the href attribute is declared as we are specifically targeting hyperlinks. Next we insert the $= selector, which specifies that we only want to target hyperlinks ending with the file format value to follow. For this example we are using a PDF document making the value .pdf, which is wrapped in "" quotations.

To target any other type of document just change the value to what ever file extension you wish. As an example, to target Microsoft Word documents just change the .pdf value to .doc.

Taking It a Step Further

Chris Coyier from CSS-Tricks has put together and outstanding resource on CSS attribute selectors in his article “The Skinny on CSS Attribute Selectors”. To learn the full potential of CSS attribute selectors this article is a must read as it does a great job of breaking them down for you.

If you are worried about the lack of support for Internet Explorer 6 you can, with a some additional work, use CSS attribute selectors via jQuery. Zac Gordon has written a great tutorial, “Selecting and Styling External Links, PDFs, PPTs, and other links by file extension using jQuery”, on how to do so.

Adding an icon to your document links is a great way to quickly draw more attention to the link. InstantShift has put together an article packed full of icons. If you would like some to use on your website check out the article at “75 Free Useful Icon Sets for Web Designers and Developers”.

About the Author

Shay Howe

Shay Howe is the man behind letscounthedays. He works as a professional web and user experience designer with a love for creating all that is interactive. Keep up with him on LinkedIn and feel free to follow him on Twitter.

Thank You for Spreading the Word

Please Share

Bookmark Hyperlink CSS Attribute Selectors via DeliciousDelicious
Digg Hyperlink CSS Attribute Selectors via DiggDigg
Stumple Hyperlink CSS Attribute Selectors via StumpleUponStumbleUpon
Share Hyperlink CSS Attribute Selectors via RedditReddit
Float Hyperlink CSS Attribute Selectors via Design FloatDesign Float
 

Leave a Comment

Post a Comment

Guidelines
  • Please do not spam, and keep comments related to topic. Unrelated comments will be deleted.
  • Basic HTML tags are allowed:
    <a href> <abbr> <acronym> <blockquote> <code> <em> <strike> <strong>
 
2. James Gill on April 4th, 2010 at 6:03 am

Damn that’s a helpful tip. I’ll definitely be using attribute selectors on a much more regular basis now. Thanks for sharing.

1. Sachin @ Web Design Mauritius on April 2nd, 2010 at 6:14 am

Thanks for breaking down these selectors Shay. As time goes by CSS3 is getting more and more interesting and these small things are what the French call “la cerise sur le gâteau” (the cherry [topping] on the cake”. I’ve got to really start digging further in the use of CSS3.

Back to Top