瀏覽器安全色之條件注釋,cssHack

來源:互聯網
上載者:User

標籤:

對於形形色色的瀏覽器,隨之而來的就是一些相容問題,大多應該都是IE下的相容問題,因為任何瀏覽器下出現渲染不一致都極有可能是我們自己的結構或樣式不符合W3C的某些要求,或者說違背了瀏覽器的某些規則而先造成的,所以我們應該盡量通過結構或CSS的修改來達到各瀏覽器渲染一致效果!

條件運算式(註:條件注釋只能用在IE5-IE9,因為微軟已經在IE10以後的版本中已禁用IE特有的條件注釋功能,詳情見:https://msdn.microsoft.com/zh-cn/library/ie/hh801214%28v=vs.85%29.aspx)

    <!--[if IE n]>        ...    <![endif]--><!--專對於IEn的一些樣式-->    <!--[if gt IE n]>        ...    <![endif]--><!--專對於IEn以上版本的一些樣式-->    <!--[if lt IE n]>        ...    <![endif]--><!--專對於IEn以下版本的一些樣式--><!--[if gte IE n]>        ...    <![endif]--><!--專對於IEn及以上版本的一些樣式--><!--[if lte IE n]>        ...    <![endif]--><!--專對於IEn及以下版的一些樣式-->  <!--[if !ie]><-->
    <!--<![endif]--><!--專對於除IE以外的一些樣式-->

條件樣式不但能用在樣式上,還能用在javascript上,但是請留意條件注釋只會在IE9及以下版本下識別。

例如下面這一段代碼只會在IE9下才會起用用,但是你把9改成10,在IE10下是沒有任何效果的

  <!--[if IE 9]>    <style type="text/css">        .typ{background:blue}    </style>    <script type="text/javascript">        alert("this is IE瀏覽器!");    </script>    <![endif]-->  <!--下面的這一段代碼在IE10下是沒有任何效果的-->  <!--[if IE 10]>    <style type="text/css">        .typ{background:blue}    </style>    <script type="text/javascript">        alert("this is IE瀏覽器!");    </script>    <![endif]-->
<!--下面的這一段代碼除了在Firefox,chrome下會彈出外,在IE10也會彈出-->
  <!--[if !ie]><-->
    <script type="text/javascript"> alert("this is not IE瀏覽器!");   </script>
    <!--<![endif]-->

綜上所述,條件注釋只能用來用來解決IE9下的相容問題,當然一般遇到樣式相容問題一般都是IE6-IE8下。

cssHack解決樣式相容問題:cssHack分為從css選取器和css屬性上來區別不同的Hack寫法

css選取器寫法

/*1、IE6以及IE6以下版本瀏覽器*/    * html .demo {color: green;}        /*2、僅僅IE7瀏覽器*/   *:first-child+html .demo {color: green;}/*3、除IE6之外的所有瀏覽器(IE7-9, Firefox,Safari,Opera)*/    html>body .demo {color: green;}/*4、IE8-9,Firefox,Safari,Opear*/     html>/**/body .demo {color: green;}/*5、IE9+*/    :root .demo {color: red;}/*6、Firefox瀏覽器*/    @-moz-document url-prefix() {                      .demo {                         color: red;                      }                    }/*6、Webkit核心瀏覽器(Safari和Google Chrome)*/    @media screen and (-webkit-min-device-pixel-ratio:0)    {                        .demo { color: red; }                    }/* 7、Opera瀏覽器*/   @media all and (-webkit-min-device-pixel-ratio:10000), not all and (-webkit-min-device-pixel-ratio:0){                        head~body  .demo { color: red; }                    }/*8、iPhone / mobile webkit*/   @media screen and (max-device-width: 480px) {                        .demo { color: red  }                    }

CSS屬性Hack寫法

/*1、IE6瀏覽器*/    .demo {_color: red;} /*2、IE6-7瀏覽器識別*/    .demo {*color: red;} /*3、所有瀏覽器除IE6瀏覽外*/    .demo {color/**/:red;}        /*4、IE6-9瀏覽器*/    .demo {color: red\9;}         /*5、IE7-8瀏覽器*/    .demo {color/*\**/:red\9;}

使用執行個體

.demo {  color: red;/*所有現代瀏覽器*/  color: green\9;/*所有IE瀏覽器*/  color: lime\0;/*IE8-9瀏覽器*/  *color: red;/*IE6-7瀏覽器*/ +color: blue;/*IE7瀏覽器*/ _color: orange;/*IE6瀏覽器*/ } :root .demo {color: #963\9;} @-moz-document url-prefix(){
  .demo{color:#897;}/* all firefox */ } @media screen and (-webkit-min-device-pixel-ratio:0){ .demo { color: #000; }/*webkit*/}@media all and (-webkit-min-device-pixel-ratio:10000), not all and (-webkit-min-device-pixel-ratio:0){ head~body .demo { color: red; }/*opera*/}

其實據個人工作經驗大多布局相容問題都會發在IE6-IE8,對於現代瀏覽器基本沒什麼相容問題,所以記住常用IE下的相容寫法就好啦!

詳細原文見:http://www.w3cplus.com/create-an-ie-only-stylesheet

詳細原文見:http://www.w3cplus.com/css/create-css-browers-hacks

 

瀏覽器安全色之條件注釋,cssHack

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.