

a[href $='.pdf'] {
padding-right: 18px;
background: transparent url(icon_pdf.gif) no-repeat center right;
}
This would attach a pdf icon to the right of any hyperlink who's URL ended in '.pdf' like this. This was pretty exciting and heady stuff. It meant I could show the file type visually with that application's icon just by including a few lines in my master css file. I didn't have to worry about it at all in my html, css would add the icon for me automatically.
This trick makes use of what the official specifications call "attribute selectors".For instance in our above example we have our anchor tag which says to style this element for anchors and then, defined by brackets, [href $='.pdf'], we see if the anchor has an href attribute and if it does if that attributes value ends in '.pdf'.
This works with any attribute. For instance...
span[id ^='google'] {
background-color: green;
}
Any span which has an id which starts with 'google' will be assigned a green background.
The kicker is that the attributes can be made up. That is you can create your own attributes. For instance, as I integrated askthecssguy's icons into my site I found his solution to work extremely well with mail and document types (pdf, word, excel, etc.), but because the ability to match and exclude substrings was so limited it didn't work very well for automatically detecting internal and external links.
I worked around this by creating my own attribute called icon and then matched it in css like this.
a[icon ^="out"] {
padding-right: 20px;
background: transparent url(http://www.hunlock.com/images/external.gif) no-repeat center right;
}
This looked for an attribute called icon and if the value began with "out" it
would show a little external icon beside the link like this.
HREF="someurl.com" icon="out">
[icon ^="out"] {
padding-right: 20px;
background: transparent url(http://www.hunlock.com/images/external.gif) no-repeat center right;
}
now worked with ANY tag which had icon="out" as an attribute, be it or .
[foo] -- Has an attribute named "foo"
[foo="bar"] -- Has an attribute named "foo" with a value of "bar" ("bar")
[foo~="bar"] -- Value has the word "bar" in it somewhere ("blue bar stools")
[foo^="bar"] -- Value begins with "bar" ("barstool")
[foo$="bar"] -- Value ends with "bar" ("I was at the bar")
[foo*="bar"] -- Value has bar somewhere ("I was looking for barstools")
With these simple selectors you can attach any style you want to any
object
either automatically (as in the case of checking for a file extension
in a HREF
attribute) or manually (by creating your own attributes). You can have
more than one attribute selector as well. For instance
[foo^="foo"][foo$="bar"] { background-color: blue } would style
anything
that had an attribute with a value which begins with foo and ends in
bar.
All by adding icon="out" to my tag like this...
The following statement will tell you if you are using Internet Explorer or not and if so which version.
Not unusual you may say, until you look closer and realise that Microsoft Conditional Comments are recognising non-IE browsers!
You can also target different versions of Internet Explorer AND non Internet Explorer.
If you are using either IE6 or a non IE browser then you will see the following text.
If you are using IE5.x or IE7 then you will not see any text.
And finally you can target specific IE browser AND non-IE browsers but feed a mixture of information to both.
The following will feed extra text to IE6 which will not be seen by non-IE browsers.
IE6 will see 'You are using IE6 and not a non IE browser' and non-IE browsers will see 'You are using a non IE browser'. IE5.x and IE7 will see 'You are using'.
I don't know what use this last one will be but there may be an occasion when it will come in handy.