Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting IE11 using CSS Capability/Feature Detection

IE10+ no longer supports browser detection tags to identify a browser.

For detecting IE10 I am using JavaScript and a capability-testing technique to detect certain ms prefixed styles are defined such as msTouchAction and msWrapFlow.

I want to do the same for IE11, but I am assuming that all the IE10 styles will be supported in IE11 as well. Can anyone help me identify IE11 only styles or capabilities that I could use to tell the two apart?

Extra Info

  • I don't want to use User Agent type detection because it's so spotty, and can be changed, as well as I think I've read that IE11 is intentionally trying to hide the fact it's Internet Explorer.
  • For an example of how the IE10 capability testing works, I used this JsFiddle (not mine) as a basis for my testing.
  • Also I am expecting a lot of answers of "This is a bad idea...". One of my needs for this is that IE10 claims it supports something, but it is very badly implemented, and I want to be able to differentiate between IE10 and IE11+ so I can move on with a capability-based detection method in the future.
  • This test is coupled with a Modernizr test that will simply make some functionality "fallback" to less glamorous behavior. We are not talking about critical functionality.

ALSO I am already using Modernizr, but it doesn't help here. I need help for a solution to my clearly asked question please.

like image 819
dano Avatar asked Sep 20 '13 00:09

dano


People also ask

Can CSS detect browser?

The closest you can come with pure CSS is with feature queries. Instead of detecting the browser type/version, it allows you to check if a specific property/value combinations are supported by the browser. And, of course, IE does not support "supports," which I need to use solely for IE.

How do I set IE to specific CSS?

#2 CSS Rules Specific to Explorer (IE CSS hacks) IE8 or below: to write CSS rules specificially to IE8 or below, add a backslash and 9 ( \9 ) at the end before the semicolon. IE7 or below: add an asterisk ( * ) before the CSS property. IE6: add an underscore ( _ ) before the property.

How to determine browser from user agent?

To detect user browser information we use the navigator. userAgent property. And then we match with the browser name to identify the user browser. Now call this JS function on page load, and this will display the user browser name on page load.

What is the proper way to conduct feature detection?

There are two very important recommendations to keep in mind when using feature detection: Always test for standards first because browsers often support the newer standard as well as the legacy workaround.


2 Answers

In the light of the evolving thread, I have updated the below:

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 Only

.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) {     .ie910{property:value;} } 

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;} } 

The use of -ms-high-contrast means that MS Edge will not be targeted, as Edge does not support -ms-high-contrast.

IE 11

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

Javascript alternatives

Modernizr

Modernizr runs quickly on page load to detect features; it then creates a JavaScript object with the results, and adds classes to the html element

User agent selection

Javascript:

var b = document.documentElement;         b.setAttribute('data-useragent',  navigator.userAgent);         b.setAttribute('data-platform', navigator.platform );         b.className += ((!!('ontouchstart' in window) || !!('onmsgesturechange' in window))?' touch':''); 

Adds (e.g) the below to html element:

data-useragent='Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C)' data-platform='Win32' 

Allowing very targetted CSS selectors, e.g.:

html[data-useragent*='Chrome/13.0'] .nav{     background:url(img/radial_grad.png) center bottom no-repeat; } 

Footnote

If possible, identify and fix any issue(s) without hacks. Support progressive enhancement and graceful degradation. However, this is an 'ideal world' scenario not always obtainable, as such- the above should help provide some good options.


Attribution / Essential Reading

  • Jeff Clayton | Browserhacks.com
  • Keith Clarke
  • Paul Irish
  • Web Devout
  • The Spanner
like image 194
SW4 Avatar answered Oct 12 '22 23:10

SW4


To target IE10 and IE11 only (and not Edge):

@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {      /* add your IE10-IE11 css here */    } 
like image 38
Apps Tawale Avatar answered Oct 12 '22 22:10

Apps Tawale