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: 11% [?]

 

Related posts

| Subscribe to Feed | Email the author

8 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

 

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

To insert a code, don't use <code>...</code>. Instead, use <pre lang="LANGUAGE" colla="-">...</pre> and replace 'LANGUAGE' by 'html4strict' for HTML, 'php', 'javascript', 'css'...