tags:

views:

49

answers:

2

What is the difference between these 2 selectors a[type="application/pdf"] and a[href$=".pdf"]

a[type="application/pdf"] { 
  background-image: url(/images/pdf.gif);
  padding-left: 20px;
}

a[href$=".pdf"] { 
  background-image: url(/images/pdf.gif);
  padding-left: 20px;
}
+2  A: 

One does MIME type matching and the other does extension globbing. You should probably use the first one because not everyone uses file extensions.

Delan Azabani
are both compatible in all browser except IE6? I know IE 6 doesn't support these
metal-gear-solid
Yes. All major browsers except IE support it.
Delan Azabani
For example: You mean even if we keep `.gif` extension for PDF even this will work `a[type="application/pdf"]`
metal-gear-solid
Attribute selectors aren't understood by IE6 and are OK in IE7+, you're right. Example in action: the two PDF icons in notes http://en.wikipedia.org/wiki/Ken_Burns_Effect#References
Felipe Alsacreations
@metal-gear-solid: as long as the "type" attribute is "application/pdf".
Delan Azabani
+3  A: 

The accepted answer isn't completely correct. No selector does "MIME type matching".

a[type="application/pdf"] will match all links where the "type" attribute is set to "application/pdf". If you want to display a PDF icon you'll need to add type="application/pdf" to all the approprite links.

This is exactly what the type attribute on links is intended for (see the spec), to provide a "hint" to the MIME type. However, the browser doesn't actually know what the type of a file is until it starts downloading it. Just wanted to clear that up.

The other selector, a[href$=".pdf"], matches the URL of the link only. It will match any links that end in .pdf, whether they are actually PDF files or not. And of course, it won't match URLs like file.pdf?v=2.

Your best bet is to mark all links to PDF files manually, either with the type attribute, or since you want IE-compatibility, just a regular class instead.

DisgruntledGoat
can't we use `a[href$=".pdf*"]` like this?
metal-gear-solid
You can use `a[href*=".pdf"]` to match any URL that contains `.pdf` in it. But as I was trying to imply, that is not very reliable. Whatever the case, for reliability you need to explicitly specify that a link is a PDF somehow, whether you add a simple class or use the `type` attribute described above.
DisgruntledGoat