如何为IE 11编写CSS hack? [重复]

问题描述 投票:167回答:8

这个问题在这里已有答案:

我怎样才能为IE 11破解或编写CSS?我有一个在IE 11中看起来很糟糕的网站。我只是在这里搜索,但还没有找到任何解决方案。

有没有css选择器?

css css-selectors internet-explorer-11 css-hack
8个回答
252
投票

使用Microsoft特定CSS规则的组合来过滤IE11:

<!doctype html>
<html>
 <head>
  <title>IE10/11 Media Query Test</title>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <style>
    @media all and (-ms-high-contrast:none)
     {
     .foo { color: green } /* IE10 */
     *::-ms-backdrop, .foo { color: red } /* IE11 */
     }
  </style>
 </head>
 <body>
  <div class="foo">Hi There!!!</div>
 </body>
</html>

由于以下因素,此工作的过滤器:

当用户代理无法解析选择器时(即,它不是有效的CSS 2.1),它也必须忽略选择器和以下声明块(如果有的话)。

<!doctype html>
<html>
 <head>
  <title>IE10/11 Media Query Test</title>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <style>
    @media all and (-ms-high-contrast:none)
     {
     .foo { color: green } /* IE10 */
     *::-ms-backdrop, .foo { color: red } /* IE11 */
     }
  </style>
 </head>
 <body>
  <div class="foo">Hi There!!!</div>
 </body>
</html>

参考


207
投票

根据不断发展的主题,我更新了以下内容:

IE 11(及以上......)

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

IE 10及以上版本

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

要么

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

仅限IE 10

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

IE 9及以上版本

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

IE 9和10

@media screen and (min-width:0\0) {
    .foo /* backslash-9 removes.foo & old Safari 4 */
}

仅限IE 9

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

IE 8,9和10

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

仅限IE 8标准模式

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

IE 8

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

要么

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

IE 7

*+html .foo {property:value;}

要么

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

IE 6,7和8

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

IE 6和7

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

要么

.foo { *property:value;}

要么

.foo { #property:value;}

IE 6,7和8

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

IE 6

* html .foo {property:value;}

要么

.foo { _property:value;}

Javascript替代品

Modernizr

Modernizr在页面加载时快速运行以检测功能;然后它创建一个包含结果的JavaScript对象,并将类添加到html元素

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':'');

html元素中添加(例如)以下内容:

data-useragent='Mozilla/5.0 (compatible; M.foo 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'

允许非常有针对性的CSS选择器,例如:

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

脚注

如果可能,请确定并修复任何问题,而不是黑客攻击。支持progressive enhancementgraceful degradation。然而,这是一个并非总能获得的“理想世界”场景,因此上述内容应该有助于提供一些好的选择。


Attribution / Essential Reading


62
投票

这里有一个两步解决方案是IE10和11的黑客攻击

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

因为IE10和IE11支持-ms-高对比度你可以利用这个来定位这两个浏览器

如果你想从中排除IE10,你必须创建一个IE10特定代码,如下所示使用用户代理技巧你必须添加这个Javascript

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

和这个HTML标记

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

现在你可以像这样写你的CSS代码了

html[data-useragent*='MSIE 10.0'] h1 {
  color: blue;
}

欲了解更多信息,请参阅这个网站,wil tutorailChris Tutorial


如果你想要IE11及更高版本的目标,我发现这是:

_:-ms-fullscreen, :root .selector {}

Here is a great resource for getting more information: browserhacks.com


23
投票

自从2013年秋天以来,我一直在写这些并将它们贡献给BrowserHacks.com - 我写的这篇文章很简单,只有IE 11支持。

<style type="text/css">
_:-ms-fullscreen, :root .msie11 { color: blue; }
</style>

当然还有div ...

<div class="msie11">
    This is an Internet Explorer 11 CSS Hack
<div>

因此,文本显示为蓝色与Internet Explorer 11。玩得开心。

-

我的实时测试网站上有更多IE浏览器和其他浏览器CSS攻击:

更新:http://browserstrangeness.bitbucket.io/css_hacks.html

镜子:http://browserstrangeness.github.io/css_hacks.html

(如果您还在寻找MS Edge CSS Hacks,那就去哪里。)


6
投票

您可以在样式标记内使用以下代码:

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

以下是一个对我有用的示例:

<style type="text/css">

@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {  
   /* IE10+ specific styles go here */  
   #flashvideo {
        width:320px;
        height:240;
        margin:-240px 0 0 350px;
        float:left;
    }

    #googleMap {
        width:320px;
        height:240;
        margin:-515px 0 0 350px;
        float:left;
        border-color:#000000;
    }
}

#nav li {
    list-style:none;
    width:240px;
    height:25px;
    }

#nav a {
    display:block;
    text-indent:-5000px;
    height:25px;
    width:240px;
    }
</style>

请注意,因为(#nav li)和(#nav a)在@media屏幕之外...,它们是通用样式。


1
投票

您可以使用js并在html中添加一个类来维护conditional comments的标准:

  var ua = navigator.userAgent,
      doc = document.documentElement;

  if ((ua.match(/MSIE 10.0/i))) {
    doc.className = doc.className + " ie10";

  } else if((ua.match(/rv:11.0/i))){
    doc.className = doc.className + " ie11";
  }

或者像bowser一样使用lib:

https://github.com/ded/bowser

或用于特征检测的现代化:

http://modernizr.com/


1
投票

所以我最终找到了解决这个问题的方法。

在通过Microsoft documentation搜索后,我设法找到了一个新的IE11风格msTextCombineHorizontal

在我的测试中,我检查IE10样式,如果它们是正面匹配,那么我检查IE11的样式。如果我找到它,那么它是IE11 +,如果我没有,那么它是IE10。

代码示例:Detect IE10 and IE11 by CSS Capability Testing (JSFiddle)

当我发现它们时,我会用更多样式更新代码示例。

注意:这几乎肯定会将IE12和IE13识别为“IE11”,因为这些样式可能会发扬光大。随着新版本的推出,我将进一步增加测试,希望能够再次依赖Modernizr。

我正在使用此测试进行回退行为。后备行为只是不那么迷人的样式,它没有减少功能。


0
投票

我觉得这很有帮助

<?php if (strpos($_SERVER['HTTP_USER_AGENT'], 'Trident/7.0; rv:11.0') !== false) { ?>
    <script>
    $(function(){
        $('html').addClass('ie11');
    });
    </script>
<?php } ?>

<head>文档下添加此内容

© www.soinside.com 2019 - 2024. All rights reserved.