欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

常见的浏览器Hack技巧整理

程序员文章站 2023-09-08 22:34:45
常见的浏览器hack技巧整理 如果你经常需要做前端页面,那么你一定多多少少需要解决页面的浏览器兼容问题。而浏览器兼容问题大部分也集中在对ie系列的兼容。这里就总结一下对i...

常见的浏览器hack技巧整理

如果你经常需要做前端页面,那么你一定多多少少需要解决页面的浏览器兼容问题。而浏览器兼容问题大部分也集中在对ie系列的兼容。这里就总结一下对ie系列的css 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,黄色部分为选择器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)*/ 
}

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!