兼容IE6/IE7/IE8/FF的CSS hack写法
成功志
兼容IE6/IE7/IE8/FF的CSS hack写法
2012-12-20 ok12

 


第一种方法:




微软提供了这样一个代码:

<meta http-equiv="x-ua-compatible" content="ie=7" />

把这段代码放到<head>里面,在ie8里面的页面解析起来就跟ie7一模一样的了,所以,基本上可以无视ie8,剩下的代码只需要这样写就可以了


background:#ffc; /*  对firefox有效*/

*background:#ccc; /* 对ie7有效 */

_background:#000; /* 只对ie6有效 */


方法解释:


firefox能解析第一段,后面的两个因为前面加了特殊符号“*”和“_”,firefox认不了,所以只认background:#ffc,看到的是黄色;


ie7前两行都能认,以最后的为准,所以最后解析是background:#ccc,看到的是灰色;


ie6三段都能认,而且“_”这个只有ie6能认,所以最后解析是_background:#000,看到的是黑色


ps:如果发现这样写有问题的话,请查看一下你的html头,看看<head>之前的内容是不是这样的标准写法


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">


这个是现在比较规范的写法,非这种规范写法的,兼容性不能保证


 


第二种方法:


 


要求苛刻的朋友是不愿意在页面头部增加<meta http-equiv="x-ua-compatible" content="ie=7" />这样一句代码的,因为这样的结果是每个页面都得加。那么要想兼容这几个浏览器还可以用以下方法。


以下是兼容IE6/IE7/IE8/FF的写法,注意下面的顺序不可颠倒


margin-bottom:40px;       /*ff的属性*/

margin-bottom:140px\9;    /* IE6/7/8的属性 */

color:red\0;              /* IE8支持 */

*margin-bottom:450px;     /*IE6/7的属性*/


下面以一个实例的形式表现,大家可以运行代码查看一下效果


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>兼容ie6,ie7以及firefox的css透明滤镜,文字不继承其透明属性</title>

<style type="text/css">

 #container{ border:1px solid #c00; background-color:rgba(212,0,0,0.5); background:#f00\9; filter:alpha(opacity=50); width:500px; margin:40px auto; line-height:200%; font-size:14px; padding:14px;}

 #container *{ position:relative;}

</style>

</head>

<body>

 <div id="container">

     <span>我是内容我是内容我是内容我是内容</span>

    </div>

</body>

</html>


 


另一例子:


 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>兼容ie6,ie7,ie8,ff的CSS HACK写法</title>

<style type="text/css">

#test {

 border:2px solid #00f;     /*ff的属性*/

 border:2px solid #090\9;   /* IE6/7/8的属性 */

 border:2px solid #F90\0;     /* IE8支持 */

 _border:2px solid #f00;     /*IE6的属性*/

  /*上下顺序不可以写错*/

 }

 </style>

</head>

<body>

<div id="test">

  <ul>

    <li>FF下蓝边</li>

    <li>IE6下红边</li>

    <li>IE7下绿边</li>

    <li>IE8下黄边</li>

  </ul>

</div>

</body>

</html>

发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容