在.css文件中能否使用if IE来判断是否是ie呢

html-css017

在.css文件中能否使用if IE来判断是否是ie呢,第1张

因为IE各版本的浏览器对我们制作的WEB标准的页面解释不一样,具体就是对CSS的解释不同,我们为了兼容这些,可运用条件注释来各自定义,最终达到兼容的目的

可使用如下代码检测当前IE浏览器的版本(注意:在非IE浏览器中是看不到效果的)

 /*判断浏览器的版本*/

<!––[if IE]> 

<h1>您正在使用IE浏览器</h1> <!––[if IE 5]> 

<h2>版本 5</h2> <![endif]––> 

<!––[if IE 5.0]> 

<h2>版本 5.0</h2> <![endif]––> 

<!––[if IE 5.5]> 

<h2>版本 5.5</h2> <![endif]––> 

<!––[if IE 6]> 

<h2>版本 6</h2> <![endif]––> 

<!––[if IE 7]> 

<h2>版本 7</h2> <![endif]––> 

<![endif]––>

那如果当前的浏览器是IE,但版本比IE5还低,该怎么办呢,可以使用<!–[if ls IE 5]>,当然,根据条件注释只能在IE5+的环境之下,所以<!–[if ls IE 5]>根本不会被执行。

 

< !–- 默认先调用css.css样式表 –-> 

<link rel="stylesheet" type="text/css" href="css.css" />< !-–[if IE 7]> 

<!–- 如果IE浏览器版是7,调用ie7.css样式表- –> 

<link rel="stylesheet" type="text/css" href="ie7.css" />< ![endif]–-> 

<!–-[if lte IE 6]> 

<!–- 如果IE浏览器版本小于等于6,调用ie.css样式表 -–> 

<link rel="stylesheet" type="text/css" href="ie.css" />< ![endif]–>

<!--[if IE 6]> 

    可以把样式写在这中间

<![endif]--> 

只有IE6版本可见 

<!--[if lte IE 6]> 

<![endif]--> 

IE6及其以下版本可见 

<!--[if gte IE 6]> 

<![endif]--> 

IE6及其以上版本可见 

<!--[if IE 7]> 

<![endif]--> 

只有IE7版本可见 

<!--[if lte IE 7]> 

<![endif]--> 

IE7及其以下版本可见 

<!--[if gte IE 7]> 

<![endif]--> 

IE7及其以上的版本可见 

<!--[if IE 8]> 

<![endif]--> 

只有IE8版本可见 

<!--[if lte IE 8]> 

<![endif]--> 

IE8及其以下的版本可见 

<!--[if gte IE 8]> 

<![endif]--> 

IE8及其以上的版本可见 

<![if !IE]> 

<![endif]> 

除了IE以外的版本

各浏览器兼容性css写法

/* 针对Chrome谷歌浏览器内核支持的CSS样式 */

@media screen and (-webkit-min-device-pixel-ratio:0) {

样式 /* 例如 .font1 {color:red} */

}

针对Firefox浏览器的内核CSS写法:

@-moz-document url-prefix(){

样式 /* 例如 .font1 {color:red} */

}

如果只让ie6看见用*html .head{color:#000}

如果只让ie7看见用*+html .head{color:#000}

如果只让ff看见用:root body .head{color:#000}

如果只让ff、IE8看见用html>/**/body .head{color:#000}

如果只是不让ie6看见用html>body .head{color:#000} 即对IE 6无效

如果只是不让ff、IE8看见用*body .head{color:#000}即对ff、IE8无效

针对具体属性

如果只让ie6看见用_

.head{_color:#000}

如果只让ie7看见用+与_结合的方法:

.head{+color:#f00!_color:#000}

IE8正式版hack

\9″ 例:”margin:0px auto\9”.这里的”\9″可以区别所有IE8和FireFox.

“*” IE6、IE7可以识别.IE8、FireFox不能.

“_” IE6可以识别”_”,IE7、IE8、FireFox不能.

如:

.a { color:#f00color:#f60\9 +color:#00FF00 _color:#0000FF}

从左到右分别对应 FireFoxsIE8 IE7 IE6

还有写css样式一定要记住顺序:

以下为引用的内容:

#1 { color: #333} /* Moz */

* html #1 { color: #666} /* IE6 */

*+html #1 { color: #999} /* IE7 */