浏览器Hack技巧

IE Hack

IE系列浏览器的hack大略如下:
_nowamagic:1px;-----------ie6
*nowamagic:1px;-----------ie7
nowamagic:1px\0;----------ie89
nowamagic:1px\9\0;--------ie9
:root nowamagic:1px; ----ie9(实际情况可能ie9还是有问题,再用这种方式)

这样就基本上就可以兼容所有IE。



其中粉红色部分为属性hack,黄色部分为选择器hack,它们可以结合使用。此外Firefox和Chrome也有它们专有的hack,详细hack方式及使用示例如下:

Firefox 与 Chrome 的 Hack

Firefox:

@-moz-document url-prefix()
/*写在选择器外层时(只可写在此处):Firefox only*/

Chrome:

@media screen and (-webkit-min-device-pixel-ratio:0)    
/*写在选择器外层时(只可写在此处):Chrome only*/

使用示例:

@-moz-document url-prefix()  /*Firefox*/
{
    body
    {
        background-color:pink;
    }
}

浏览器对css的解析是从前到后的,并且采用最后一个样式声明。

CSS 实例

.color{
    background-color: #CC00FF;        /*所有浏览器都会显示为紫色*/
    background-color: #FF0000\9;    /*IE6、IE7、IE8会显示红色*/
    *background-color: #0066FF;        /*IE6、IE7会变为蓝色*/            
    _background-color: #009933;        /*IE6会变为绿色*/
}
background: red;       /* 对FF Opera和Safari有效 */
#background: blue;      /* 对 IE6 和 IE7有效 */
_background: green;      /* 只对IE6有效 */
/*/background: orange;*/      /** 只对IE8有效 **/
!important         /*FF、IE7有效*/
*         /*IE都有效*/

IE8是可以和IE7兼容的,简单一行代码,让IE8自动调用IE7的渲染模式。只需要在页面中加入如下

HTTP meta-tag:<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />

只要IE8读到这个标签,它就会自动启动IE7兼容模式,保证页面完整展示。
混用起来大约是这样:

:root .demo {  
    background:#963\9; /* 仅IE9适用 */  
}  
.demo {  
    width: 300px;  
    height: 200px;  
    background: #036; /* 所有浏览器都适用 */  
    background: #09F\9; /* IE6~IE9 */  
    background: #09F\0; /* IE8~IE9 */  
    background: #09F\0/; /* IE8 */  
    *background: #F60; /* IE6/IE7 */  
    +background: #F60; /* IE6/IE7 */  
    @background: #F60; /* IE6/IE7 */  
    >background: #F60; /* IE6/IE7 */  
    _background: #ccc; /* IE6 */  
}  
@media all and (min-width:0) {  
    .demo {  
        background: #F06; /* webkit and opera */  
    }  
}  
  
@media screen and (-webkit-min-device-pixel-ratio:0){  
    .demo {background:#609;}/*webkit (& Opera9.2)*/  
}

原文

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 做前端多年,虽然不是经常需要hack,但是我们经常会遇到各浏览器表现不一致的情况。基于此,某些情况我们会极不情愿的...
    大女表哥阅读 1,132评论 0 9
  • 一、如何调试 IE 浏览器? 在IE7以上的版本中可以通过按快捷键F12调出开发人员调试框,如下图IE7以上调试工...
    dengpan阅读 564评论 0 2
  • CSS Hack 不同的浏览器对某些CSS代码解析会存在一定的差异,因此就会导致不同浏览器下给用户展示的页面效果不...
    zx9426阅读 408评论 0 0
  • 1.如何调试 IE 浏览器 IE7以上(包括Edge)自带开发者工具。Edge: 安装虚拟机,安装各种不同版本的I...
    26d608950683阅读 1,690评论 0 5
  • 一、问答部分 1. 如何调试 IE 浏览器 IE7及以上版本有调试台,可以按F12启动。Paste_Image.p...
    _hello__world_阅读 484评论 0 1