Close Panel
 
 

24

Sep

2007

Add File Type Icons next to your links with CSS

By jeeremie. Posted in HTML / CSS

Many people use Javascript to automatically add file type icons to their external links, mailto links, Word, PDF, Excel Document links… when it can be done very easily with CSS 2.1.

Here is an example:

File Type Icons

HowTo:

The trick is to use the attibute selector (using the [square brackets]) with the “a” tags. For example, if you want all your external links to display an icon, use a[href^=”http://”] in your CSS stylesheet:

a[href^="http://"] {
background:transparent url(../images/external.png) center right no-repeat;
display:inline-block;
padding-right:15px;
}

The operator “^” means “start with”. Thus the code a[href^=”http://”] will add an icon to links starting with the attribute “http://”.

The operator $:

Another useful operator is $ wich means “end with”. You can use it with any extensions you might think of (.doc, .pdf, .xls, .zip, .rar…).

e.g. a[href$='.zip'] will display an icon next to any links to a document ending with the extension .zip :

a[href$='.zip'], a[href$='.rar'], a[href$='.gzip'] {
background:transparent url(../images/zip.png) center left no-repeat;
display:inline-block;
padding-left:20px;
line-height:18px;
}

Some more examples:

You could use this to customize links to PDF documents, Word documents, mailto… Here are some examples you might want to use in the future:

/* PDF document links */
a[href$='.pdf'] {
background:transparent url(../images/pdf.png) center left no-repeat;
display:inline-block;
padding-left:20px;
line-height:15px;
}
 
/* Excel Documents links */
a[href$='.xls'], a[href$='.csv'], a[href$='.xlw'], a[href$='.xlt'] {
background:transparent url(../images/excel.png) center left no-repeat;
display:inline-block;
padding-left:20px;
line-height:15px;
}
 
/* Word Document Links */
a[href$='.doc'], a[href$='.rtf'], a[href$='.wps'], a[href$='.txt'] {
background:transparent url(../images/word.png) center left no-repeat;
display:inline-block;
padding-left:20px;
line-height:15px;
}
 
/* mailto: links */
a[href^="mailto:"] {
background:transparent url(../images/mailto.png) center left no-repeat;
display:inline-block;
padding-left:20px;
line-height:15px;
}

The icons

You can download some free icons at:
IconFinder.net
FamFamFam

Note:

The attribute selector works fine in the most recent browsers.

Technorati Tags: , , , , , ,

Popularity: 14% [?]

 

Related posts

| Subscribe to Feed | Email the author

31 Responses to “Add File Type Icons next to your links with CSS”

  1. 1
    Diane Says:

    Great advice as usual, Jeremie!

    Thank you!

  2. 2
    Absolute Says:

    IE6 not support!

  3. 3
    shane plasebo Says:

    I’ve found this code in the Tripoli CSS framework.

    By the way, some of the pseudo-classes defined do not work on IE

  4. 4
    shane plasebo Says:

    You will have to define a class in your CSS and call in the tag for IE to use it.

  5. 5
    Jeremy Schrader Says:

    anyone got this to work on IE 6?
    tag for IE to use?? what are the specific tags…?
    I assumed that it was working since it displayed at the top of the page, but then looked again and noticed that the “example” was not actually a working example.

    http://schraderdesigns.com/VanguardGMAC/VRO/Forms_Drawer.shtml

    I’ve got it working on just about everything else. very easy setup by the way, and glad you linked the icon finder link – hadn’t seen that one before, very useful.

  6. 6
    Jeeremie Says:

    Hi Jeremy,

    Yes, you are right. It is not a working example (I didn’t want to have an icon for all my external links and mailto links).
    This code is not supported in IE6. I am sorry if I forgot to mention it.

  7. 7
    rdmey Says:

    If you’re looking for a cross-browser method of accomplishing this, Javascript will do the trick. I’ve written about a way to do it with jQuery here: Unobtrusive, cross-browser method to add icons to links

  8. 8
    Jeeremie Says:

    Thank you very much for this very interesting post. This will be a great addition to mine.

    Could be nice if you show us an example on your post

  9. 9
    Beach Says:

    who cares if IE6 isn’t supported. by now no one should be supporting that dinosaur anyway.

  10. 10
    shahriveri Says:

    Hi,
    great tips but i have a big issue which i’m hoping you could offer some advice.
    I am currently styling an asp.net website from out of house developers and have tried enforcing the icons for the links. Users can upload files but currently the links to word docs and pdf’s are generated through a javascript__doPostBack and so the the icons do not appear. is there anyway of pinpointing the asp/javascript generated links in the css so the icons will show?

    will appreciate any advice.

  11. 11
    Emre Toprak Says:

    Thank you!

    yeah ie6 not supported, but it’s not problem.

  12. 12
    blogg | animadverto corporate blog Says:

    [...] dosya tipini ikon şeklinde göstermek [...]

  13. 13
    tasarım, css, bağlantı, link, css kod, simge link, ikon link | Bilinmeyen Gerçekler Antiduzen - Kralblog Says:

    [...] dosya tipini ikon şeklinde göstermek [...]

  14. 14
    8 CSS tips for better linking « Themeflash.com Says:

    [...] If you are linking directly to a file, why not let your users know about it? I’m always terribly annoyed when Acrobat launches by surprise. One way many thoughtful web writers deal with this issue is by adding (PDF) after the link. It would be even better if you used the proper CSS technique that does it automagically! Web-kreation has a good explanation on how to do that. [...]

  15. 15
    10 astonishing CSS hacks and techniques Says:

    [...] Source: Add File Type Icons next to your links with CSS [...]

  16. 16
    The Frosty Says:

    Very cool!

  17. 17
    10大跨浏览器的CSS hack技术与应用 « SonicHtml工作室- PSD转HTML / XHTML,CSS / W3C验证 / 多浏览器支持 / WordPress模板 / Joomla模板 Says:

    [...] 来源:Add File Type Icons next to your links with CSS [...]

  18. 18
    10个非常有用的CSS hack和技术| CSS| 前端观察 Says:

    [...] 使用CSS选择器创建个性化链接样式使用CSS为链接添加文档类型图标 VN:F [1.4.5_712]提交中…Rating: 0.0/10 (0 votes [...]

  19. 19
    10个非常有用的CSS hack和技术 | 互联网的那点事... Says:

    [...] 资源: 使用CSS选择器创建个性化链接样式 使用CSS为链接添加文档类型图标 [...]

  20. 20
    Mastering CSS, Part 1: Styling Design Elements | CSS | Smashing Magazine Says:

    [...] Add File Type Icons next to your links with CSSAnother tutorial for adding file type icons to your links. [...]

  21. 21
    Mastering CSS, Part 1: Styling Design Elements « Tech7.Net Says:

    [...] Add File Type Icons next to your links with CSSAnother tutorial for adding file type icons to your links. [...]

  22. 22
    Wordpress Blog Services - Mastering CSS, Part 1: Styling Design Elements Says:

    [...] Add File Type Icons next to your links with CSSAnother tutorial for adding file type icons to your links. [...]

  23. 23
    Mastering CSS, Part 1: Styling Design Elements - Programming Blog Says:

    [...] Add File Type Icons next to your links with CSSAnother tutorial for adding file type icons to your links. [...]

  24. 24
    Bradford Sherrill Says:

    Nice, needs support for IE6 though

  25. 25
    Myfreepedia.com » Blog Archive » Mastering CSS, Part 1: Styling Design Elements Says:

    [...] Add File Type Icons next to your links with CSSAnother tutorial for adding file type icons to your links. [...]

  26. 26
    Advertisers Blog » Mastering CSS, Part 1: Styling Design Elements Says:

    [...] Add File Type Icons next to your links with CSSAnother tutorial for adding file type icons to your links. [...]

  27. 27
    Benga Says:

    While not supported by ie6, this is still an interesting post. The little things that are essential to know, which will save any designer/developer sometime when need.

    Benga creative

  28. 28
    Kennith Nichol - Conversion Expert Says:

    It’s quite surprising to find out how many CSS 2.1 features are overlooked or completely forgoet. As for IE6 support, obviously in this situation it would be required to employ a supplementary stylesheet which will require class attributes on the link tags, but that’s only if you want to support a 2 generation old browser. (which, of course, most clients really appreciate).

  29. 29
    Link icoontje - webhostingtalk.nl Says:
  30. 30
    Kawika Says:

    THANKS! This is awesome and just what I was looking for! I didn’t know CSS was so powerful.

  31. 31
    精通CSS造型设计元素 Says:

    [...] Add File Type Icons next to your links with CSS 为链接添加文件类型图标的教程。 [...]

 

Leave a Reply

Wrap all code in <pre lang="LANGUAGE" colla="-">...</pre> and replace 'LANGUAGE' by 'html4strict' for HTML, 'php', 'javascript', 'css'...