How do I target only Internet Explorer 10 for certain situations like Internet Explorer-specific CSS or Internet Explorer-specific JavaScript code?
I tried this, but
I've written a small, vanilla JavaScript plugin called Layout Engine, which allows you to feature detect IE 10 (and every other browser), in a simple way that cannot be faked, unlike user agent sniffing.
It adds the rendering engine name as a class on the html tag and returns a JavaScript object containing the vendor and version (where appropriate)
Check out my blog post: http://mattstow.com/layout-engine.html and get the code on GitHub: https://github.com/stowball/Layout-Engine
If you don't mind targeting of IE10 and above and non IE browsers, you can use this conditional comment:
<!--[if gt IE 9]><!--> Your code here. <!--<![endif]-->
Derived from http://www.paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither
I wouldn't use JavaScript navigator.userAgent
or $.browser (which uses navigator.userAgent
) since it can be spoofed.
To target Internet Explorer 9, 10 and 11 (Note: also the latest Chrome):
@media screen and (min-width:0\0) {
/* Enter CSS here */
}
To target Internet Explorer 10:
@media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
/* IE10+ CSS here */
}
To target Edge Browser:
@supports (-ms-accelerator:true) {
.selector { property:value; }
}
Sources:
This answer got me 90% of the way there. I found the rest of my answer on the Microsoft site here.
The code below is what I'm using to target all ie by adding a .ie class to <html>
Use jQuery (which deprecated .browser in favor of user agents in 1.9+, see http://api.jquery.com/jQuery.browser/) to add an .ie class:
// ie identifier
$(document).ready(function () {
if (navigator.appName == 'Microsoft Internet Explorer') {
$("html").addClass("ie");
}
});
You can use PHP to add a stylesheet for IE 10
Like:
if (stripos($_SERVER['HTTP_USER_AGENT'], 'MSIE 10')) {
<link rel="stylesheet" type="text/css" href="../ie10.css" />
}
If you really have to, you can make conditional comments work by adding the following line to <head>
:
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9">
Source