一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

css区分ie8/ie9/ie10/ie11 chrome firefox的代码

时间:2020-03-23 编辑:袖梨 来源:一聚教程网

本篇文章小编给大家分享一下css区分ie8/ie9/ie10/ie11 chrome firefox的代码,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

例如现有CSS代码如下:

.divContent{
    background-color:#eee;
}

那么下面我们就来写一下,如何使代码兼容几个主流浏览器。

/* IE8+ */
.divContent{
    background-color:#eee;
}
/* IE8、IE9 */
.divContent{
    background-color:#eee89;
}
/* IE9 */
.divContent{
    background-color:#eee9;
}

注意,8的写法是错误的,不能试图这样hack IE8。上述代码没有对IE10和IE11分别hack(好像没有对这两个浏览器单独hack的写法),那么IE10和IE11使用的就是IE8+那个样式。

IE家族hack完毕,下面看看如何hack Chrome和Firefox浏览器。

/* Chrome */
@media screen and (-webkit-min-device-pixel-ratio:0) {
    .divContent{
        background-color:#eee;
    }
}
/* Firefox */
@-moz-document url-prefix() {
    .divContent{
        background-color:#eee;
    }
}

另外,还可以这样hack其他浏览器

/* Chrome 和 opera */
@media all and (min-width:0){
    .divContent{
        background-color:#eee;
    }
}
/* IE9+ */
@media all and (min-width:0) {
    .divContent{
        background-color:#eee;
    }
}
/* IE10+ */
@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none)  {
    .divContent{
        background-color:#eee;
    }
}

经过这样hack,网站浏览器兼容性问题就可以完美解决了。

在css中区别ie浏览器和chrome浏览器

/***** 样式 Hack ******/

/* IE6 */
 _color: blue;

/* IE6, IE7 */
 *color: blue; /* 或 #color: blue */

/* 除 IE6 外任何浏览器 */
 color/**/: blue;

/* IE6, IE7, IE8 */
 color: blue9;

/* IE7, IE8 */
 color/***/: blue9;

/* IE6, IE7 -- 作为 !important 使用 */
 color: blue !ie; /*  !后面的字串可以为任意字串 */

/***** 选择器 Hack ******/

/* IE6 及以下 */
* html #uno  { color: red }

/* IE7 */
*:first-child+html #dos { color: red } 

/* IE7, FF, Saf, Opera  */
html>body #tres { color: red }

/* IE8, FF, Saf, Opera (除 IE 6,7 外任何浏览器) */
html>/**/body #cuatro { color: red }

/* Opera 9.27 及以下, safari 2 */
html:first-child #cinco { color: red }

/* Safari 2-3 */
html[xmlns*=""] body:last-child #seis { color: red }

/* safari 3+, chrome 1+, opera9+, ff 3.5+ */
body:nth-of-type(1) #siete { color: red }

/* safari 3+, chrome 1+, opera9+, ff 3.5+ */
body:first-of-type #ocho {  color: red }

/* saf3+, chrome1+ */
@media screen and (-webkit-min-device-pixel-ratio:0) {
 #diez  { color: red  }
}

/* iPhone / webkit 内核移动端 */
@media screen and (max-device-) {
 #veintiseis { color: red  }
}

/* Safari 2 - 3.1 */
html[xmlns*=""]:root #trece  { color: red  }

/* Safari 2 - 3.1, Opera 9.25 */
*|html[xmlns*=""] #catorce { color: red  }

/* 除 IE6-8 外任何浏览器 */
:root *> #quince { color: red  }

/* IE7 */
*+html #dieciocho {  color: red }

/* Firefox only. 1+ */
 #veinticuatro,  x:-moz-any-link  { color: red }

/* Firefox 3.0+ */
 #veinticinco,  x:-moz-any-link, x:default  { color: red  }

热门栏目