css hack

css hack

求助编辑百科名片

CSS hack

CSS hack

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

目录

简介
CSS Hack的原理是什么
如何写CSS Hack
  1. IE6 hack
  2. IE7 hack
  3. IE8 hack
  4. IE9 hack
  5. 火狐,遨游,及其它高级浏览器通用
  6. IE9 和 IE8 以及其他版本的区别说明
展开
简介
CSS Hack的原理是什么
如何写CSS Hack
  1. IE6 hack
  2. IE7 hack
  3. IE8 hack
  4. IE9 hack
  5. 火狐,遨游,及其它高级浏览器通用
  6. IE9 和 IE8 以及其他版本的区别说明
展开

编辑本段简介

这个针对不同的浏览器写不同的CSS code的过程,就叫CSS hack!

编辑本段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,

-----------------------------------------------------------------------------------------------

IE6支持重定义中的!important,例如:

.yuanxin {color:#e00!important;}

.yuanxin {color:#000;}

你将会发现定义了样式class="yuanxin"时,在IE下,字体显示为红色(#e00)。

但不支持同一定义中的!important。例如:

.yuanxin {color:#e00!important;color:#000}

此时在IE6下不支持,你将会发现定义了样式class="yuanxin"时,字体显示为黑色(#000)。

不包括如下这种形式的同一定义中的!important。

#pageOver{height:expression(document.documentElement.offsetHeight)!important;

height:100%;}此种形式的定义,IE6中是可以解释到important的。

-----------------------------------------------------------------------------------------------

IE6 IE7能识别*,也能识别!important;

FF不能识别*,但能识别!important;

IE6 IE7 FF

* √ √ ×

!important √

√ √

浏览器优先级别:FF<IE7<IE6,CSS hack书写顺序一般为FF IE7 IE6

以: " #demo {100px;} "为例;

#demo {100px;} /*被FIREFOX,IE6,IE7执行.*/

* html #demo {120px;} /*会被IE6执行,之前的定义会被后来的覆盖,所以#demo的宽度在IE6就为120px; */

*+html #demo {130px;} /*会被IE7执行*/

所以最后,#demo的宽度在三个浏览器的解释为: FIREFOX:100px; ie6:120px; ie7:130px;

IE8 最新css hack:

"\9" 例:"border:1px \9;".这里的"\9"可以区别所有IE和FireFox.

"\0" IE8识别,IE6、IE7不能.

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

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

IE6 hack
_background-color:#CDCDCD; /* ie 6*/
IE7 hack
*background-color:#dddd00; /* ie 7*/
IE8 hack
background-color:red \0; /* ie 8/9*/
IE9 hack
background-color:blue \9\0;
火狐,遨游,及其它高级浏览器通用
background-color:red!important;

注意写hack的顺序,其中:

background-color:red\0;IE8和IE9都支持;background-color:blue\9\0; 仅IE9支持;

另外,background-color:eeeeee\9;的HACK支持IE6-IE8,但是IE8不能识别“*”和“_”的CSS HACK。

可综合上述规律灵活应用。

IE9 和 IE8 以及其他版本的区别说明
background-color:blue; 各个浏览器都认识,这里给firefox用;

background-color:red\9;\9所有的ie浏览器可识别;

background-color:yellow\0; \0 是留给ie8的,最新版opera也认识,后面自有hack写了给opera认的,所以,\0我们就认为是给ie8留的;

+background-color:pink; + ie7定了;

_background-color:orange; _专门留给神奇的ie6;

:root #test { background-color:purple\9; } :root是给ie9的,网上流传了个版本是 :root #test { background- color:purple\0;},这个,新版opera也认识,所以经笔者反复验证最终ie9特有的为:root 选择符 {属性\9;}

@media all and (min-0px){ #test {background-color:black\0;} } 这个是老是跟ie抢着认\0的神奇的opera,必须加个\0,不然firefox,chrome,safari也都认识。。。

@media screen and (-webkit-min-device-pixel-ratio:0){ #test {background-color:gray;} }最后这个是浏览器新贵chrome和safari的。

原文地址:https://www.cnblogs.com/phpbin/p/2703903.html