Target IE9 Only via CSS [duplicate]
Asked Answered
T

5

22

Just wondering given these IE hacks in my bag of tricks

"\9" - for IE8 and below.
"*" - for IE7 and below.
"_" - for IE6.

i.e. such as

body { 
    border:2px solid blue;
    border:2px solid yellow \9;
    *border:2px solid green;
    _border:2px solid orange;
}

Whether anyone has such a hack for IE9 ? i.e. I'm trying to target IE9 only via CSS ?

Thigmotaxis answered 11/7, 2011 at 18:4 Comment(4)
This will come back and bite you when you least expect it. Use a conditional comment instead: <!--[if IE 9]><![endif]-->.Replay
@nightfirecat - awesome thanks. tried searching and couldn't find anything :) seems the source is - blog.vervestudios.co/blog/post/2011/05/13/…Thigmotaxis
It's such a pity that Microsoft never implemented conditional comments in CSS. The * and _ are effectively de-facto standards for IE6 and IE7; in lieu of Microsoft implementing standards, we'll always need these rough & ready hacks. At least IE10's looking promising so shouldn't need hacks.Languishing
Conditional comments in HTML are useless in situations where you can't modify the HTML.Original
P
29

I suggest using condcoms to feed an IE9 css file or have a conditional html class, similar to:

<!--[if lt IE 7]> <html lang="en-us" class="no-js ie6"> <![endif]--> 
<!--[if IE 7]>    <html lang="en-us" class="no-js ie7"> <![endif]--> 
<!--[if IE 8]>    <html lang="en-us" class="no-js ie8"> <![endif]--> 
<!--[if gt IE 8]><!--> <html lang="en-us" class="no-js"> <!--<![endif]--> 
Peder answered 11/7, 2011 at 18:6 Comment(12)
thanks @meder - but I want to try and use direct CSS instead :) i.e. because You cannot use condcoms in the CSS code itselfThigmotaxis
@Tim: The problem with the hacks you describe above is that they are bugs. There were never intended to be features. By using them, you may cause problems with other browsers with similar bugs. Stick to standards.Replay
@Thigmotaxis - it's not recommended to rely on hacks because with future iterations of the browser, those hacks could stab you in the back. If you adopt the html conditional as depicted and make one for IE9, in your main css you can do .ie9 body { } for example..Peder
@meder @andrew - yeah I suspected as much. just noted that Google was using such hacks in Google+ [wow, yes Google] - so assumed if Google engineers use them ...Thigmotaxis
Google applications don't usually have the best front end code.Peder
@Tim: Google doesn't exactly have great code review practices outside their core products... And not all software dev/engineer is equal.Replay
@andrew @meder :) you've won me over ... I'll implement a JS solution instead :) [ no fun really :D ]Thigmotaxis
Google uses hacks on purpose for the purpose of saving characters. Unless you're google and 1 extra byte translates to lots of money you should be writing good code. Google+ is a core product.Ammamaria
@Tim: Sigh... Again, a JavaScript solution also have problems. Use a condcom. It's standard compliant and works just fine.Replay
@andrew - ok ok :) was going to use quirksmode script as I need to add style elements but don't have acccess to the <head> for what i needThigmotaxis
You can put a style tag anywhere.Ammamaria
These CSS hacks do not interfere with other browsers, I have used them for years with no problems whatsoever. Yes it is preferable to use condcoms, but what if you don't have control of the HTML, only the CSS? In this case it is impossible to use a condcom if one isn't already being used and a CSS hack may be your only optionStanzel
M
42

Terrible, but it should work:

body { 
    border:2px solid blue;
    border:2px solid yellow \9;
    *border:2px solid green;
    _border:2px solid orange;
}
body:nth-child(n) {border:1px solid purple \9; /*Should target IE9 only - not fully tested.*/}
Myiasis answered 23/3, 2012 at 20:36 Comment(3)
I've tested this and confirm that it does target only IE9 successfully. And it's the only actual answer to the question that is actually correct. Advice to use condcoms is sound, but advising a different solution doesn't actually answer the original questionStanzel
Is it me or doesn't this work for the property background-image?Grizelda
\9 also seems to be targeting IE10, but IE11 seems to ignore itClout
P
29

I suggest using condcoms to feed an IE9 css file or have a conditional html class, similar to:

<!--[if lt IE 7]> <html lang="en-us" class="no-js ie6"> <![endif]--> 
<!--[if IE 7]>    <html lang="en-us" class="no-js ie7"> <![endif]--> 
<!--[if IE 8]>    <html lang="en-us" class="no-js ie8"> <![endif]--> 
<!--[if gt IE 8]><!--> <html lang="en-us" class="no-js"> <!--<![endif]--> 
Peder answered 11/7, 2011 at 18:6 Comment(12)
thanks @meder - but I want to try and use direct CSS instead :) i.e. because You cannot use condcoms in the CSS code itselfThigmotaxis
@Tim: The problem with the hacks you describe above is that they are bugs. There were never intended to be features. By using them, you may cause problems with other browsers with similar bugs. Stick to standards.Replay
@Thigmotaxis - it's not recommended to rely on hacks because with future iterations of the browser, those hacks could stab you in the back. If you adopt the html conditional as depicted and make one for IE9, in your main css you can do .ie9 body { } for example..Peder
@meder @andrew - yeah I suspected as much. just noted that Google was using such hacks in Google+ [wow, yes Google] - so assumed if Google engineers use them ...Thigmotaxis
Google applications don't usually have the best front end code.Peder
@Tim: Google doesn't exactly have great code review practices outside their core products... And not all software dev/engineer is equal.Replay
@andrew @meder :) you've won me over ... I'll implement a JS solution instead :) [ no fun really :D ]Thigmotaxis
Google uses hacks on purpose for the purpose of saving characters. Unless you're google and 1 extra byte translates to lots of money you should be writing good code. Google+ is a core product.Ammamaria
@Tim: Sigh... Again, a JavaScript solution also have problems. Use a condcom. It's standard compliant and works just fine.Replay
@andrew - ok ok :) was going to use quirksmode script as I need to add style elements but don't have acccess to the <head> for what i needThigmotaxis
You can put a style tag anywhere.Ammamaria
These CSS hacks do not interfere with other browsers, I have used them for years with no problems whatsoever. Yes it is preferable to use condcoms, but what if you don't have control of the HTML, only the CSS? In this case it is impossible to use a condcom if one isn't already being used and a CSS hack may be your only optionStanzel
A
4

IE9 is pretty standards compliant. You shouldn't need to hack it.

Also, you should be using IE conditional comments to load different styles. For IE 9 you would do:

<!--[if IE 9]>
    <!-- conditional content goes here -->
<![endif]-->
Ammamaria answered 11/7, 2011 at 18:7 Comment(3)
IE9 is pretty standards compliant. But THAT standards compliant. Once in a while we still get different interpretations IE 9 vs Good Browsers - so, yes, having those conditionals for IE 9 are quite nice. :)Cataldo
IE9 is the most standard-compliant thing to come out of MS sure, but it's still way behind any other browser.Stanzel
@danwellman: alas, IE9 has quite a few rendering bugs (e.g. box-shadow) which require the odd tweak in the CSS. Whilst a CC is the right way to implement these tweaks, they're often a bit heavy handed and can make it harder to maintain your CSS, e.g. needing another stylesheet, or at least to define additional CSS selectors.Languishing
L
4

At this adress : http://www.impressivewebs.com/ie10-css-hacks/ I found a media query specific for IE10 only (and below) :

@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {  
   /* IE10-specific styles go here */  
}
Lai answered 28/5, 2013 at 16:0 Comment(5)
it will affect ie11 also ?Blinny
This doesn't work for IE9 anymore. I have no idea why.Extrajudicial
@Extrajudicial #6418639 Check the firts answer to activate media querys on IE9 with <meta http-equiv="X-UA-Compatible" content="IE=9">Lai
This actually worked for me again, I found when running on localhost it would fail. But once pushed onto my server, IE9 would pick it up as expected. Weird.Extrajudicial
I think it may come from a conflict with the headers pushed by your HTTP server (Apache / Nginx / IIS...). Check if your config files for the project on dev, test and prod environement are the same.Lai
O
3

As noted in some of the comments, there are times when conditional HTML won't work for a specific situation, especially if you're unable to modify the page code itself. So here's a workaround:

Base Style

.test{color:red;}

Browser-Specific Overrides

IE < 8: html >/**/body .test { color: green; }
IE 9: :root .test{color:green \ ;}
IE 8 and 9: .test{color:green \ ;}
IE 9 and Opera :root .test {color: green\0;}

Notes

The above won't work for background or font-*, and any \0 or \9 hacks are generally unstable. For a complete list of CSS hacks, see http://mynthon.net/howto/-/webdev/CSS-big-list-of-css-hacks.txt.

Original answered 23/9, 2015 at 21:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.