前端相关

浏览器兼容

2017-08-05  本文已影响11人  DeeJay_Y

浏览器兼容思路

由于不同的浏览器,比如IE6,IE7,Firefox等,对CSS的解析标准不一样,因此会导致生成的页面效果不一样,得不到我们所需要的页面效果。

这时我们就需要针对不同的浏览器去写不同的CSS,让它能够同时兼容不同的浏览器,能在不同的浏览器中得到想要的相同的效果。

这个针对不同的浏览器写不同的CSS的过程,就叫css hack。

borwserhacks

要不要做:
产品的角度(受众,受众浏览器比例,效果优先还是基本功能优先)
成本的角度(有没有必要做)
做到什么程度:
让哪些浏览器支持哪些效果
如何做:
根据兼容需求选择技术框架/库
根据兼容需求选择兼容工具(html5shiv.js,respond.js,css reset,normalize.css)
条件注释,CSS hack,js能力检测做一些修补

浏览器兼容行动

css hack有3种表现形式:css属性前缀法,选择器前缀法,以及IE条件注释法。大部分是针对IE浏览器不同版本之间的表现差异而引入的。

  1. 属性前缀法(类内部hack):IE6可以识别下划线和星号,IE7可以识别星号,但不能识别下划线,IE6~IE10都识别\9。
.box{
    color: red;
    _color: blue; /*IE 6*/
    *color: green; /*IE 6 7*/
    color: yellow\9; /*IE EDGE 6-8*/
}
  1. 选择器前缀法(选择器hack)

  2. IE条件注释法

<style type="text/css">
    .clearfix:after{
        content: "";
        display: block;
        clear: both;
    }
    .clear{
        *zoom: 1;  /*针对ie6 7*/
    }
</style>

兼容inline-block:

<style type="text/css">
    .target{
        display: inline-block;
        *display: inline;
        *zoom: 1;
    }
</style>

一般兼容性差的先写,做兼容性处理的语句放到后面。

条件注释写法

<!--[if IE 6]>
<p>You are using Internet Explorer 6.</p>
<![endif]-->
<!--[if !IE]><!-->
<script>alert(1);</script>
<!--<![endif]-->
<!--[if IE 8]>
<link href="ie8only.css" rel="stylesheet">
<![endif]-->

css hack:

.box{
color: red;
_color: blue; /*ie6*/
*color: pink; /*ie67*/
color: yellow\9;  /*ie/edge 6-8*/
}

使用浏览器兼容工具:

小于ie9时生效
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->

浏览器兼容工具

上一篇下一篇

猜你喜欢

热点阅读