兼容IE6/IE7/IE8/FireFox的CSS
发布时间:11-28
来源:秋风网络工作室
浏览次数:13如果你只是为了兼容ie7和8,其实可以在里加上这样一条代码:
/TR/xhtml1/DTD/xhtml1-transitional.dtd">
/1999/xhtml">
即可免去你大量修改代码. 注意: IE8兼容IE7的模式
IE8 最新css hack:
"9" 例:"margin:0px auto9;".这里的"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
/**//*/
p{color:#f00;}
/**/ 支持 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;} 支持 IE7 IE8 不支持 FF IE6
p {*color:#f00;} 支持 IE7 IE6 不支持 FF IE8
* html p {color:#f00;} 支持 IE6 不支持 FF IE7 IE8
=========================================================
一、CSS HACK
以下两种方法几乎能解决现今所有HACK.
1, !important
随着IE7对!important的支持, !important 方法现在只针对IE6的HACK.(注意写法.记得该声明位置需要提前.)
#wrapper
{
width: 100px!important; /* IE7 FF */
width: 80px; /* IE6 */
}
=========================================================
2, IE6/IE77对FireFox
* html 与 *html 是IE特有的标签, firefox 暂不支持.而* html 又为 IE7特有标签.
#wrapper
{
#wrapper { width: 120px; } /* FireFox */
*html #wrapper { width: 80px;} /* ie6 fixed */
* html #wrapper { width: 60px;} /* ie7 fixed, 注意顺序 */
}
注意:
* html 对IE7的HACK 必须保证HTML顶部有如下声明:
/TR/html4/loose.dtd">
=========================================================
二、通用 float 闭合(非常重要!)
关于 clear float 的原理可参见 [How To Clear Floats Without Structural Markup]
将以下代码加入Global CSS 中,给需要闭合的div加上 class="clearfix" 即可,屡试不爽.
/* Clear Fix */
.clearfix:after {
content:".";
display:block;
height:0;
clear:both;
visibility:hidden;
}
.clearfix {
display:inline-block;
}
/* Hide from IE Mac */
.clearfix {display:block;}
/* End hide from IE Mac */
/* end of clearfix */
=========================================================
三、其他兼容技巧(再次啰嗦)
1, FF下给 div 设置 padding 后会导致 width 和 height 增加, 但IE不会.(可用!important解决)
2, 居中问题.
1).垂直居中.将 line-height 设置为 当前 div 相同的高度, 再通过 vertical-align: middle.( 注意内容不要换行.)
2).水平居中. margin: 0 auto;(当然不是通用)
3, 若需给 a 标签内内容加上 样式, 需要设置 display: block;(常见于导航标签)
4, FF 和 IE 对 BOX 理解的差异导致相差 2px 的还有设为 float的div在ie下 margin加倍等问题.
5, ul 标签在 FF 下面默认有 list-style 和 padding . 最好事先声明, 以避免不必要的麻烦. (常见于导航标签和内容列表)
6, 作为外部 wrapper 的 div 不要定死高度, 最好还加上 overflow: hidden.以达到高度自适应.
7, 关于手形光标. cursor: pointer. 而hand 只适用于 IE.






