我想用符合W3C标准的CSS定位IE7和IE8。有时为一个版本修复CSS并不能解决另一个版本。我怎样才能做到这一点?
答案 0 :(得分:164)
明确定位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“Hacks”
定位IE版本更重要的是,这里有一些黑客可以让你定位IE版本。
使用“\ 9”定位IE8及以下。
使用“*”来定位IE7及以下。
使用“_”来定位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”类应用于<html>
元素
<!doctype html>
<html lang="en">
<!--[if !IE]><!--><script>if (/*@cc_on!@*/false) {document.documentElement.className+=' ie10';}</script><!--<![endif]-->
<head></head>
<body></body>
</html>
答案 1 :(得分:21)
我建议您查看有条件的评论,并为您遇到问题的IE制作单独的表格。
<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="ie7.css" />
<![endif]-->
答案 2 :(得分:9)
选择所有浏览器但IE8及以下版本的完全有效的方法是使用:not()
伪类。由于IE 8及更低版本不支持:not()
,因此忽略包含它的选择器。这意味着你可以这样做:
p {color:red;}
p:not([ie8min]) {color:blue;}
这仍然是完全有效的CSS,但它确实导致IE8和更低版本呈现不同的样式(以及Opera&lt; 9.5和Safari&lt; 3.2)。
以下是我能找到的所有完全有效的CSS浏览器特定选择器的列表,除了之外的一些似乎非常冗余,例如只选择1种类型的古代浏览器({3 },1):
/****** 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 */
答案 3 :(得分:7)
截至2015年的更完整清单:
* html .ie6 {property:value;}
或
.ie6 { _property:value;}
*+html .ie7 {property:value;}
或
*:first-child+html .ie7 {property:value;}
@media screen\9 {
.ie67 {property:value;}
}
或
.ie67 { *property:value;}
或
.ie67 { #property:value;}
@media \0screen\,screen\9 {
.ie678 {property:value;}
}
html>/**/body .ie8 {property:value;}
或
@media \0screen {
.ie8 {property:value;}
}
.ie8 { property /*\**/: value\9 }
@media screen\0 {
.ie8910 {property:value;}
}
@media screen and (min-width:0) and (min-resolution: .001dpcm) {
// IE9 CSS
.ie9{property:value;}
}
@media screen and (min-width:0) and (min-resolution: +72dpi) {
// IE9+ CSS
.ie9up{property:value;}
}
@media screen and (min-width:0) {
.ie910{property:value;}
}
_:-ms-lang(x), .ie10 { property:value\9; }
_:-ms-lang(x), .ie10up { property:value; }
或
@media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
.ie10up{property:value;}
}
_:-ms-fullscreen, :root .ie11up { property:value; }
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;
}
如果可能,请避免使用浏览器定位。识别并修复您识别的任何问题。支持progressive enhancement和graceful degradation。考虑到这一点,这是一个理想的世界&#39;场景并不总是在生产环境中可以获得,因此上述内容应该有助于提供一些好的选择。
答案 4 :(得分:4)
嗯,你真的不必担心IE7代码无法在IE8中运行,因为IE8具有兼容模式(它可以呈现与IE7相同的页面)。但是,如果你仍然希望定位不同版本的IE,那么现在已经做了一段时间的方法就是使用conditional comments或begin your css rule with a * to target IE7 and below。或者您可以关注服务器上的用户代理,并根据该信息提供不同的CSS文件。
答案 5 :(得分:4)
实际问题不是IE8,而是用于早期版本IE的黑客攻击。
IE8非常接近符合标准,所以你根本不需要任何黑客攻击,也许只需要一些调整。问题是如果你使用IE6和IE7的一些黑客攻击;你必须确保它们只适用于那些版本,而不是IE8。
我刚才让我们公司的网站与IE8兼容。我实际上唯一改变的是添加元标记,告诉IE该页面符合IE8 ......
答案 6 :(得分:1)
我是用Javascript做的。我在html元素中添加了三个css类:
ie<version>
lte-ie<version>
lt-ie<version + 1>
因此,对于IE7,它添加了ie7
,lte-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所述。
(在Check if user is using IE with jQuery中使用了Mario的detectIE功能)
拥有lte-ie8和lt-ie8等的好处是它可以将所有浏览器定位到小于或等于IE9,即IE7 - IE9。