Was*_*ikh 65 css internet-explorer web-standards internet-explorer-8
我想用符合W3C标准的CSS来定位IE7和IE8.有时为一个版本修复CSS并不能解决另一个版本.我怎样才能做到这一点?
pot*_*nch 164
使用HTML和CSS明确地定位IE版本而不使用黑客
如果您不想在CSS中使用hacks,请使用此方法.向<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>
Run Code Online (Sandbox Code Playgroud)
然后在您的CSS中,您可以非常严格地访问目标浏览器.
例
.ie6 body {
border:1px solid red;
}
.ie7 body {
border:1px solid blue;
}
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请访问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 */
}
Run Code Online (Sandbox Code Playgroud)
更新:目标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>
Run Code Online (Sandbox Code Playgroud)
scr*_*gar 21
我建议查看条件注释,并为您遇到问题的IE制作单独的表格.
<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="ie7.css" />
<![endif]-->
Run Code Online (Sandbox Code Playgroud)
选择所有浏览器但IE8及以下版本的完全有效的方法是使用:not()伪类.由于IE 8及更低版本不支持:not(),因此忽略包含它的选择器.这意味着你可以这样做:
p {color:red;}
p:not([ie8min]) {color:blue;}
Run Code Online (Sandbox Code Playgroud)
这仍然是完全有效的CSS,但它确实导致IE8和更低版本呈现不同的样式(以及Opera <9.5和Safari <3.2).
这里的一切完全符合CSS特定浏览器的选择列表我能找到的,除了一些似乎相当多余的,比如那些只是1型古浏览器的选择(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 */
Run Code Online (Sandbox Code Playgroud)
:first-child;
:root;
:first-of-type;
:not();
:any();
* ~ *有关截至2015年的更完整列表:
* html .ie6 {property:value;}
Run Code Online (Sandbox Code Playgroud)
要么
.ie6 { _property:value;}
Run Code Online (Sandbox Code Playgroud)
*+html .ie7 {property:value;}
Run Code Online (Sandbox Code Playgroud)
要么
*:first-child+html .ie7 {property:value;}
Run Code Online (Sandbox Code Playgroud)
@media screen\9 {
.ie67 {property:value;}
}
Run Code Online (Sandbox Code Playgroud)
要么
.ie67 { *property:value;}
Run Code Online (Sandbox Code Playgroud)
要么
.ie67 { #property:value;}
Run Code Online (Sandbox Code Playgroud)
@media \0screen\,screen\9 {
.ie678 {property:value;}
}
Run Code Online (Sandbox Code Playgroud)
html>/**/body .ie8 {property:value;}
Run Code Online (Sandbox Code Playgroud)
要么
@media \0screen {
.ie8 {property:value;}
}
Run Code Online (Sandbox Code Playgroud)
.ie8 { property /*\**/: value\9 }
Run Code Online (Sandbox Code Playgroud)
@media screen\0 {
.ie8910 {property:value;}
}
Run Code Online (Sandbox Code Playgroud)
@media screen and (min-width:0) and (min-resolution: .001dpcm) {
// IE9 CSS
.ie9{property:value;}
}
Run Code Online (Sandbox Code Playgroud)
@media screen and (min-width:0) and (min-resolution: +72dpi) {
// IE9+ CSS
.ie9up{property:value;}
}
Run Code Online (Sandbox Code Playgroud)
@media screen and (min-width:0) {
.ie910{property:value;}
}
Run Code Online (Sandbox Code Playgroud)
_:-ms-lang(x), .ie10 { property:value\9; }
Run Code Online (Sandbox Code Playgroud)
_:-ms-lang(x), .ie10up { property:value; }
Run Code Online (Sandbox Code Playgroud)
要么
@media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
.ie10up{property:value;}
}
Run Code Online (Sandbox Code Playgroud)
_:-ms-fullscreen, :root .ie11up { property:value; }
Run Code Online (Sandbox Code Playgroud)
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':'');
Run Code Online (Sandbox Code Playgroud)
在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'
Run Code Online (Sandbox Code Playgroud)
允许非常有针对性的CSS选择器,例如:
html[data-useragent*='Chrome/13.0'] .nav{
background:url(img/radial_grad.png) center bottom no-repeat;
}
Run Code Online (Sandbox Code Playgroud)
如果可能,请避免浏览器定位.识别并修复您识别的任何问题.支持渐进增强和优雅降级.考虑到这一点,这是一个并非总能在生产环境中获得的"理想世界"场景,因此上述内容应该有助于提供一些好的选择.
| 归档时间: |
|
| 查看次数: |
138061 次 |
| 最近记录: |