There are numerous hacks on how to isolate some css rules from various browsers. But most of those ie6 hacks fail on ie7. Microsoft did obviously make some fixes here.
Many developers warn us not to use css hacks, instead we should use conditional comments. For me, these are really not the best option, for:
- Version comparisons fail with multiple explorers installed (or at least they do for me)
- I don’t like the idea of having extra html just because IE is a “special” browser. extra css.. ok. It’s the css that doesn’t work correctly in explorer, not html.
So I’ve been struggling to find a css hack, that would validate. There are quite a few out there, but most of them don’t work very well. So I’ve stumbled upon this website.
And this is the hack I liked:
*:first-child+html {}
This will only target html tag in IE7 (confirmed not to work in FF2, safari 2, opera 7 & 8 & 9, IE6). What it does is it targets every html tag, which is preceeded by some other tag, that is the first child of any third tag. We all know (or atleast we should) html is the topmost tag of our page and it has no preceeding element. Luckily, explorer doesn’t know that. And by adding some fancy selectors, we confuse IE6 and lower, so they won’t apply any rules.
Needless to say, we can address any element, not just the html element by knowing some css. For example:
*:first-child+html div { background:red; }
will make all divs in IE7 have a red background.
Word of cation though - if you want to apply rules to IE7 and IE6, you can not just put hacks for both in the same line. For example:
*:first-child+html, * html { /* rules... */ }
won’t work in IE6, while
*:first-child+html { /* rules... */ }
* html { /* same rules again... */ }
will.