<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml" xml:lang = "en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>兼容各种浏览器层透明的CSS Hack</title>
<style type = "text/css">
.topDiv{
100%;
height:1800px;
background:rgba(0,0,0,0.3) none repeat scroll 0 0 !important;
filter:alpha(opacity = 30);
background:#000;
z-index:99;
position:absolute;
left:0px;
top:0px;
color:white;
font-size:20px;
font-family::"微软雅黑" "宋体" "Time New Roman";
text-align:center;
border-radius:20px;
}
</style>
</head>
<body>
<div class = "topDiv">
这是一个透明的层...
</div>
<h2>CSS Hack的原理是什么</h2>
<p>
由于不同的浏览器对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写在后面。下面如何写里面说得更详细些。
</p>
<h2>如何写CSS Hack</h2>
比如要分辨IE6和firefox两种浏览器,可以这样写:<br/>
<style><br/>
div{<br/>
background:green; /* for firefox */<br/>
*background:red; /* for IE6 */ (both IE6 && IE7)<br/>
}<br/>
</style><br/>
在IE6中看到是红色的,在firefox中看到是绿色的。<br/>
解释一下:<br/>
上面的css在firefox中,它是认识不了后面的那个带星号的东东是什么的,于是将它过滤掉,不予理睬,解析得到的结果是:div{background:green},于是理所当然这个div的背景是绿色的。<br/>
在IE6中呢,它两个background都能识别出来,它解析得到的结果
是:div{background:green;background:red;},于是根据优先级别,处在后面的red的优先级高,于是当然这个div
的背景颜色就是红色的了。<br/>
CSS hack:区分IE6,IE7,firefox<br/>
区别不同浏览器,CSS hack写法:<br/>
区别IE6与FF:<br/>
background:orange;*background:blue;<br/>
区别IE6与IE7:<br/>
background:green !important;background:blue;<br/>
区别IE7与FF:<br/>
background:orange; *background:green;<br/>
区别FF,IE7,IE6:<br/>
background:orange;*background:green;_background:blue;<br/>
background:orange;*background:green !important;*background:blue;<br/>
注:IE都能识别*;标准浏览器(如FF)不能识别*;<br/>
IE6能识别*,某些情况下不能识别 !important,<br/>
-----------------------------------------------------------------------------------------------
IE6支持重定义中的!important,例如:<br/>
.yuanxin {color:#e00!important;}<br/>
.yuanxin {color:#000;}<br/>
你将会发现定义了样式class="yuanxin"时,在IE下,字体显示为红色(#e00)。<br/>
但不支持同一定义中的!important。例如:<br/>
.yuanxin {color:#e00!important;color:#000}<br/>
此时在IE6下不支持,你将会发现定义了样式class="yuanxin"时,字体显示为黑色(#000)。<br/>
不包括如下这种形式的同一定义中的!important。<br/>
#pageOver{height:expression(document.documentElement.offsetHeight)!important;<br/>
height:100%;}此种形式的定义,IE6中是可以解释到important的。<br/>
-----------------------------------------------------------------------------------------------
IE6 IE7能识别*,也能识别!important;<br/>
FF不能识别*,但能识别!important;<br/>
IE6 IE7 FF<br/>
* √ √ ×<br/>
!important √<br/>
浏览器优先级别:FF<IE7<IE6,CSS hack书写顺序一般为FF IE7 IE6<br/>
以: " #demo {100px;} "为例;<br/>
#demo {100px;} /*被FIREFOX,IE6,IE7执行.*/<br/>
* html #demo {120px;} /*会被IE6执行,之前的定义会被后来的覆盖,所以#demo的宽度在IE6就为120px; */<br/>
*+html #demo {130px;} /*会被IE7执行*/<br/>
---------------<br/>
所以最后,#demo的宽度在三个浏览器的解释为:<br/>
FIREFOX:100px;<br/>
ie6:120px;<br/>
ie7:130px;<br/>
IE8 最新css hack:<br/>
"\9" 例:"border:1px \9;".这里的"\9"可以区别所有IE和FireFox.<br/>
"\0" IE8识别,IE6、IE7不能.<br/>
"*" IE6、IE7可以识别.IE8、FireFox不能.<br/>
"_" IE6可以识别"_",IE7、IE8、FireFox不能.<br/>
IE6 hack<br/>
_background-color:#CDCDCD; /* ie 6*/<br/>
IE7 hack<br/>
*background-color:#dddd00; /* ie 7*/<br/>
IE8 hack<br/>
background-color:red \0; /* ie 8/9*/<br/>
IE9 hack<br/>
background-color:blue \9\0;<br/>
火狐,遨游,及其它高级浏览器通用<br/>
background-color:red!important;<br/>
注意写hack的顺序,其中:<br/>
background-color:red\0;IE8和IE9都支持;background-color:blue\9\0; 仅IE9支持;<br/>
另外,background-color:eeeeee\9;的HACK支持IE6-IE8,但是IE8不能识别“*”和“_”的CSS HACK。<br/>
可综合上述规律灵活应用。<br/>
IE9 和 IE8 以及其他版本的区别说明<br/>
background-color:blue; 各个浏览器都认识,这里给firefox用;<br/>
background-color:red\9;\9所有的ie浏览器可识别;<br/>
background-color:yellow\0; \0 是留给ie8的,最新版opera也认识,后面自有hack写了给opera认的,所以,\0我们就认为是给ie8留的;<br/>
+background-color:pink; + ie7定了;<br/>
_background-color:orange; _专门留给神奇的ie6;<br/>
:root #test { background-color:purple\9; } :root是给ie9的,网上流传了个版本是 :root #test { background- color:purple\0;},
这个,新版opera也认识,所以经笔者反复验证最终ie9特有的为:root 选择符 {属性\9;}<br/>
@media all and (min-0px){ #test {background-color:black\0;} }
这个是老是跟ie抢着认\0的神奇的opera,必须加个\0,不然firefox,chrome,safari也都认识。。。<br/>
@media screen and (-webkit-min-device-pixel-ratio:0){ #test {background-color:gray;} }最后这个是浏览器新贵chrome和safari的。<br/>
</p>
</body>
</html>
另有一种兼容的写法为:
opacity:.35;
-moz-opacity:.35;
filter:alpha(opacity = 35);