【轉】整理瀏覽器對網頁的兼容性問題(二)——CSS篇
二、 CSS兼容性:
1. CSS中幾種瀏覽器對不同關鍵字的支持,可進行瀏覽器兼容性重復定義
!important 可被FireFox和IE7識別
* 可被IE6、IE7識別
_ 可被IE6識別
*+ 可被IE7識別
2. IE專用的條件注釋
<!--其他瀏覽器 -->
<link rel="stylesheet" type="text/css" href="css.css" />
<!--[if IE 7]>
<!-- 適合于IE7 -->
<link rel="stylesheet" type="text/css" href="ie7.css" />
<![endif]-->
<!--[if lte IE 6]>
<!-- 適合于IE6及一下 -->
<link rel="stylesheet" type="text/css" href="ie.css" />
<![endif]-->
3. 幾個瀏覽器對實際像素的解釋
IE/Opera:對象的實際寬度 = (margin-left) + width + (margin-right)
Firefox/Mozilla:對象的實際寬度= (margin-left) + (border-left-width) + (padding- left) + width + (padding-right) + (border-right-width) + (margin-right)
4. 鼠標手勢問題:FireFox的cursor屬性不支持hand,但是支持pointer,IE兩個都支持;所以為了兼容都用pointer
5. FireFox中設置HTML標簽的Style屬性時,所有位置、寬高和尺寸值必須后跟px,IE也支持此寫法,因此統一加px單位。如 Obj.Style.Height = imgObj.Style.Height + ‘px’;
6. FireFox無法解析簡寫的padding屬性設置,如padding 5px 4px 3px 1px;必須改為 padding-top:5px; padding-right:4px; padding-bottom:3px; padding-left:1px0;
7. 消除ul、ol等列表的縮進時,樣式應寫成:list-style:none;margin:0px;padding:0px;其中margin屬性對IE有效,padding屬性對FireFox有效
8. CSS控制透明:IE:filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=60); FireFox:opacity:0.6;
9. CSS控制圓角:IE:不支持圓角;
FireFox: -moz-border-radius:4px;或
-moz-border-radius-topleft:4px;
-moz-border-radius-topright:4px;
-moz-border-radius-bottomleft:4px;
-moz-border-radius- bottomright:4px;
10. CSS雙線凹凸邊框:IE:border:2px outset;
FireFox:
-moz-border-top-colors: #d4d0c8 white;
-moz-border-left-colors: #d4d0c8 white;
-moz-border-right-colors:#404040 #808080;
-moz-border-bottom-colors:#404040 #808080;
11. IE支持CSS方法cursor:url()自定義光標樣式文件和滾動條顏色風格;FireFox對以上兩者均不支持
12. IE有Select控件永遠處于最上層的bug,且所有CSS對Select控件都不起作用
13. IE支持Form中的Label標簽,包括圖片和文字內容;FireFox不支持包含圖片的Label,點擊圖片不能讓標記 label for 的Radio或CheckBox產生效果
14. FireFox中的TextArea不支持onScroll事件
15. FireFox不支持display的inline和block
16. FireFox對Div設置margin-left, margin-right為auto時已經居中, IE中不行
17. FireFox對Body設置text-align時, Div需要設置margin: auto(主要是margin-left margin-right) 方可居中
18. 對超鏈接的CSS樣式設置最好遵從這樣的順序:L-V-H-A。即
<style type="text/css">
<!--
a:link {}
a:visited {}
a:hover {}
a:active {}
-->
</style>
這樣可以避免一些訪問過后的超鏈接就不具備hover和active樣式了
19. IE中設置長段落自動換行在CSS中設置word-wrap:break-word;FireFox中使用JS插入 的方法來實現,具體代碼如下:
<script type="text/javascript">
/* <![CDATA[ */
function toBreakWord(el, intLen){
var obj=document.getElementById(el);
var strContent=obj.innerHTML;
var strTemp="";
while(strContent.length>intLen){
strTemp+=strContent.substr(0,intLen)+" ";
strContent=strContent.substr(intLen,strContent.length);
}
strTemp+=" "+strContent;
obj.innerHTML=strTemp;
}
if(document.getElementById && !document.all) toBreakWord("div_id", 37);
/* ]]> */
</script>
20. 在子容器加了浮動屬性后,該容器將不能自動撐開
解決方法:在標簽結束后下一個標簽中加上一個清除浮動的CSS clear:both;
21. 浮動后IE6解釋外邊距為實際邊距的雙倍
解決辦法:加上display:inline
22. IE6下圖片下方會有空隙
解決辦法:為img加上display:block或設置vertical-align 屬性為vertical-align:top | bottom |middle |text-bottom
23. IE6下兩個層中間有空隙
解決辦法:設置右側div也同樣浮動float:left或者相對IE6定義 margin-right:-3px;
24. LI中內容超過長度后以省略號的顯示方法
<style type="text/css">
<!--
li {
width:200px;
white-space:nowrap;
text-overflow:ellipsis;
-o-text-overflow:ellipsis;
overflow: hidden;
}
-->
</style>
25. 將元素的高度和行高設為相同值,即可垂直居中文本
<style type="text/css">
<!--
div {
height:30px;
line-height:30px;
}
-->
</style>
26. 對齊文本與文本輸入框,須在CSS中增加vertical-align:middle;屬性設置
<style type="text/css">
<!--
… …
vertical-align:middle;
}
-->
</style>
27. 支持WEB標準的瀏覽器設置了固定高度值就不會像IE6那樣被撐開,但是又想設置固定高度又想能夠被撐開呢?解決辦法是去掉height屬性而設置min-height,為了兼容不支持min-height的IE6可以這樣定義:
{
height:auto!important;
height:200px;
min-height:200px;
}
28. web標準中IE無法設置滾動條顏色
解決辦法:在CSS中對body的設置改為對html的
<style type="text/css">
<!--
html {
scrollbar-face-color:#f6f6f6;
scrollbar-highlight-color:#fff;
scrollbar-shadow-color:#eeeeee;
scrollbar-3dlight-color:#eeeeee;
scrollbar-arrow-color:#000;
scrollbar-track-color:#fff;
scrollbar-darkshadow-color:#fff;
}
-->
</style>
29. IE6由于默認行高問題無法定義1px左右高度的容器,
解決辦法:在CSS中對容器設置如:overflow:hidden | zoom:0.08 | line-height:1px
30. 給Flash設置透明屬性可使層顯示在Flash之上
<param name="wmode" value="transparent" /> <!-- 解決IE上的問題 //>
<embed wmode="transparent" …… > <!-- 解決FireFox上的問題 //>
31. FireFox設置Padding屬性后會相應的增加Width和Height屬性值,IE不會
解決辦法:用!important方法多定義一套Height和Width
32. FireFox對div與div之間的空格是忽略的,但IE是處理的;因此盡量在兩個相連的div之間不要有空格和回車,否則可能會造成不同瀏覽器之間格式不正確,比如著名的3px偏差;而且原因很難查明
33. 形如如下格式
<div id="parent">
<div id="content"> </div>
</div>
當Content內容多時,即使parent設置了高度100%或auto,在不同瀏覽器下還是不能完好的自動伸展;解決辦法在層的最下方產生一個高度為1的空格,代碼如下
<div id="parent">
<div id="content"> </div>
<div style="font: 0px/0px sans-serif;clear: both;display: block"> </div>
</div>
34. IE和FireFox對字體small的尺寸解釋不同,FireFox為13px,IE中為16px
35. IE和FireFox對空格的尺寸解釋不同,FireFox為4px,IE中為8px
文止于此,由于作者才疏學淺,文中紕漏在所難免,歡迎大家批評指正、相互交流。