224

I have an inherited project and there are places where it's an utter mess. This is one of them. I need to target only IE (any version).

#nav li {
    float: left;
    height: 54px;
    background: #4f5151;
    display: table;
    border-left: 1px solid grey;
}

To be clear: Inside the embedded stylesheet and without adding ID's or classes to the tags in the html, I need to apply the border style only if the user is using IE. How can I do this?

Edit: found a solution for Firefox, editing question to reflect this.

MetalPhoenix
  • 3,440
  • 6
  • 26
  • 51
  • You're question is a little confusing. Are you referring to vendor prefixes for CSS properties or are you referring to identifying a user's browser through UA sniffing and then applying the stylesheet only if it matches?... – War10ck Feb 09 '15 at 18:51
  • For Fireofox: http://stackoverflow.com/questions/952861/targeting-only-firefox-with-css – nikoskip Feb 09 '15 at 18:51
  • To target IE you have to modify your HTML file and add conditional comments, for IE10 you will also need some Javascript because it comes with 0 support for conditional comments. **EDIT** there are some CSS hacks to target some versions of IE, but that's also the problem - those are hacks. – Ramiz Wachtler Feb 09 '15 at 18:53
  • @War10ck: this is entirely within the embedded stylesheet. CSS only. – MetalPhoenix Feb 09 '15 at 18:54
  • 1
    possible duplicate of [Apply style ONLY on IE](http://stackoverflow.com/questions/11173106/apply-style-only-on-ie) – MatthewG Feb 09 '15 at 19:01
  • 1
    If you need a solution inside your CSS, I only can think in JavaScript. I found this http://rafael.adm.br/css_browser_selector/ but it's a little outdated. – nikoskip Feb 09 '15 at 19:03
  • What was the solution for Firefox? I'm intrigued because I can't think of a way to possible due this without JavaScript or without editing the HTML source... – War10ck Feb 09 '15 at 19:13
  • That @-moz-document hack works but I had been coding it wrong. O.o – MetalPhoenix Feb 09 '15 at 19:29
  • @MatthewG: not exactly since I need a .css embedded stylesheet solution.However, it's close enough that I'm considering whether or not to delete this question on that basis. – MetalPhoenix Feb 09 '15 at 19:31

6 Answers6

487

Internet Explorer 9 and lower : You could use conditional comments to load an IE-specific stylesheet for any version (or combination of versions) that you wanted to specifically target.like below using external stylesheet.

<!--[if IE]>
  <link rel="stylesheet" type="text/css" href="all-ie-only.css" />
<![endif]-->

However, beginning in version 10, conditional comments are no longer supported in IE.

Internet Explorer 10 & 11 : Create a media query using -ms-high-contrast, in which you place your IE 10 and 11-specific CSS styles. Because -ms-high-contrast is Microsoft-specific (and only available in IE 10+), it will only be parsed in Internet Explorer 10 and greater.

@media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
     /* IE10+ CSS styles go here */
}

Microsoft Edge 12 : Can use the @supports rule Here is a link with all the info about this rule

@supports (-ms-accelerator:true) {
  /* IE Edge 12+ CSS styles go here */ 
}

Inline rule IE8 detection

I have 1 more option but it is only detect IE8 and below version.

  /* For IE css hack */
  margin-top: 10px\9 /* apply to all ie from 8 and below */
  *margin-top:10px;  /* apply to ie 7 and below */
  _margin-top:10px; /* apply to ie 6 and below */

As you specefied for embeded stylesheet. I think you need to use media query and condition comment for below version.

Community
  • 1
  • 1
Jay Patel
  • 5,768
  • 1
  • 16
  • 20
  • 16
    Good enough, I tested that this fix does not affect Edge browser, JIC someone wondered. – j4v1 Jan 28 '16 at 14:45
  • also needs @supports (-ms-accelerator:auto) for edge see below – Phyllis Sutherland Jun 28 '17 at 19:11
  • 7
    For Edge, using @supports (-ms-ime-align:auto) instead of -ms-accelerator works for me – SeventhSteel Sep 23 '17 at 14:25
  • `-ms-high-contrast:active` affects Edge if the the system is in using high-contrast mode. – ShortFuse May 07 '19 at 12:00
  • The `@supports` solution is really great: feature detection is the way to go. I was willing to target Edge due to its lack of support of `width: max-content`: `@supports not (width: max-content)` does it neatly, and will be nicely ignored when Edge ends up supporting it. (It should happen in 2019 Fall, since it should then switch to Chromium for rendering.) – Frédéric Jun 30 '19 at 10:09
96

Here's a collection of media queries that will allow you to do that for any version of Internet Explorer (from IE6 to IE11+), Firefox, Chrome & Safari (EDIT: also added Opera).

IE 6

* html .ie6 { property: value; }

or

.ie6 { _property: value; }

IE 7

*+html .ie7 { property: value; }

or

*:first-child+html .ie7 { property: value; }

IE 6 and 7

@media screen\9 { 
    .ie67 {
        property: value; 
    }
}

or

.ie67 { *property: value; }

or

.ie67 { #property: value; }

IE 6, 7 and 8

@media \0screen\,screen\9 {
    .ie678 {
        property: value;
    }
}

IE 8

html>/**/body .ie8 { property: value; }

or

@media \0screen {
    .ie8 {
        property: value;
    }
}

IE 8 Standards Mode

.ie8 { property /*\**/: value\9 }

IE 8,9 and 10

@media screen\0 {
    .ie8910 {
        property: value;
    }
}

IE 9 only

@media screen and (min-width:0\0) and (min-resolution: .001dpcm) { 
    // IE9 CSS
    .ie9{
        property: value;
    }
}

IE 9 and above

@media screen and (min-width:0\0) and (min-resolution: +72dpi) {
    // IE9+ CSS
    .ie9up { 
        property: value; 
    }
}

IE 9 and 10

@media screen and (min-width:0\0) {
    .ie910 {
        property: value\9;
    } /* backslash-9 removes ie11+ & old Safari 4 */
}

IE 10 only

_:-ms-lang(x), .ie10 { property: value\9; }

IE 10 and above

_:-ms-lang(x), .ie10up { property: value; }

or

@media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
    .ie10up {
        property:value;
    }
}

IE 11 (and above..)

_:-ms-fullscreen, :root .ie11up { property: value; }

Firefox (any version)

@-moz-document url-prefix() {
    .ff {
        color: red;
    }
}

Firefox (Quantum Only / Stylo)

@-moz-document url-prefix() {
    @supports (animation: calc(0s)) {
        /* Stylo */
        .ffStylo {
            property: value;
        }
    }
}

Firefox Legacy (pre-Stylo)

@-moz-document url-prefix() {
    @supports not (animation: calc(0s)) {
        /* Gecko */
        .ffGecko {
            property: value;
        }
    }
}

Webkit (Chrome & Safari, any version)

@media screen and (-webkit-min-device-pixel-ratio:0) { 
    property: value;
}

Google Chrome (29+)

@media screen and (-webkit-min-device-pixel-ratio:0) and (min-resolution:.001dpcm) {
    .chrome {
        property: value;
    }
}

Safari (7.1+)

_::-webkit-full-page-media, _:future, :root .safari_only {
    property: value;
}

Safari (from 6.1 to 10.0)

@media screen and (min-color-index:0) and(-webkit-min-device-pixel-ratio:0) { 
    @media {
        .safari6 { 
            color:#0000FF; 
            background-color:#CCCCCC; 
        }
    }
}

Safari (10.1+)

@media not all and (min-resolution:.001dpcm) { 
    @media {
        .safari10 { 
            color:#0000FF; 
            background-color:#CCCCCC; 
        }
    }
}

Opera (12+)

@media (min-resolution: .001dpcm) {
    _:-o-prefocus, .selector {
        .opera12 {
            color:#0000FF; 
            background-color:#CCCCCC; 
        }
    } 
}

Opera (11 and lower)

@media all and (-webkit-min-device-pixel-ratio:10000), not all and (-webkit-min-device-pixel-ratio:0) {
    .opera11 {
        color:#0000FF; 
        background-color:#CCCCCC; 
    }
}

For further info or additional media queries, visit the browserhacks.com web site and/or check out this blog post that I wrote on this topic.

Darkseal
  • 9,334
  • 8
  • 75
  • 104
  • 1
    September 2018 and you still save lives! Many thanks. But what about Opera (old versions)? Just Webkit? – The girl with red hair Sep 28 '18 at 19:36
  • 1
    @Thegirlwithredhair there are a couple selector hacks to target Opera >= 9, Opera <= 9 and Opera <= 11 here: http://browserhacks.com/#op – Darkseal Sep 29 '18 at 17:57
  • 1
    @Thegirlwithredhair I added two media queries that can be used to target Opera >= 12 and Opera <= 11 to my answer above. – Darkseal Sep 29 '18 at 18:03
12

When using SASS I use the following 2 @media queries to target IE 6-10 & EDGE.

@media screen\9
    @import ie_styles
@media screen\0
    @import ie_styles

http://keithclark.co.uk/articles/moving-ie-specific-css-into-media-blocks/

Edit

I also target later versions of EDGE using @support queries (add as many as you need)

@supports (-ms-ime-align:auto)
    @import ie_styles
@supports (-ms-accelerator:auto)
    @import ie_styles

https://jeffclayton.wordpress.com/2015/04/07/css-hacks-for-windows-10-and-spartan-browser-preview/

minlare
  • 2,098
  • 21
  • 44
8

For targeting IE only in my stylesheets, I use this Sass Mixin :

@mixin ie-only {
  @media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
    @content;
  }
}
2

After experiencing issues with sites breaking on Edge when using High Contrast Mode, I came across the following work by Jeff Clayton:

https://browserstrangeness.github.io/css_hacks.html

It's a crazy, weird media query, but those are easier to use in Sass:

@media screen and (min-width:0\0) and (min-resolution:+72dpi), \0screen\,screen\9 {
   .selector { rule: value };
}

This targets IE versions expect for IE8.

Or you can use:

@media screen\0 {
  .selector { rule: value };
}

Which targets IE8-11, but also triggers FireFox 1.x (which for my use case, doesn't matter).

Right now I'm testing with print support, and this seems to be working okay:

@media all\0 {
  .selector { rule: value };
}
ShortFuse
  • 5,131
  • 3
  • 32
  • 33
1

Another working solution for IE specific styling is

<html data-useragent="Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)">

And then your selector

html[data-useragent*='MSIE 10.0'] body .my-class{
        margin-left: -0.4em;
    }
Sahib Khan
  • 497
  • 4
  • 17