兼容IE6/IE7/IE8/FireFox的css hack(转)

1 .color{
2 background-color: #CC00FF;
3 background-color: #FF0000\9;
4 *background-color: #0066FF;
5 _background-color: #009933;
6  }

**记住上面得样式解释为顺序是 ff、ie8、ie7、ie6 ** 

显示的结果: 

用火狐浏览,颜色是紫色 

用 IE8 浏览,颜色是红色 

用 IE7 浏览,颜色是蓝色 

用 IE6 浏览,颜色是绿色 

如果你只是为了兼容ie7和8,其实可以在<head>里加上这样一条代码: 

1 <meta http-equiv="X-UA-Compatible" content="IE=7">

即可免去你大量修改代码,但是我发现,有些后台程序员往往会把它删掉,具体原因我也不清楚。 

例子: 

1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2  <html xmlns="http://www.w3.org/1999/xhtml">
3  <head>
4  <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
5  <title>无标题文档</title>
6  <style type="text/css">
7 .main{
8 width:200px;color:#fff;
9 height:400px;background:#f00;
10 height:200px\9;background:#ff0\9;
11 *height:200px;*background:#0ff;
12 _height:100px;_background:#000;
13  }
14 </style>
15 </head>
16 <body>
17 <div class="main">ff红色height:400px / IE8黄色height:200px / IE7 蓝色height:200px / IE6黑色height:100px</div>
18 </body>
19 </html>

----------------------------------------------------------------------------------------------------------------------------------- 

微软在IE8提供三种解析页面的模式 

  IE8 Standard Modes :默认的最标准的模式,严格按照W3C相关规定 

  IE7 Standards Modes :IE7现在用的解析网页的模式,开起机关是在<head>中加入 <meta http-equiv="X-UA-Compatible" content="IE=7"> 

  Quirks Modes :IE5用的解析网页的模式,开起机关是删除HTML顶部的DOCTYPE声明 

  注意:不同模式间的网页在IE8中可以互相 frame ,因此因不会模式下的DOM和CSS渲染不一样,所以会引发很多问题,务必注意如果你的页面对IE7兼容没有问题,又不想大量修改现有代码,同时又能在IE8中正常使用,微软声称,开发商仅需要在目前兼容IE7的网站上添加一行代码即可解决问题,此代码如下: 

<meta http-equiv="x-ua-compatible" content="ie=7" /> 

  IE8 最新css hack: 

  "\9" 例:"margin:0px auto\9;".这里的"\9"可以区别所有IE和FireFox. 

  "*" IE6、IE7可以识别.IE8、FireFox不能. 

  "_" IE6可以识别"_",IE7、IE8、FireFox不能. 

---------------------------------------------------------------------------------------------------------------------------------- 

一些IE6 IE7 IE8 FF的CSS hack 

 

1 p{+color:#f00;} 支持 IE6 IE7 不支持FF IE8
2 p{_color:#f00;} 支持 IE6 不支持FF
3 p{color:#00f !important;}
4 p{color:#f00;} 支持 IE7 IE6 FF IE8
5 p{color:#00f !important;color:#f00;} 支持 IE7 IE8 FF 不支持 IE6
6 head:first-child+body p{color:#f00;} 支持 IE7 IE8 FF 不支持 IE6

支持 IE8 不支持IE6 IE7 FF 

 

1 html*p{color:#f00;} 支持 IE6 IE7 不支持FF IE8
2 html>p{color:#f00;} 支持 IE7 IE8 FF 不支持 IE6
3 html[xmlns] p{color:#f00;} 支持 IE7 IE8 FF 不支持 IE6
4 @import 'style.css';
5 @import "style.css";
6 @import url(style.css);
7 @import url('style.css');
8 @import url("style.css"); 支持 IE7 IE6 FF IE8
9 p{color:#f00;} 支持 IE6 IE7 FF 不支持 IE8
10 * html p {color:#f00;} 支持 IE6 不支持FF IE7 IE8
11 *+html p {color:#f00;} 支持 IE7 IE8 不支持FF IE6
12 p {*color:#f00;} 支持 IE7 IE6 不支持FF IE8
 

==================================================== 

这样写,也许会简单些,很方便,但是这个只是IE8对IE7的兼容模式 

 

1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <meta http-equiv="x-ua-compatible" content="ie=7">
3 <html xmlns="http://www.w3.org/1999/xhtml">

注意:<meta http-equiv="x-ua-compatible" content="ie=7"> IE8兼容IE7的模式 

aliyun活动 https://www.aliyun.com/acts/limit-buy?userCode=re2o7acl
原文地址:https://www.cnblogs.com/wangbin/p/1919471.html