css饥人谷技术博客

浏览器兼容

2017-10-17  本文已影响26人  74f4321c3397

什么是CSS hack

由于不同厂商的浏览器,比如Internet Explorer,Safari,Mozilla Firefox,Chrome等,或者是同一厂商的浏览器的不同版本,如IE6和IE7,对CSS的解析认识不完全一样,因此会导致生成的页面效果不一样,得不到我们所需要的页面效果。

这个时候我们就需要针对不同的浏览器去写不同的CSS,让它能在不同的浏览器中也能得到我们想要的页面效果。

简单来说,就是利用不同浏览器对不同css的识别不同这个bug,在css中加入兼容各个浏览器的css写法。

谈一谈浏览器兼容的思路

知道浏览器兼容的思路,比如知道如何去兼容ie6的思路,比具体去兼容去做重要的多。了解了思路之后,再去caniuser查CSS属性兼容,去BrowserHacks查Hack的写法,兼容的写法,具体去实现就可以了。

列举5种以上浏览器兼容的写法

范例1

.clearfix:after{
  content: '';
  display: block;
  clear: both;
}
.clearfix{
  *zoom: 1; /* 对于ie6、7浏览器,由于不兼容伪元素,需要加上下面的才可以生效 */
}

范例2

.target{
  display: inline-block;
  *display: inline; /* 对于ie6、7不兼容inline-block,所以需要加上下面这两句才生效*/
  *zoom: 1;   /*这个顺序是不能变得*/
}

范例3

 <!--[if lt IE 9]>  /*如果小于ie9下面的生效*/
    <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]-->

范例4

<!DOCTYPE html>
<!--[if IEMobile 7 ]> <html dir="ltr" lang="en-US"class="no-js iem7"> <![endif]-->
<!--[if lt IE 7 ]> <html dir="ltr" lang="en-US" class="no-js ie6 oldie"> <![endif]-->
<!--[if IE 7 ]>    <html dir="ltr" lang="en-US" class="no-js ie7 oldie"> <![endif]-->
<!--[if IE 8 ]>    <html dir="ltr" lang="en-US" class="no-js ie8 oldie"> <![endif]-->
<!--[if (gte IE 9)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html dir="ltr" lang="en-US" class="no-js"><!--<![endif]-->

范例5

color:red;    ⁄* 所有浏览器都支持 *⁄ 
  color:red !important;   ⁄* Firefox、IE7支持 *⁄
  _color:red;   ⁄* IE6支持 *⁄
  *color:red;   ⁄* IE6、IE7支持 *⁄
 *+color:red;   ⁄* IE7支持 *⁄
 color:red \9;   ⁄* IE6、IE7、IE8支持 *⁄
 color:red \0;   ⁄* IE8支持 *⁄

以下工具/名词是做什么的

一般在哪个网站查询属性兼容性?

caniuse

上一篇 下一篇

猜你喜欢

热点阅读