1. 程式人生 > 實用技巧 >瀏覽器-css相容性

瀏覽器-css相容性

CSS

  1. cursor:hand VS cursor:pointer
    firefox不支援hand,但ie支援pointer
    解決方法: 統一使用pointer

  2. innerText在IE中能正常工作,但在FireFox中卻不行.
    需用textContent。
    解決方法:

1
2
3
4
5
if ( navigator . appName . indexOf ( "Explorer" ) > - 1 ) {
document . getElementById ( 'element' ) . innerText = "my text" ;
} else {
document . getElementById ( 'element' ) . textContent = "my text" ;
}
3. CSS透明
IE:filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=60)。
FF:opacity:0.6。
opacity 透明,子元素會繼承透明屬性。解決方式:

(1)使用 background:rgba(0,0,0,.6) //IE8及以下無效果。
(2)使用定位,背景色與子元素處於同級關係。

  1. css中的width和padding
    在IE7和FF中width寬度不包括padding,在Ie6中包括padding.

  2. FF和IEBOX模型解釋不一致導致相差2px
    box.style{width:100;border 1px;}
    ie理解為box.width = 100
    ff理解為box.width = 100 + 1*2 = 102 //加上邊框2px

解決方法:div{margin:30px!important;margin:28px;}
注意這兩個margin的順序一定不能寫反, IE不能識別!important這個屬性,但別的瀏覽器可以識別。所以在IE下其實解釋成這樣:div{maring:30px;margin:28px}
重複定義的話按照最後一個來執行,所以不可以只寫margin:XXpx!important;

  1. IE5 和IE6的BOX解釋不一致
    IE5下div{width:300px;margin:0 10px 0 10px;}
    div 的寬度會被解釋為300px-10px(右填充)-10px(左填充),最終div的寬度為280px,而在IE6和其他瀏覽器上寬度則是以 300px+10px(右填充)+10px(左填充)=320px來計算的。這時我們可以做如下修改 div{width:300px!important;width /**/:340px;margin:0 10px 0 10px}

  2. ul和ol列表縮排問題
    消除ul、ol等列表的縮排時,樣式應寫成:list-style:none;margin:0px;padding:0px;
    經驗證,在IE中,設定margin:0px可以去除列表的上下左右縮排、空白以及列表編號或圓點,設定padding對樣式沒有影響;在 Firefox 中,設定margin:0px僅僅可以去除上下的空白,設定padding:0px後僅僅可以去掉左右縮排,還必須設定list- style:none才能去除列表編號或圓點。也就是說,在IE中僅僅設定margin:0px即可達到最終效果,而在Firefox中必須同時設定margin:0px、 padding:0px以及list-style:none三項才能達到最終效果。

  3. 元素水平居中問題
    FF: margin:0 auto;

IE: 父級{ text-align:center; }

  1. Div的垂直居中問題
    vertical-align:middle; 將行距增加到和整個DIV一樣高:line-height:200px; 然後插入文字,就垂直居中了。缺點是要控制內容不要換行。

  2. margin加倍的問題
    設定為float的div在ie下設定的margin會加倍。這是一個ie6都存在的bug。解決方案是在這個div裡面加上display:inline;

例如:

相應的css為

1
2
3
4
5

imfloat{

 float : left ;
 margin : 5px ; /*IE下理解為10px*/
 display : inline ; /*IE下再理解為5px*/

}
11. IE與寬度和高度的問題
IE不認得min-這個定義,但實際上它把正常的width和height當作有min的情況來使。這樣問題就大了,如果只用寬度和高度,正常的瀏覽器裡這兩個值就不會變,如果只用min-width和min-height的話,IE下面根本等於沒有設定寬度和高度。

比如要設定背景圖片,這個寬度是比較重要的。要解決這個問題,可以這樣:

1

box{ width: 80px; height: 35px;}html>body #box{ width: auto; height: auto; min-width: 80px; min-height: 35px;}

  1. 頁面的最小寬度
    如上一個問題,IE不識別min,要實現最小寬度,可用下面的方法:

container{ min-width: 600px; width:expression(document.body.clientWidth< 600? “600px”: “auto” );}

第一個min-width是正常的;但第2行的width使用了Javascript,這隻有IE才認得,這也會讓你的HTML文件不太正規。它實際上通過Javascript的判斷來實現最小寬度。

  1. DIV浮動IE文字產生3象素的bug
    左邊物件浮動,右邊採用外補丁的左邊距來定位,右邊物件內的文字會離左邊有3px的間距.

1
2
3
4
5
6
7
8
9
10

< div id = "box" >
< div id = "left" >< / div >
< div id = "right" >< / div >
< / div >
14. IE捉迷藏的問題
當div應用複雜的時候每個欄中又有一些連結,DIV等這個時候容易發生捉迷藏的問題。

有些內容顯示不出來,當滑鼠選擇這個區域是發現內容確實在頁面。

解決辦法:對#layout使用line-height屬性或者給#layout使用固定高和寬。頁面結構儘量簡單。

  1. float的div閉合;清除浮動;自適應高度
    ① 例如:<div id=”floatA”><div id=”floatB”><div id=”NOTfloatC”>

這裡的NOTfloatC並不希望繼續平移,而是希望往下排。(其中floatA、floatB的屬性已經設定為float:left;)

這段程式碼在IE中毫無問題,問題出在FF。原因是NOTfloatC並非float標籤,必須將float標籤閉合。在<div class=”floatB”><div class=”NOTfloatC”>之間加上<div class=”clear”>這個div一定要注意位置,而且必須與兩個具有float屬性的div同級,之間不能存在巢狀關係,否則會產生異常。並且將clear這種樣式定義為為如下即可:.clear{clear:both;}

②作為外部 wrapper 的 div 不要定死高度,為了讓高度能自適應,要在wrapper裡面加上overflow:hidden; 當包含float的box的時候,高度自適應在IE下無效,這時候應該觸發IE的layout私有屬性(萬惡的IE啊!)用zoom:1;可以做到,這樣就達到了相容。
例如某一個wrapper如下定義:

.colwrapper{overflow:hidden; zoom:1; margin:5px auto;}

③對於排版,我們用得最多的css描述可能就是float:left.有的時候我們需要在n欄的float div後面做一個統一的背景,譬如:

1
2
3
4
5
6
7
< div id =” page” >

< div id =” left” >< / div >
< div id =” center” >< / div >
< div id =” right” >< / div >

< / div >
比如我們要將page的背景設定成藍色,以達到所有三欄的背景顏色是藍色的目的,但是我們會發現隨著left center right的向下拉長,而page居然儲存高度不變,問題來了,原因在於page不是float屬性,而我們的page由於要居中,不能設定成float,所以我們應該這樣解決:

1
2
3
4
5
6
7
8
9
10
11
< div id =” page” >

< div id =” bg” style =” float : left ; width : 100 %” >

< div id =” left” >< / div >
< div id =” center” >< / div >
< div id =” right” >< / div >

< / div >

< / div >
再嵌入一個float left而寬度是100%的DIV解決之。

或者另一種方法:用選擇器(:after)在page之後插入一個空標籤,並清除浮動

1
. page : after { content : "" ; display : table ; clear : both ; }
④萬能float 閉合(非常重要!)

關於 clear float 的原理可參見 [How To Clear Floats Without Structural Markup],將以下程式碼加入Global CSS 中,給需要閉合的div加上class=”clearfix” 即可,屢試不爽。

1
2
3
4
5
6
7
/* 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 */
或者這樣設定:.hackbox{ display:table; //將物件作為塊元素級的表格顯示}

  1. 高度不適應
    高度不適應是當內層物件的高度發生變化時外層高度不能自動進行調節,特別是當內層物件使用margin 或padding時。

例:

1
2
3
4
5

box {background-color:#eee; }

box p {margin-top: 20px;margin-bottom: 20px; text-align:center; }

< div id = "box" >
< p > p物件中的內容< / p >
< / div >
解決技巧:在P物件上下各加2個空的div物件CSS程式碼{height:0px;overflow:hidden;}或者為DIV加上border屬性。

  1. IE6下圖片下有空隙產生
    解決這個BUG的技巧有很多,可以是改變html的排版,或者設定img為display:block或者設定vertical-align屬性為vertical-align:top/bottom/middle/text-bottom 都可以解決.

  2. 對齊文字與文字輸入框
    加上vertical-align:middle;

1
2
3
4
5
6
7
8
9
10

經驗證,在IE下任一版本都不適用,而ff、opera、safari、chrome均OK!

  1. LI中內容超過長度後以省略號顯示
    此技巧適用與IE、Opera、safari、chrom瀏覽器,FF暫不支援。

1
2
3
4
5
6
7
8
9
10
11
12

  1. 為什麼web標準中IE無法設定滾動條顏色了
    解決辦法是將body換成html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
< ! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >
< meta http - equiv = "Content-Type" content = "text/html; charset=gb2312" / >

  1. 為什麼無法定義1px左右高度的容器
    IE6下這個問題是因為預設的行高造成的,解決的技巧也有很多:

例如:overflow:hidden  zoom:0.08   line-height:1px

22.怎麼樣才能讓層顯示在FLASH之上呢

解決的辦法是給FLASH設定透明

  1. 連結(a標籤)的邊框與背景
    a連結加邊框和背景色,需設定 display: block, 同時設定 float: left 保證不換行。參照menubar, 給 a 和menubar設定高度是為了避免底邊顯示錯位, 若不設 height, 可以在menubar中插入一個空格。

  2. 超連結訪問過後hover樣式就不出現的問題
    被點選訪問過的超連結樣式不在具有hover和active了,很多人應該都遇到過這個問題,解決技巧是改變CSS屬性的排列順序: L-V-H-A

Code:

1
2
3
4
5
6
7
8

  1. FORM標籤
    這個標籤在IE中,將會自動margin一些邊距,而在FF中margin則是0,因此,如果想顯示一致,所以最好在css中指定margin和 padding,針對上面兩個問題,我的css中一般首先都使用這樣的樣式ul,form{margin:0;padding:0;}。

  2. 屬性選擇器(這個不能算是相容,是隱藏css的一個bug)
    p[id]{}div[id]{}

這個對於IE6.0和IE6.0以下的版本都隱藏,FF和OPera作用.屬性選擇器和子選擇器還是有區別的,子選擇器的範圍從形式來說縮小了,屬性選擇器的範圍比較大,如p[id]中,所有p標籤中有id的都是同樣式的.

  1. 為什麼FF下文字無法撐開容器的高度
    標準瀏覽器中固定高度值的容器是不會象IE6裡那樣被撐開的,那我又想固定高度,又想能被撐開需要怎樣設定呢?辦法就是去掉height設定min-height:200px; 這裡為了照顧不認識min-height的IE6 可以這樣定義:

1
2
3
4
5
{
height : auto ! important ;
height : 200px ;
min - height : 200px ;
}
28. IE和FireFox 對空格的尺寸解釋不同
FireFox為4px,IE為8px; FireFox對div與div之間的空格是忽略的,但是IE是處理的。因此在兩個相鄰div之間不要有空格跟回車,否則可能造成不同瀏覽間之間格式不正確,比如著名的3px偏差(多個img標籤連著,然後定義float: left;結果在firefox裡面正常,而IE裡面顯示的每個img都相隔了3px。我把標籤之間的空格都刪除都沒有作用。解決方法是在img外面套li,並且對li定義margin: 0; 避免方式:在必要的時候不要無視 list 標籤)而且原因難以查明。

  1. 條件註釋

1
2
3
4
5
6
7
8
9
< link rel = "stylesheet" type = "text/css" href = "css.css" / >

< ! -- [ if IE 7 ] >
< link rel = "stylesheet" type = "text/css" href = "ie7.css" / >
< ! [ endif ] -- >

< ! -- [ if lte IE 6 ] >
< link rel = "stylesheet" type = "text/css" href = "ie.css" / >
< ! [ endif ] -- >
lte — 小於等於
lt — 小於
gte — 大於等於
gt — 大於
! — 不等於

30.強制渲染

1
2
3
4
5
6
7
8
9
10
11
< meta http - equiv = X - UA - Compatible content = IE = EmulateIE7 > //這句話的意思是強制使用IE7模式來解析網頁程式碼!

< meta http - equiv =“ X - UA - Compatible” content =“ IE = 8″ >

< meta http - equiv =“ X - UA - Compatible” content =“ chrome = 1″ / > //Google Chrome Frame也可以讓IE用上Chrome的引擎

< meta http - equiv =“ X - UA - Compatible” content =“ IE = EmulateIE7″ > < !– IE7 mode – > 或者 < meta http - equiv =“ X - UA - Compatible” content =“ IE = 7″ > < !– IE7 mode – > //強制IE8使用IE7模式來解析

< meta http - equiv =“ X - UA - Compatible” content =“ IE = 6″ > < !– IE6 mode – > < meta http - equiv =“ X - UA - Compatible” content =“ IE = 5″ > < !– IE5 mode – > //強制IE8使用IE6或IE5模式來解析

< meta http - equiv =“ X - UA - Compatible” content =“ IE = 5 ; IE = 8″ / > //一個特定版本的IE支援所要求的相容性模式多於一種
31.js相容檔案

使IE5,IE6相容到IE7模式(推薦)

1
2
3
4
5
6
7
8
9
10
11
12
13
< !– [ if lt IE 7 ] >

< ! [ endif ]– >
使 IE5 , IE6 , IE7相容到 IE8模式

< !– [ if lt IE 8 ] >

< ! [ endif ]– >
使 IE5 , IE6 , IE7 , IE8相容到 IE9模式

< !– [ if lt IE 9 ] >

< ! [ endif ]– >
32. 瀏覽器識別符

1
2
3
4
5
6
7
8
9
10
11
12
13

    p
    {
     
    _color
    :
    red
    ;
     
    } 
       
       
        
    IE6
     專用
   

   

    *
    html
     
    p
    {
     
    color
    :
    #red; }  IE6 專用
   

   

    p
    {
     
    +
    color
    :
    red
    ;
     
    } 
       
       
        
    IE6
    ,
    7
     專用
   

   

    p
    {
     
    *
    color
    :
    red
    ;
     
    } 
       
       
        
    IE6
    ,
    7
     專用
   

   

    *
    html
     
    p
    {
     
    color
    :
    red
    ;
     
    } 
      
     
    IE6
    ,
    7
     專用
   

   

    p
    {
    *
    +
    color
    :
     
    red
    ;
    } 
       
       
      
     
    IE7
     專用
   

   

    Body
    >
     
    p
    {
     
    color
    :
     
    red
    ;
     
    }  遮蔽
     
    IE6
   

   

    p
    {
     
    color
    :
    red
    \
    9
    ;
     
    } 
       
       
      
     
    IE8
   

   

      
   

   

    Firefox
    :
     
    -
    moz
    -
   

   

    Safari
    :
     
    -
    webkit
    -
   

   

    Opera
    :
     
    -
    o
    -
   

   

    IE
    :
     
    -
    ms
    -