IE 10 特定样式

Avatar of Chris Coyier
Chris Coyier

DigitalOcean 为您旅程的每个阶段提供云产品。立即开始使用 200 美元的免费额度!

IE 10 中 不再使用 条件注释。这很好。IE 10 是一款非常优秀的浏览器。在几乎所有情况下,特性检测都是更好的选择。但是,如果您发现某些样式情况下绝对需要定位 IE 10 呢?我想您必须这样做。

Rogie 之前发布过 一个非常简单的想法,它现在仍然非常有效。使用少量的 JavaScript 将用户代理添加到 <html> 元素中

var doc = document.documentElement;
doc.setAttribute('data-useragent', navigator.userAgent);

IE 10 的用户代理字符串是

Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)

这将导致

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

然后您可以像这样设置样式

html[data-useragent*='MSIE 10.0'] h1 {
  color: blue;
}
Check out this Pen!