zoukankan      html  css  js  c++  java
  • CSS Hack

    摘要:

          在我们制作页面时CSS hack由于不同的浏览器,比如Internet Explorer,Mozilla Firefox等,对CSS的解析认识不一样,因此会导致生成的页面效果不一样,得不到我们所需要的页面效果。 这个时候我们就需要针对不同的浏览器去写不同的CSS,让它能够同时兼容不同的浏览器,能在不同的浏览器中也能得到我们想要的页面效果。

      CSS Hack大致有3种表现形式,属性级Hack、选择器Hack以及IE条件Hack

    注意:尽可能减少对CSS Hack的使用。

    原理:

      由于不同的浏览器对CSS的支持及解析结果不一样,还由于CSS中的优先级的关系。我们就可以根据这个来针对不同的浏览器来写不同的CSS。

    实际应用:

    1. 选择器:
        

      语法:

             <hack> selector{ sRules }

      说明:

       选择不同的浏览器及版本
      • 通常如未作特别说明,所有的代码和示例的默认运行环境都为标准模式。
      • 一些CSS Hack由于浏览器存在交叉认识,所以需要通过层层覆盖的方式来实现对不同浏览器进行Hack的。
      • 简单列举几个:
        • * html .test{color:#090;}                                                  /* For IE6 and earlier */
          * + html .test{color:#ff0;}                                                /* For IE7 */
          .test:lang(zh-cn){color:#f00;}                                             /* For IE8+ and not IE */
          .test:nth-child(1){color:#0ff;}                                            /* For IE9+ and not IE */
          :root .test {background-color:green;} /* For IE9 and Opera */
          @media screen and (-webkit-min-device-pixel-ratio:0) {.test {color:gray;}} /* For Chrome and Safari */
          @-moz-document url-prdfix() {.test {color:#fff}} /* For Forefox */

          * 上述代码中的3,4两行就是典型的利用能力来进行选择的CSS Hack。

                 
    2. IE条件Hack:

      语法:

      <!--[if <keywords>? IE <version>?]>
      HTML代码块
      <![endif]-->

      取值:

      <keywords>

        if条件共包含6种选择方式:是否、大于、大于或等于、小于、小于或等于、非指定版本

           是否:
      指定是否IE或IE某个版本。关键字:空
           大于:
      选择大于指定版本的IE版本。关键字:gt(greater than)
           大于或等于:
      选择大于或等于指定版本的IE版本。关键字:gte(greater than or equal)
           小于:
      选择小于指定版本的IE版本。关键字:lt(less than)
           小于或等于:
      选择小于或等于指定版本的IE版本。关键字:lte(less than or equal)
           非指定版本:
      选择除指定版本外的所有IE版本。关键字:!
      <version>

            目前的常用IE版本为6.0及以上

      说明:

      用于选择IE浏览器及IE的不同版本
      • if条件Hack是HTML级别的(包含但不仅是CSS的Hack,可以选择任何HTML代码块)
      • 如不想在非IE中看到某区域,可这样写:

        <!--[if IE]>
        <p>你在非IE中将看不到我的身影</p>
        <![endif]-->

        上述p代码块,将只在IE中可见。

      • if条件6种选择方式的使用示例(下述代码中被条件注释包含的HTML代码块也可以是link标记):
      • 是否,示例代码:

        <!--[if IE]>
        <style>
        .test{color:red;}
        </style>
        <![endif]-->

        在上述代码中,只有IE浏览,才能看到应用了test类的元素是红色文本。

      • 大于,示例代码:

        <!--[if gt IE 6]>
        <style>
        .test{color:red;}
        </style>
        <![endif]-->

        在上述代码中,只有IE6以上,才能看到应用了test类的元素是红色文本。

      • 大于或等于,示例代码:

        <!--[if gte IE 6]>
        <style>
        .test{color:red;}
        </style>
        <![endif]-->

        在上述代码中,只有IE6以上(含IE6),才能看到应用了test类的元素是红色文本。

      • 小于,示例代码:

        <!--[if lt IE 7]>
        <style>
        .test{color:red;}
        </style>
        <![endif]-->

        在上述代码中,只有IE7以下,才能看到应用了test类的元素是红色文本。

      • 小于或等于,示例代码:

        <!--[if lte IE 7]>
        <style>
        .test{color:red;}
        </style>
        <![endif]-->

        在上述代码中,只有IE7以下(含IE7),才能看到应用了test类的元素是红色文本。

      • 非指定版本,示例代码:

        <!--[if ! IE 7]>
        <style>
        .test{color:red;}
        </style>
        <![endif]-->

        在上述代码中,除IE7以外的IE版本,都能看到应用了test类的元素是红色文本。

    3. 属性级:

        语法:

            selector{<hack>property:value;}或者selector{property:value<hack>;}

        取值:

            _:选择IE6及以下。连接线(中划线)(-)亦可使用,为了避免与某些带中划线的属性混淆,所以使用下划线(_)更为合适。
            *:选择IE7及以下。诸如:(+)与(#)之类的均可使用,不过业界对(*)的认知度更高
            9:选择IE6+,可以区别所有IE和FireFox。
            :选择IE8+和Opera
            [;property:value;];:选择webkit核心浏览器(Chrome,Safari)。IE7及以下也能识别。中括号内外的3个分号必须保留,第一个分号前可以是任意规则或任意多个规则
                    [;color:#f00;]; 与 [color:#f00;color:#f00;]; 与 [margin:0;padding:0;color:#f00;]; 是等价的。生效的始终是中括号内的最后一条规则,所以通常选用第一种写法最为简洁。
         注意:!important并不是一个hack手段,他是被用来改变css的优先级的,因为ie6是不识别!important,所以就被拿来当做css hack的一种,这是错误的。

        说明:

        选择不同的浏览器及版本
      •   浏览器优先级别:FF<IE9<IE8<IE7<IE6,CSS hack书写顺序一般为FF IE9 IE8 IE7 IE6
      • 一些CSS Hack由于浏览器存在交叉认识,所以需要通过层层覆盖的方式来实现对不同浏览器进行Hack的。如下面这个例子:
      • 如想同一段文字在IE6,7,8显示为不同颜色,可这样写:
        • .test{
          	color:#0909; /* For IE8+ */
          	*color:#f00;  /* For IE7 and earlier */
          	_color:#ff0;  /* For IE6 and earlier */
          }
            * 上述Hack均需运行在标准模式下,若在怪异模式下运行,这些Hack将会被不同版本的IE相互识别,导致失效。

    HACK Demo:

      

     1 .demo{color:#f1ee18;/*所有识别*/ background-color:#00deff9; /*IE6、7、8识别*/ +background-color:#a200ff;/*IE6、7识别*/ _background-color:#1e0bd1/*IE6识别*/} 
     2 
     3 @media screen and (-webkit-min-device-pixel-ratio:0){.demo{background-color:#f1ee18}}{} /* Safari(Chrome) 有效 */
     4 @media all and (min- 0px){ .demo{background-color:#f1ee18;/*opera and Safari(Chrome) and firefox*/ background-color:#4cac70;}/* 仅 Opera 有效 */ }{} 
     5 
     6 .demo, x:-moz-any-link, x:default{background-color:#4eff00;/*IE7、Firefox3.5及以下 识别 */} 
     7 @-moz-document url-prefix(){.demo{background-color:#4eff00;/*仅 Firefox 识别 */}} 
     8 * +html .demo{background-color:#a200ff;}/* 仅IE7 识别 */
     9 
    10 /* 一般情况下 我们区分IE7 只用 +background-color 配合 _background-color 就行了 如果必须写 .demo, x:-moz-any-link, x:default 这样的代码区分 Firefox3.5及以下 则谨记此写法对IE7也有效,故在其中要再重写一次 +background-color 或者使用 * +html .demo{background-color:blue;} 方法仅对 IE7 有效。可使用 @-moz-document url-prefix(){}方法独立区分所有 firefox */
    11 
    12 .demo, x:-moz-any-link, x:default{display:block;/*IE7 firefox3.5及以下 识别 */+display:none/*再区分一次IE7*/} 
    13 @-moz-document url-prefix(){.demo{display:block;/*仅 firefox 识别 */}} 
    14 @media screen and (-webkit-min-device-pixel-ratio:0){.demo{display:block;}}{} /* safari(Chrome) 有效 */ 
    15 @media all and (min- 0px){.demo{display:none;} /* 仅 Opera 有效 */ }{}

    总结:

      css hack 并不是标准的css,所以应该尽量少使用hack。

     
  • 相关阅读:
    180. Consecutive Numbers
    181. Employees Earning More Than Their Managers
    15. 3Sum
    11. Container With Most Water
    178. Rank Scores
    在多台服务器上简单实现Redis的数据主从复制
    Head First
    23种设计模式(6):模版方法模式
    《Head.First设计模式》的学习笔记(9)--外观模式
    Head First--设计模式(装饰者模式)
  • 原文地址:https://www.cnblogs.com/xiyangbaixue/p/3967118.html
Copyright © 2011-2022 走看看