• CSS hack 整理 - [椒图之门 Code]

    2009-06-29

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
    http://fuckui.net/logs/41663193.html

    HACK1 #example {
    margin:5px!important/* for Firefox */
    margin:10px/* for IE6 */
    margin /**/:11px/* for IE5 */
    }
    *+
    html #example{margin:15px!important; }/* for IE7 */

    说明:!important声明可以提升指定样式规则的应用优先权。上面“for Firefox” 有“!important”,“for IE7” 也要加“!important”,否则实际出来后IE7依然使用第一个maring值5px。
    HACK2 #example {margin:5px; }/* for Firefox */
    html #example {margin:10px;} /* for IE6 */
    *+html #abc{margin:15px!important;} /* for IE7 */

    说明:“for IE7”要加“!important”,“+html”能被ie7与ie5.01所识别,但ie5.01不识别important,所以*+html 再加!important 才只能被ie7识别。
    HACK3 #example {
    margin:5px/* For Firefox */
    *margin:10px/* For IE7 & IE6*/
    _margin:15px/* For IE6*/
    }

    说明:这样写CSS文件无法通过验证,但是代码可以写的比较少。
    HACK4:使用IE专用的条件注释 <!--其他浏览器 -->
    <
    link rel="stylesheet" type="text/css" href="css.css" />
    <!--[if 
    IE 7]>
    <!-- 
    适合于IE7 -->
    <
    link rel="stylesheet" type="text/css" href="ie7.css" />
    <![endif]-->
    <!--[if 
    lte IE 6]>
    <!-- 
    适合于IE6及以下 -->
    <
    link rel="stylesheet" type="text/css" href="ie.css" />
    <![endif]-->

    说明:条件注释的语法:
    [*]gt /Greater than/大于/<!--[if gt IE 5.5]>
    [*]gte /Greater than or equal to/大于等于/<!--[if gte IE 5.5]>
    [*]lt /Less than/小于/<!--[if lt IE 5.5]>
    [*]lte /Less than or equal to/小于等于/<!--[if lte IE 5.5]>
    [*]! /Note/不等于/<!--[if !IE 5.5]>        
    HACK5 example{
    margin5px;/*FF*/
    >margin10px;/*IE5*/
    }
    example/*IE5.5 */{
    >
    /*IE only*/margin15px;/*IE6*/
    >/*IE only*/margin /*IE5.5*/20px;
    }

    说明:“margin”定义的顺利不能改变,即FF-IE5-IE6-IE5.5。对于IE的定义在属性前要加“>”,因为“example/**/{}”这个HACK在FF中可以识别。