认识css常见的hack

一、认识css hack

CSS Hack只要是来解决浏览器局部的兼容性问题,主要是因为每个浏览器对css的解析各不相同,导致输出到页面的效果的差异;

二、css hack的三种常见形式:css属性hack,css选择符hack和css针对IE浏览器的条件注释hack

      1、css属性hack:比如IE6能识别"_"和"*",IE7能识别"*"但不能识别"_";

      2、选择符级Hack:IE6能识别*html .class{} IE7能识别*+html .class{}或者*:html .class{};

      3、针对ie的条件注释Hack:比如针对所有IE:<!–[if IE]><!–您的代码–><![endif]–>针对IE6及以下版本:<!–[if lt IE 7]><!–您的代码–><![endif]–>,此时写在判断力的代码都生效。

三、hack例子

      1、CSS属性级Hack

           color:red; /*所有浏览器*/    _color:red; /*仅IE6*/    *color:red; /*IE6和IE7*/  +color:red; /*仅IE6*/   color:red; /* IE8、IE9 识别*/ color:red9; /* 仅IE9识别 */    color:red!important; /* IE6 不识别!important*/

      2、CSS选择符级Hack

           *html #container { color:red;} /* 仅IE6 识别 */    *+html #container { color:red;} /* 仅IE7 识别 */

      3、IE条件注释Hack

          <!--[if IE]>此处内容只有IE可见<![endif]-->    <!--[if IE 6]>此处内容只有IE6.0可见<![endif]-->    <!--[if IE 7]>此处内容只有IE7.0可见<![endif]-->   <!--[if gt IE 6]> IE6以上版本可识别,IE6无法识别 <![endif]-->    <!--[if gte IE 7]> IE7以及IE7以上版本可识别 <![endif]--> <!--[if lt IE 7]> 低于IE7的版本才能识别,IE7无法识别。 <![endif]-->    <!--[if lte IE 7]> IE7以及IE7以下版本可识别<![endif]-->   <!--[if !IE]>此处内容只有非IE可见<![endif]-->

 

          

     

posted @ 2018-09-30 11:08  叫我汤先森  阅读(683)  评论(0编辑  收藏  举报