CSS Hack

来源:互联网 发布:aris 7.1 软件下载 编辑:程序博客网 时间:2024/06/06 19:44
针对不同的浏览器写不同的CSS code的过程,叫CSS hack。
CSS Hack的原理:
  由于不同的浏览器对CSS的支持及解析结果不一样,还由于CSS中的优先级的关系。我们就可以根据这个来针对不同的浏览器来写不同的CSS。 
  比如 IE6能识别下划线"_"和星号" * ",IE7能识别星号" * ",但不能识别下划线"_",而firefox两个都不能认识。等等 
  书写顺序,一般是将识别能力强的浏览器的CSS写在后面。下面如何写里面说得更详细些。 
  比如要分辨IE6和firefox两种浏览器,可以这样写: 
  <style> 
  div{ 
  background:green; /* for firefox */ 
  *background:red; /* for IE6 */ 
  } 

  </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)。 

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

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

  IE6 IE7 FF 

  * √ √ × 

  !important × √ √ 

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

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

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

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

  *+html #demo {width:130px;} /*会被IE7执行*/ 
  --------------- 
  所以最后,#demo的宽度在三个浏览器的解释为: 

  FIREFOX:100px; 

  ie6:120px; 

  ie7:130px; 

  IE8 最新css hack: 

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

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

  "_" IE6可以识别"_",IE7、IE8、FireFox不能. 
 
IE6/IE7对FireFox 
*+html 与 *html 是IE特有的标签, firefox 暂不支持.而*+html 又为 IE7特有标签,IE6暂不支持。
用法如下:
Html代码
  1. <style rel="stylesheet" type="text/css">  
  2. <!--   
  3. .content{background:#a5a5a5;height:100px;}/* Moz */   
  4. *html .content{background:#a5a5a5;height:200px;}/* IE6 */   
  5. *+html .content{background:#a5a5a5;height:300px;}/* IE7 */    
  6. -->  
  7. </style>  
<style rel="stylesheet" type="text/css">
<!--
.content{background:#a5a5a5;height:100px;}/* Moz */
*html .content{background:#a5a5a5;height:200px;}/* IE6 */
*+html .content{background:#a5a5a5;height:300px;}/* IE7 */ 
-->
</style>
 
Html代码
  1. height:50px; /*For Firefox*/   
  2. *height:100px; /*For IE7 & IE6*/   
  3. _height:150px; /*For IE6*/  
height:50px; /*For Firefox*/
*height:100px; /*For IE7 & IE6*/
_height:150px; /*For IE6*/
同样可以用相同的原理来为IE6、IE7、FF设置不同的width,height,margin,padding等属性。
 
min-height 
IE不认min-height,FF识别,利用以上这些属性,我们可以这样定义最小高度
 
Html代码
  1. <style rel="stylesheet" type="text/css">  
  2. <!--  
  3. .content{background:#a5a5a5;height:auto!important; height:500px; min-height:500px;}  
  4. -->  
  5. </style>  
<style rel="stylesheet" type="text/css">
<!--
.content{background:#a5a5a5;height:auto!important; height:500px; min-height:500px;}
-->
</style>
因为在IE里(包括IE6,IE7)认为height就是最小高度,当content中的内容超出设置的高度时,在FF中可以用min-height设置最小高度××,这时即使超出content的高度,背景颜色也会自动延伸下去。但IE不识别min-height,所以要加hack(对程序所作的修改,在尚未被开发者接受并集成到正式版本中之前被称为hack)。
 
区别不同浏览器,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 !important;    *background:blue;
原创粉丝点击