我想使用符合 W3C 的 CSS 来定位 IE7 和 IE8。有时修复一个版本的 CSS 并不能修复另一个版本的 CSS。我怎样才能实现这个目标?

有帮助吗?

解决方案

明确定位IE版本,不使用HTML和CSS

如果您不想在CSS中使用黑客,请使用此方法。将一个浏览器唯一的类添加到<html>元素中,以便稍后基于浏览器进行选择。

实施例

<!doctype html>
<!--[if IE]><![endif]-->
<!--[if lt IE 7 ]> <html lang="en" class="ie6">    <![endif]-->
<!--[if IE 7 ]>    <html lang="en" class="ie7">    <![endif]-->
<!--[if IE 8 ]>    <html lang="en" class="ie8">    <![endif]-->
<!--[if IE 9 ]>    <html lang="en" class="ie9">    <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--><html lang="en"><!--<![endif]-->
    <head></head>
    <body></body>
</html>

然后在您的CSS中,您可以非常严格地访问目标浏览器。

实施例

.ie6 body { 
    border:1px solid red;
}
.ie7 body { 
    border:1px solid blue;
}

有关详细信息,请查看 http://html5boilerplate.com/

使用CSS定位IE版本<!>;黑客<!>;

更重要的是,这里有一些黑客可以让你定位IE版本。

使用<!> \ n; \ 9 <!>;目标IE8及以下。
使用<!>“; * <!>”;目标IE7及以下。
使用<!> quot; _ <!> quot;以IE6为目标。

示例:

body { 
border:1px solid red; /* standard */
border:1px solid blue\9; /* IE8 and below */
*border:1px solid orange; /* IE7 and below */
_border:1px solid blue; /* IE6 */
}

更新:定位IE10

IE10无法识别条件语句,因此您可以使用它来应用<!>“ie10 <!>”;类到<=>元素

<!doctype html>
    <html lang="en">
    <!--[if !IE]><!--><script>if (/*@cc_on!@*/false) {document.documentElement.className+=' ie10';}</script><!--<![endif]-->
        <head></head>
        <body></body>
</html>

其他提示

我建议您查看有条件的评论,并为您遇到问题的IE制作单独的表格。

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

你的问题的答案

选择除 IE8 及更低版本之外的所有浏览器的完全有效的方法是使用 :not() 伪类。由于IE 8及以下版本不支持 :not(), ,包含它的选择器将被忽略。这意味着你可以这样做:

p {color:red;}
p:not([ie8min]) {color:blue;}

这仍然是完全有效的 CSS,但它确实会导致 IE8 及更低版本呈现不同的样式(以及 Opera<9.5 和 Safari<3.2)。

其他技巧

这是我能找到的所有完全有效的 CSS 浏览器特定选择器的列表, 除了 对于一些看起来相当多余的内容,例如仅选择一种古代浏览器的浏览器(1, 2):

/******  First the hacks that target certain specific browsers  ******/
* html p                        {color:red;} /* IE 6- */
*+html p                        {color:red;} /* IE 7 only */
@media screen and (-webkit-min-device-pixel-ratio:0) {
    p                           {color:red;}
}                                            /* Chrome, Safari 3+ */
p, body:-moz-any-link           {color:red;} /* Firefox 1+ */
:-webkit-any(html) p            {color:red;} /* Chrome 12+, Safari 5.1.3+ */
:-moz-any(html) p               {color:red;} /* Firefox 4+ */

/****** And then the hacks that target all but certain browsers ******/
html> body p                  {color:green;} /* not: IE<7 */
head~body p                   {color:green;} /* not: IE<7, Opera<9, Safari<3 */
html:first-child p            {color:green;} /* not: IE<7, Opera<9.5, Safari&Chrome<4, FF<3 */
html>/**/body p               {color:green;} /* not: IE<8 */
body:first-of-type p          {color:green;} /* not: IE<9, Opera<9, Safari<3, FF<3.5 */
:root p                       {color:green;} /* not: IE<9, Opera<9.5 */
body:not([oldbrowser]) p      {color:green;} /* not: IE<9, Opera<9.5, Safari<3.2 */

学分和来源:

有关 2015 年更完整的列表:

浏览器6

* html .ie6 {property:value;}

或者

.ie6 { _property:value;}

浏览器7

*+html .ie7 {property:value;}

或者

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

IE 6 和 7

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

或者

.ie67 { *property:value;}

或者

.ie67 { #property:value;}

IE 6、7 和 8

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

浏览器8

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

或者

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

仅 IE 8 标准模式

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

IE 8,9 和 10

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

仅限 IE 9

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

IE 9及以上版本

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

IE 9 和 10

@media screen and (min-width:0) {
    .ie910{property:value;}
}

仅限 IE 10

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

IE 10及以上

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

或者

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

IE 11(及更高版本..)

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

JavaScript 替代品

现代化

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

用户代理选择

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

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

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

脚注

如果可能,请避免以浏览器为目标。识别并解决您发现的任何问题。支持 渐进增强优雅降级. 。考虑到这一点,这是一个“理想世界”场景,并不总是在生产环境中实现,因此,上述内容应该有助于提供一些不错的选择。


归因/必读

嗯,你真的不必担心IE7代码无法在IE8中运行,因为IE8具有兼容模式(它可以呈现与IE7相同的页面)。但是如果你仍然想要针对不同版本的IE,那么现在已经做了一段时间的方法就是使用条件评论开始你的css规则a *以IE7及以下为目标。或者您可以关注服务器上的用户代理,并根据该信息提供不同的CSS文件。

实际问题不是IE8,而是用于早期版本IE的黑客攻击。

IE8非常接近符合标准,所以你根本不需要任何黑客攻击,也许只需要一些调整。问题是如果你使用IE6和IE7的一些黑客攻击;你必须确保它们只适用于那些版本,而不是IE8。

我刚才让我们公司的网站与IE8兼容。我实际上唯一改变的是添加元标记,告诉IE该页面符合IE8 ......

我是用Javascript做的。我在html元素中添加了三个css类:

ie<version>
lte-ie<version>
lt-ie<version + 1>

因此,对于IE7,它添加了ie7lte-ie7 ...,lt-ie8 ...

以下是javascript代码:

(function () {
    function getIEVersion() {
        var ua = window.navigator.userAgent;
        var msie = ua.indexOf('MSIE ');
        var trident = ua.indexOf('Trident/');

        if (msie > 0) {
            // IE 10 or older => return version number
            return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
        } else if (trident > 0) {
            // IE 11 (or newer) => return version number
            var rv = ua.indexOf('rv:');
            return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
        } else {
            return NaN;
        }
    };

    var ieVersion = getIEVersion();

    if (!isNaN(ieVersion)) { // if it is IE
        var minVersion = 6;
        var maxVersion = 13; // adjust this appropriately

        if (ieVersion >= minVersion && ieVersion <= maxVersion) {
            var htmlElem = document.getElementsByTagName('html').item(0);

            var addHtmlClass = function (className) { // define function to add class to 'html' element
                htmlElem.className += ' ' + className;
            };

            addHtmlClass('ie' + ieVersion); // add current version
            addHtmlClass('lte-ie' + ieVersion);

            if (ieVersion < maxVersion) {
                for (var i = ieVersion + 1; i <= maxVersion; ++i) {
                    addHtmlClass('lte-ie' + i);
                    addHtmlClass('lt-ie' + i);
                }
            }
        }
    }
})();

此后,您可以使用样式表中的.ie<version> css类,如potench所述。

(在检查用户是否使用IE与jQuery <使用Mario的detectIE功能

拥有lte-ie8和lt-ie8等的好处是它可以将所有浏览器定位到小于或等于IE9,即IE7 - IE9。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top