27

For Internet Explorer 8 (IE8) I'd like to use the following CSS:

color : green;

I would like to apply a hack which only affects IE8, not to IE9, IE6 and 7.

Paul Lammertsma
  • 35,234
  • 14
  • 128
  • 182
Jitendra Vyas
  • 134,556
  • 218
  • 544
  • 822

11 Answers11

44

Use conditional comments in HTML, like this:

<!--[if IE 8]>
<style>...</style>
<![endif]-->

See here: http://www.quirksmode.org/css/condcom.html

You can test for IE versions reliably and also be sure other browsers won't be confused.

Palantir
  • 22,691
  • 9
  • 74
  • 84
  • 9
    I need hack not conditional – Jitendra Vyas Jun 15 '10 at 06:57
  • 3
    Why do you need a hack? Hacks, by their very nature, should be avoided (except as an absolute last resort). – Dean Harding Jun 15 '10 at 07:04
  • You should be able to go a long way by using conditionals (which are a hack themselves, by the way), as they allow you to have a specific code section executed only if a certain version is detected, and that could contain CSS, HTML, JS, ... – Palantir Jun 15 '10 at 07:06
  • 3
    @Palantir: You're right that conditional comments are a kind of hack, but at least they're "officially" supported and can be targeted directly at a specific version as required, rather than just relying on quirks of the engine to interpret them in different ways. – Dean Harding Jun 15 '10 at 07:08
  • Well, lets consider this purely from an academic pov - is there a hack for IE8 only? – James Westgate Jun 15 '10 at 07:50
  • 1
    Another thing to (slightly) strengthen the argument for a hack is what if there is already a hack for ie7, above a hack for ie6. You would want to keep the hacks together, rather than seperating the ie8 hack away in another place in the code. – James Westgate Jun 15 '10 at 07:51
  • 2
    One reason to use hacks is when you are working in an environment (XML/XSL and stuff) that happens to remove all comments from files before they get passed to the browser. – Juha Palomäki Mar 20 '13 at 20:03
35

Use media queries to separate each browser:

/* IE6/7 uses media, */
@media, { 
        .dude { color: green; } 
        .gal { color: red; }
} 

/* IE8 uses \0 */
@media all\0 { 
        .dude { color: brown; } 
        .gal { color: orange; }
} 

/* IE9 uses \9 */
@media all and (monochrome:0) { 
          .dude { color: yellow\9; } 
          .gal { color: blue\9; }
} 

/* IE10 and IE11 both use -ms-high-contrast */
@media all and (-ms-high-contrast:none)
 {
 .foo { color: green } /* IE10 */
 *::-ms-backdrop, .foo { color: red } /* IE11 */
 }

References

Paul Sweatte
  • 22,871
  • 7
  • 116
  • 244
  • 5
    your ie8 solution is not a IE8-only hack, i try it on my computer, ie9 and ie10 will also inherit this style. Do you have an IE8 and below hack? – Jack Zhang Apr 03 '14 at 05:44
  • 3
    @JackZhang Yes, use the IE9, IE10, IE11, etc hacks to reset the style rules set by the IE8 hack. I've updated the answer with those hacks. – Paul Sweatte Apr 03 '14 at 19:17
  • 2
    the ie8 solution still not work for IE8-only, '\0' will work for ie8, ie9 and ie10. Actually there no hack method for ie8-only style except conditional css. – Jack Zhang Apr 05 '14 at 11:24
  • The essence of the CSS reset in the answer uses the cascade. The cascade allows implicit overriding of rules based on source order. This is the nature of CSS, and is both backwards-compatible and forwards-compatible. – Paul Sweatte Jun 19 '14 at 00:38
  • It's a bit of necromancy, but.... your IE10/11 hack doesn't quite work. At all. 10 uses what you labeled as 9 (as will 9), 11 will use 8's. Additionally your comments should really reflect that IE8's applies to 8-11, rather than sweeping that fact under the cascade. – abluejelly Apr 04 '16 at 18:06
15

Use \0.

color: green\0;

I however do recommend conditional comments since you'd like to exclude IE9 as well and it's yet unpredictable whether this hack will affect IE9 as well or not.

Regardless, I've never had the need for an IE8 specific hack. What is it, the IE8 specific problem which you'd like to solve? Is it rendering in IE8 standards mode anyway? Its renderer is pretty good.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • 9
    Yes but don't forget the **white space** right before `\0` otherwise it'll also affect IE9. – ed1nh0 Jan 24 '13 at 13:43
8

If needed small changes in CSS use \9 it targets IE8 and below (IE6, IE7, IE8)

.element { 
   color:green \9;
 }

Best way is to use conditional comments in HTML, like this:

<!--[if IE 8]>
<style>...</style>
<![endif]-->
Žiga
  • 188
  • 3
  • 7
  • Tried it with property `content` and it won't budge under IE8 (haven't checked other IE versions). I'm guessing this is because we pass a quoted string value and this hack (and perhaps other hacks too) may not be compatible with it. `content: "\0020" \9;` – adi518 Aug 07 '16 at 15:52
2

So a recent question prompted me to notice a selector set hack for excluding IE 8 only.

.selector, #excludeIE8::before {} will cause IE 8 to throw out the entire selector set, while 5-7 and 9-11 will read it just fine. Any of the :: selectors (::first-line, ::before, ::first-letter, ::selection) will work, I've merely chosen ::before so the line reads accurately. Note that the goal of the fake ::before selector is to be fake, so be sure to change it to something else if you actually have an element with the ID excludeIE8

Interestingly enough, in modern browsers (FF 45-52, GC 49-57, Edge 25/13) a bad :: selector eats the entire selector set (dabblet demo). It seems that the last Windows version of Safari (and LTE IE 7, lol) doesn't have this behavior while still understanding ::before. Additionally, I can't find anything in the spec to indicate that this is intended behavior, and since it would cause breakage on any selector set containing: ::future-legitimate-pseudoelement... I'm inclined to say this is a bug- and one that'll nibble our rears in the future.


However, if you only want something at the property level (rather than the rule level), Ziga above had the best solution via appending  \9 (the space is key; do NOT copypaste that inline as it uses an nbsp):

/*property-level hacks:*/
/*Standards, Edge*/
prop:val;
/*lte ie 11*/
prop:val\9;
/*lte ie 8*/
prop:val \9;
/*lte ie 7*/
*prop:val;
/*lte ie 6*/
_prop:val;

/*other direction...*/
/*gte ie 8, NOT Edge*/
prop:val\0;

Side note, I feel like a dirty necromancer- but I wanted somewhere to document the exclude-IE8-only selector set hack I found today, and this seemed to be the most fitting place.

abluejelly
  • 1,135
  • 7
  • 16
2

Doing something like this should work for you.

<!--[if IE 8]> 
<div id="IE8">
<![endif]--> 

*Your content* 

<!--[if IE 8]> 
</div>
<![endif]--> 

Then your CSS can look like this:

#IE8 div.something
{ 
    color: purple; 
}
John Fisher
  • 21,209
  • 2
  • 34
  • 60
0

There are various ways to get a class onto the HTML element, identifying which IE version you're contending with: Modernizr, the HTML 5 Boilerplate, etc - or just roll your own. Then you can use that class (eg .lt-ie9) in a normal CSS selector, no hack needed. If you only want to affect IE8 and not previous versions, put the old value back using a .lt-ie8 selector.

thepeer
  • 7,981
  • 2
  • 16
  • 13
0

Can you not use the hack you've already shown, and then use an IE7 and below hack to override it?

Damien_The_Unbeliever
  • 220,246
  • 21
  • 302
  • 402
  • 4
    *Pleeeeease* don't use hacks! Hacks to override *other* hacks is even worse! – Dean Harding Jun 15 '10 at 07:03
  • 1
    @codeka - MGS has already insisted in a comment to another answer that he will only use hacks, so I was just going with the flow. If it was me, I'd go with conditionals, as Plantir already answered. – Damien_The_Unbeliever Jun 15 '10 at 07:05
0

For IE8 native browser alone:

.classname{
    *color: green; /* This is for IE8 Native browser alone */
}
Vel Murugan S
  • 540
  • 2
  • 12
  • 28
-1

I was looking for a good option for IE10 and below CSS styling (but it would work for IE8 only as well) and I came up with this idea:

<!--[if lt IE 10]><div class="column IE10-fix"><![endif]-->
<!--[if !lt IE 10]><!--><div class="column"><!--<![endif]-->

I declare my div or whatever you want with an extra class, this allows me to have extra CSS in the same stylesheet in a very readable manner.

It's W3C valid and not a weird CSS hack.

Sunkhern
  • 228
  • 4
  • 18
-2

This will work for Bellow IE8 Versions

.lt-ie9 #yourID{

your css code

}

Ravi Chothe
  • 180
  • 4
  • 17