CSS Hack大全-教你如何区分出IE6-IE10、FireFox、Chrome、Opera,ie6-ie10chrome


现在的浏览器IE6-IE10、Firefox、Chrome、Opera、Safari。。。数量众多,可谓百家争鸣,对用户来说有了很多的可选择型,不过这可就苦了Web前端开发人员了。
今天把一些常用的CSS Hack整理了一下,包括常用的IE hack以及火狐、Chrome、Opera浏览器的Hack,并把这些CSS Hack综合的一起,写了一个小的浏览器测试器。如图所示:

下面就来看一下代码吧:

html部分:

复制代码 代码如下:

<div class="content">
    <div class="test"></div>
    <div class="txt">
        <p>IE6下背景颜色:<span class="ie6" style="background-color: #ccc;">#ccc</span></p>
        <p>IE7下背景颜色:<span class="ie7" style="background-color: #666;">#666</span></p>
        <p>IE8下背景颜色:<span class="ie8" style="background-color: #06f;">#06f</span></p>
        <p>IE9下背景颜色:<span class="ie9" style="background-color: #f00;">#f00</span></p>
        <p>IE10下背景颜色:<span class="ie10" style="background-color: #0ff;">#0ff</span></p>
        <p>webkit,Safari,Chrome下背景颜色:<span class="webkit-safari-gg" style="background-color: #ff0;">#ff0</span></p>
        <p>FireFox下背景颜色:<span class="firefox" style="background-color: #f0f;">#f0f</span></p>
        <p>Opera下背景颜色:<span class="opera" style="background-color: #0f0;">#0f0</span></p>
    </div>
</div>

CSS部分,此部分就只贴Hack部分的代码吧,布局的就不贴了:

复制代码 代码如下:

.content .test {
    width: 200px;
    height: 200px;
    background: #f60; /*all*/
    background: #06f9; /*IE*/
    *background: #666; /*IE6,7*/
    _background: #ccc; /*IE6*/
}

/* webkit and opera */
@media all and (min-width:0){
    .content .test {
        background: #0f0;
    }
}

/* webkit */
@media screen and (-webkit-min-device-pixel-ratio:0) {
    .content .test {
        background: #ff0;
    }
}

/*FireFox*/
@-moz-document url-prefix() {
    .content .test {
        background: #f0f;
    }
}

/*IE9+*/
@media all and (min-width:0) {
    .content .test{
        background: #f009;
        }
}

/*IE10+*/
@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {
    .content .test {
        background: #0ff;
    }
}

css-hack-ms-moz-webkit-o-Jb51.net.rar


对于Firefox与Opera的CSS hack

Firefox: -moz-

Opera:-o-

ie -ms-
Chrome、Safari -webkit-
 

CSS Hack问题,都有什?详解

由于不同的浏览器对CSS的支持及解析结果不一样,还由于CSS中的优先级的关系。我们就可以根据这个来针对不同的浏览器来写不同的CSS。
  CSS Hack大致有3种表现形式,CSS类内部Hack、选择器Hack以及HTML头部引用(if IE)Hack,CSS Hack主要针对IE浏览器。
  类内部Hack:比如 IE6能识别下划线"_"和星号" * ",IE7能识别星号" * ",但不能识别下划线"_",而firefox两个都不能认识。等等
  选择器Hack:比如 IE6能识别*html .class{},IE7能识别*+html .class{}或者*:first-child+html .class{}。等等
  HTML头部引用(if IE)Hack:针对所有IE:<!--[if IE]><!--您的代码--><![endif]-->,针对IE6及以下版本:<!--[if lt IE 7]><!--您的代码--><![endif]-->,这类Hack不仅对CSS生效,对写在判断语句里面的所有代码都会生效。
  书写顺序,一般是将识别能力强的浏览器的CSS写在后面。下面如何写里面说得更详细些。
编辑本段
如何写CSS Hack
  比如要分辨IE6和firefox两种浏览器,可以这样写:
  <style>
  div{
  background:green; /* for firefox */
  *background:red; /* for IE6 */ (both IE6 && IE7)
  }
  </style>
  我在IE6中看到是红色的,在firefox中看到是绿色的。
  解释一下:
  上面的css在firefox中,它是认识不了后面的那个带星号的东东是什么的,于是将它过滤掉,不予理睬,解析得到的结果是:div{background:green},于是理所当然这个div的背景是绿色的。
  在IE6中呢,它两个background都能识别出来,它解析得到的结果是:div{background:green;background:red;},于是根据优先级别,处在后面的red的优先级高,于是当然这个div的背景颜色就是红色的了。
  CSS hack:区分IE6,IE7,firefox
  区别不同浏览器,CSS hack写法:
  区别IE6与FF:
  background:orange;*background:blue;
  区别IE6与IE7:
  background:green !important;background:blue;
  区别IE7与FF:
  background:orange; *background:green;
  区别FF,IE7,IE6:
  background:orange;*background:green;_background:blue;
  background:orange;*background:green !important;*background:blue;
  注:IE都能识别*;标准浏览器(如FF)不能识别*;
  IE6能识别*,某些情况下不能识别 !important,
  ----......余下全文>>
 

评论关闭