[转]CSS完美兼容IE6/IE7/FF的通用方法

关于CSS对各个浏览器兼容已是老生常谈的问题了, 网络上的教程遍地都是.如下内容没有太多新颖, 纯属我的总结, 但愿能对初学者有必定的帮助.
1、CSS HACK
如下两种方法几乎能解决现今全部HACK.
1, !important
随着IE7对!important的支持, !important 方法如今只针对IE6的HACK.(注意写法.记得该声明位置须要提早.)
<style>
#wrapper
{
width: 100px!important; /* IE7+FF */
width: 80px; /* IE6 */
}
</style>

2, IE6/IE77对FireFox
*+html 与 *html 是IE特有的标签, firefox 暂不支持.而*+html 又为 IE7特有标签.
<style>
#wrapper
{
#wrapper { width: 120px; } /* FireFox */
*html #wrapper { width: 80px;} /* ie6 fixed */
*+html #wrapper { width: 60px;} /* ie7 fixed, 注意顺序 */
}
</style>

注意:
*+html 对IE7的HACK 必须保证HTML顶部有以下声明:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2、万能 float 闭合(很是重要!)
关于 clear float 的原理可参见 [How To Clear Floats Without Structural Markup]
将如下代码加入Global CSS 中,给须要闭合的div加上 class="clearfix" 便可,屡试不爽.
<style>
/* 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 */
</style>
3、其余兼容技巧(再次啰嗦)
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.
相关文章
相关标签/搜索