IE6/FF/google等浏览器hack的方法有哪些?hack技巧分享

一、IE6 下
a标签嵌套img标签IE下会有边框,那是超链接默认的样式,解决办法:img{border:0 none;}
1、终极方法:条件注释



缺点是在IE浏览器下可能会增加额外的HTTP请求数 。
2、CSS选择器区分
IE6不支持子选择器;先针对IE6使用常规申明CSS选择器,然后再用子选择器针对IE7 及其他浏览器 。
/* IE6 专用 */
.content {color:red;}
/* 其他浏览器 */
div>p .content {color:blue;} -->
3、PNG半透明图片的问题
虽然可以通过JS等方式解决,但依然存在载入速度等问题,所以,这个在设计上能避免还是尽量避免为好 。以达到网站最大优化 。
4、IE6下的圆角
IE6不支持CSS3的圆角属性,性价比最高的解决方法就是用图片圆角来替代,或者放弃IE6的圆角 。
5、IE6背景闪烁
如果你给链接、按钮用CSS sprites作为背景,你可能会发现在IE6下会有背景图闪烁的现象 。造成这个的原因是由于IE6没有将背景图缓存,每次触发hover的时候都会重新加载,可以用JavaScript设置IE6缓存这些图片:
document.execCommand("BackgroundImageCache",false,true);
6、最小高度
IE6 不支持min-height属性,但它却认为height就是最小高度 。解决方法:使用ie6不支持但其余浏览器支持的属性!important 。
#container {min-height:200px; height:auto !important; height:200px;}
7、最大高度
//直接使用ID来改变元素的最大高度
var container = document.getElementById('container');
container.style.height = (container.scrollHeight > 199) ? "200px" : "auto";
//写成函数来运行
function setMaxHeight(elementId, height){
var container = document.getElementById(elementId);
container.style.height = (container.scrollHeight > (height - 1)) ? height"px" : "auto";
}
//函数示例
setMaxHeight('container1', 200);
setMaxHeight('container2', 500);
8、100% 高度
在IE6下,如果要给元素定义100%高度,必须要明确定义它的父级元素的高度,如果你需要给元素定义满屏的高度,就得先给html和body定义height:100%; 。
9、最小宽度
同max-height和max-width一样,IE6也不支持min-width 。
//直接使用ID来改变元素的最小宽度
var container = document.getElementById('container');
container.style.width = (container.clientWidth < width) ? "500px" : "auto";
//写成函数来运行
function setMinWidth(elementId, width){
var container = document.getElementById(elementId);
container.style.width = (container.clientWidth < width) ? width"px" : "auto";
}
//函数示例
setMinWidth('container1', 200);
setMinWidth('container2', 500);
10、最大宽度
【IE6/FF/google等浏览器hack的方法有哪些?hack技巧分享】//直接使用ID来改变元素的最大宽度
var container = document.getElementById(elementId);
container.style.width = (container.clientWidth > (width - 1)) ? width"px" : "auto";
//写成函数来运行
function setMaxWidth(elementId, width){
var container = document.getElementById(elementId);
container.style.width = (container.clientWidth > (width - 1)) ? width"px" : "auto";
}
//函数示例
setMaxWidth('container1', 200);
setMaxWidth('container2', 500);
11、双边距Bug
当元素浮动时,IE6会错误的把浮动方向的margin值双倍计算 。个人觉得较好解决方法是避免float和margin同时使用 。
12、清除浮动
如果你想用div(或其他容器)包裹一个浮动的元素,你会发现必须给div(容器)定义明确的height、width、overflow之中一个属性(除了auto值)才能将浮动元素严实地包裹 。
#container {border:1px solid #333; overflow:auto; height:100%;}
#floated1 {float:left; height:300px; width:200px; background:#00F;}

推荐阅读