Attach Icons to Links Automatically

Let your readers know before they click a link and it opens up some large application like Acrobat, Word, Excel, or Quicktime.

The HTML for the above link has no styles attached to it:

<a href="news/issue12.pdf">View Latest Issue</a>

The trick is all in the CSS:

a[href $='.pdf'] {
 padding-right: 18px;
 background: transparent url(icon_pdf.gif) no-repeat center right;
}

Now when you link to a PDF, your reader will be prepared. Other ideas for this include Word and Excel docs, and even mailto: links (for which you need to use the ^= option instead of $= to search the beginning of string). Looking at the first line of the code, you can see this can be used on any element and any of its attributes.

16 Comments

  1. Posted June 17, 2008 at 10:35 am | Permalink

    This is a fantastic tip!

    We do a lot of websites for trade associations and this tip would definitely save us and our clients time. Thanks Kevin!

  2. Posted July 5, 2008 at 9:03 am | Permalink

    That’s awesome!
    Definitely adding CSS For Lunch to my Google Reader.

  3. Posted July 26, 2008 at 5:44 pm | Permalink

    Pretty sure that won’t work in a couple of browsers :) but nice quick little add on, thanks for the tip!

    I think i would use jquery (since i always use it, might as well use its functionality) to do the same selection but add a generic class to each element matched. maybe .pdflink and add the css to that. This way it will load in ie6 and such.

  4. Posted August 23, 2008 at 11:24 am | Permalink

    Wow, great tip! I think I’m going to do something that’s in a bit of reverse, and add an “external link” icon to appropriate content. So if we can use ^= to search the beginning, I can set links of a certain class to have the external link icon, UNLESS it starts with my domain. Very cool!

  5. Al
    Posted August 23, 2008 at 12:01 pm | Permalink

    Search “iconize”. Someone already did the legwork.

  6. Posted August 23, 2008 at 5:58 pm | Permalink

    PDF files support data after the .pdf similar to how URLs do with query strings. These can direct your to a specific page. I suspect that the [href $=’.pdf’] should be a bit more complex for this to work in all cases

  7. Posted August 23, 2008 at 10:03 pm | Permalink

    I love this.
    Very Very Helpful.

  8. Posted August 24, 2008 at 12:51 am | Permalink

    Interesting! Please contact us for further marketing. Kind regards / Oakleaf Marketing http://www.oakleafnow.com

  9. Posted August 25, 2008 at 9:36 am | Permalink

    very usefull!

  10. Posted August 25, 2008 at 6:55 pm | Permalink

    Wow, that’s a great effect, I’m going to use that.

  11. Posted August 25, 2008 at 11:44 pm | Permalink

    Very useful, clever….

  12. Posted August 30, 2008 at 5:13 am | Permalink

    Wow, what a simple an elegant solution, I’ll defintately be using this on my sites from now on, Thanks!

  13. Posted September 1, 2008 at 10:38 am | Permalink

    I stumbled this article on accident and im sure glad i did, GREAT TIP! I have been looking for something like this for a while now. Thank you, keep it up!

  14. Posted September 7, 2008 at 5:55 pm | Permalink

    Great tricks, hope people stop using ie6 so we can use this

  15. Posted October 24, 2008 at 1:31 pm | Permalink

    Very nice, no more manually linking those!

  16. LeeJH
    Posted October 30, 2008 at 1:29 pm | Permalink

    This is how Wikipedia adds its link images. You can make the icons only appear next to external links and not JavaScript links, as I’ve done here… http://discussionator.com/unify.html

11 Trackbacks

  1. […] CSS for Lunch Posted by Tiago Filed in Geek, […]

  2. […] Read more […]

  3. […] CSS for lunch ยป Attach Icons to Links Automatically (tags: webdev webdesign web tips css) […]

  4. By Vicodin. on August 29, 2008 at 12:38 am

    Vicodin side effects….

    Cheap vicodin. Vicodin side effects. Vicodin no prescription. Buying cod vicodin. Buy vicodin online without. Buy vicodin. Vicodin withdrawal. Vicodin withdrawal symtoms….

  5. By Vicodin. on August 31, 2008 at 2:04 am

    Long term vicodin use….

    Signs of vicodin addictio. Vicodin. Buy vicodin without a prescription. How l ong does vicodin stay in your system….

  6. By Percocet without prescription cheap. on August 31, 2008 at 12:21 pm

    Cheap percocet….

    Percocet without prescription cheap cod. Percocet without a prescription cheap. Cheap percocet….

  7. By I quit percocet. on September 4, 2008 at 4:25 pm

    Percocet high blood pressure….

    Compare brand name and generic percocet. Percocet….

  8. […] Attach Icons to Links Automatically Let your readers know before they click a link and it opens up some large application like Acrobat, Word, Excel, or Quicktime. […]

  9. By Eternal nymphet models. on September 5, 2008 at 12:32 pm

    Nymphet tgp….

    Nymphet panites. Nymphet. Bbs and nymphet. Youngest nymphet sites. Nymphet russian….

  10. By Buy percocet online without office visit. on September 12, 2008 at 4:09 pm

    Buy percocet online….

    Buy percocet online. Buy percocet online no prescription. Buy percocet online without office visit….

  11. By vtafljxy on October 12, 2008 at 12:03 am

    vtafljxy…

    vtafljxy…

Post a Comment

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

*
*
vpn service