兼容IE6/IE7/IE8/FireFox的css hack

  1. .color{  
  2.   
  3. background-color: #CC00FF;  
  4.   
  5. background-color: #FF0000/9;  
  6.   
  7. *background-color: #0066FF;  
  8.   
  9. _background-color: #009933;  
  10.   
  11. }  


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



显示的结果: 



用火狐浏览,颜色是紫色 



用 IE8 浏览,颜色是红色 



用 IE7 浏览,颜色是蓝色 



用 IE6 浏览,颜色是绿色 



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


Java代码   收藏代码
  1. <meta http-equiv="X-UA-Compatible" content="IE=7">  

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



例子: 



Java代码   收藏代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2.   
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4.   
  5. <head>  
  6.   
  7. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
  8.   
  9. <title>无标题文档</title>  
  10.   
  11. <style type="text/css">  
  12.   
  13. .main{  
  14.   
  15. width:200px;color:#fff;  
  16.   
  17. height:400px;background:#f00;  
  18.   
  19. height:200px/9;background:#ff0/9;  
  20.   
  21. *height:200px;*background:#0ff;  
  22.   
  23. _height:100px;_background:#000;  
  24.   
  25. }  
  26.   
  27. </style>  
  28.   
  29. </head>  
  30.   
  31. <body>  
  32.   
  33. <div class="main">ff红色height:400px / IE8黄色height:200px / IE7 蓝色height:200px / IE6黑色height:100px</div>  
  34.   
  35. </body>  
  36.   
  37. </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 



Java代码   收藏代码
  1. p{+color:#f00;} 支持 IE6 IE7 不支持FF IE8  
  2.   
  3. p{_color:#f00;} 支持 IE6 不支持FF  
  4.   
  5. p{color:#00f !important;}  
  6.   
  7. p{color:#f00;} 支持 IE7 IE6 FF IE8  
  8.   
  9. p{color:#00f !important;color:#f00;} 支持 IE7 IE8 FF 不支持 IE6  
  10.   
  11. head:first-child+body p{color:#f00;} 支持 IE7 IE8 FF 不支持 IE6  

支持 IE8 不支持IE6 IE7 FF 



Java代码   收藏代码
  1. html*p{color:#f00;} 支持 IE6 IE7 不支持FF IE8  
  2.   
  3. html>p{color:#f00;} 支持 IE7 IE8 FF 不支持 IE6  
  4.   
  5. html[xmlns] p{color:#f00;} 支持 IE7 IE8 FF 不支持 IE6  
  6.   
  7. @import 'style.css';  
  8.   
  9. @import "style.css";  
  10.   
  11. @import url(style.css);   
  12.   
  13. @import url('style.css');  
  14.   
  15. @import url("style.css"); 支持 IE7 IE6 FF IE8  
  16.   
  17. p{color:#f00;} 支持 IE6 IE7 FF 不支持 IE8  
  18.   
  19. * html p {color:#f00;} 支持 IE6 不支持FF IE7 IE8  
  20.   
  21. *+html p {color:#f00;} 支持 IE7 IE8 不支持FF IE6  
  22.   
  23. p {*color:#f00;} 支持 IE7 IE6 不支持FF IE8  


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



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



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

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值