兼容IE6/IE7/IE8/FireFox的css hack

本文详细介绍了针对不同浏览器版本的CSS hack技巧,特别是IE8特有的CSS hack方法,帮助开发者实现跨浏览器兼容性。通过特定的代码注释和选择器,可以轻松地为目标浏览器定制样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

.color{

background-color: #CC00FF;

background-color: #FF0000\9;

*background-color: #0066FF;

_background-color: #009933;

}


**记住上面得样式解释为顺序是 ff、ie8、ie7、ie6 **

显示的结果:

用火狐浏览,颜色是紫色

用 IE8 浏览,颜色是红色

用 IE7 浏览,颜色是蓝色

用 IE6 浏览,颜色是绿色

如果你只是为了兼容ie7和8,其实可以在<head>里加上这样一条代码:
<meta http-equiv="X-UA-Compatible" content="IE=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=gb2312" />

<title>无标题文档</title>

<style type="text/css">

.main{

width:200px;color:#fff;

height:400px;background:#f00;

height:200px\9;background:#ff0\9;

*height:200px;*background:#0ff;

_height:100px;_background:#000;

}

</style>

</head>

<body>

<div class="main">ff红色height:400px / IE8黄色height:200px / IE7 蓝色height:200px / IE6黑色height:100px</div>

</body>

</html>

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

微软在IE8提供三种解析页面的模式

  IE8 Standard Modes :默认的最标准的模式,严格按照W3C相关规定

  IE7 Standards Modes :IE7现在用的解析网页的模式,开起机关是在<head>中加入 <meta http-equiv="X-UA-Compatible" content="IE=7">

  Quirks Modes :IE5用的解析网页的模式,开起机关是删除HTML顶部的DOCTYPE声明

  注意:不同模式间的网页在IE8中可以互相 frame ,因此因不会模式下的DOM和CSS渲染不一样,所以会引发很多问题,务必注意如果你的页面对IE7兼容没有问题,又不想大量修改现有代码,同时又能在IE8中正常使用,微软声称,开发商仅需要在目前兼容IE7的网站上添加一行代码即可解决问题,此代码如下:

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

  IE8 最新css hack:

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

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

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

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

一些IE6 IE7 IE8 FF的CSS hack

p{+color:#f00;} 支持 IE6 IE7 不支持FF IE8

p{_color:#f00;} 支持 IE6 不支持FF

p{color:#00f !important;}

p{color:#f00;} 支持 IE7 IE6 FF IE8

p{color:#00f !important;color:#f00;} 支持 IE7 IE8 FF 不支持 IE6

head:first-child+body p{color:#f00;} 支持 IE7 IE8 FF 不支持 IE6

支持 IE8 不支持IE6 IE7 FF

html*p{color:#f00;} 支持 IE6 IE7 不支持FF IE8

html>p{color:#f00;} 支持 IE7 IE8 FF 不支持 IE6

html[xmlns] p{color:#f00;} 支持 IE7 IE8 FF 不支持 IE6

@import 'style.css';

@import "style.css";

@import url(style.css);

@import url('style.css');

@import url("style.css"); 支持 IE7 IE6 FF IE8

p{color:#f00;} 支持 IE6 IE7 FF 不支持 IE8

* html p {color:#f00;} 支持 IE6 不支持FF IE7 IE8

*+html p {color:#f00;} 支持 IE7 IE8 不支持FF IE6

p {*color:#f00;} 支持 IE7 IE6 不支持FF IE8


====================================================

这样写,也许会简单些,很方便,但是这个只是IE8对IE7的兼容模式

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

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

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

注意:<meta http-equiv="x-ua-compatible" content="ie=7"> IE8兼容IE7的模式
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值