DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> CSS入門知識 >> DIV十CSS布局 >> 布局實例 >> CSS兼容性問題 && CSS HACK
CSS兼容性問題 && CSS HACK
編輯:布局實例     

 以前做網頁的時候,只考慮 IE6 和 FF 的兼容性,公司換了,要求也高了,FF和IE 6 7 8 要全兼容了,

碰到要單獨Hack IE8的。當然,用注釋非常方便,只要添加相應的注釋就可以解決。但問題是,為了一句CSS寫多一個文件,或者在header上添加注釋,那顯然不是懶人的習慣做法。結論如下:
selector{
property:value; /* 所有浏覽器 */
property:value9; /* 所有IE浏覽器 */
+property:value; /* IE7 */
_property
當然,注意順序。根據CSS的優先性,上面的寫法,分別針對Firefox、IE8、IE7和IE6顯示值。讓我們看看這個:

CSS代碼如下:

p.ie{
height:60px;text-align:center;line-height:60px;border:1px dashed #bbb;background:#f7f7f7;font:15;
color:blue; // 所有浏覽器
color:brown9; // 所有IE浏覽器
+color:red; // IE7
_color:green; // IE6
}

HTML 代碼:

<body style="width:500px;margin:0 auto;">
<p class="ie">
<span style="display:block;display:none9;">嘿嘿,小子竟然也用Firefox,藍色文字。</span>
<!--[if IE 8]>不錯不錯,挺先進的嘛,使用IE8呢!文字是褐色的。<![endif]-->
<!--[if IE 7]>你,IE7,紅色文字!<![endif]-->
<!--[if IE 6]>孩子,雖然顯示的是綠色文字,不過,IE6可不是好東西呢!<![endif]-->
</p>
</body>


注意下面介紹的這些hack寫法僅適用於XHTML1.0。如果沒有在HTML最前加上


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">

那麼效果將不一樣!此外,這裡所說的IE8,不是指IE8的兼容模式,因為IE8的兼容模式其實就是IE7。

區別IE6、7與FF/IE8:
background:blue;*background:orange;

引用
顯示效果:
IE 6/7:orange
FF/IE8:blue
原理:FF/IE8不支持*開頭,而IE6/7都支持。

區別IE6與IE7/IE8/FF:
background:green;_background:blue;

引用
顯示效果:
IE7/8/FF:green
IE6:blue
原理:IE6支持下劃線"_",IE7、8和firefox均不支持下劃線。


區別FF/IE8和IE6/7:

background:orange;+background:green;-background:blue;

或者

background:orange;*background:green!important;*background:blue;

引用
顯示效果:
IE6:blue
IE7:green
FF/IE8:orange
原理:IE6能識別-,IE7能識別+,IE8和FF都不能識別+和-
IE8/FF都不識別*,IE7優先識別!important,IE6不能識別!important。

關於IE8的hacks:

.test{
color:/***/#00f9; /* IE8 only */

color:#00f9; /* 適用於所有IE版本 */
}

可同時區分IE8、IE7、IE6、Firefox的CSS hacks:
.test{
color:#000; /* Firefox */
color:/***/#00f9; /* IE8 */
*color:#f00; /* IE7 */
_color:#0f0; /* IE6 */
}

添加相應的注釋解決兼容性問題

注釋相應的Css文件:

<link rel="stylesheet" type="text/css" href="css/style.css" media="screen" />

<!--[if IE 6]>

<link rel="stylesheet" type="text/css" href="css/IE6style.css" media="screen" />

<![endif]-->

<!--[if IE 7]>

<link rel="stylesheet" type="text/css" href="css/IE7style.css" media="screen" />

<![endif]-->

<!--[if gte IE 8]>

<link rel="stylesheet" type="text/css" href="css/IE8style.css" media="screen" />

<![endif]-->

注釋相應的Css 內容:

<!--[if ie 6]>

<style>

<!--

#warp{ padding-bottom:11px;}

-->

</style>

<![endif]-->

<!--[if ie 7]>

<style>

<!--

#warp{ padding-bottom:11px;}

-->

</style>

<![endif]-->

<!--[if ie 8]>

<style>

<!--

#warp{ padding-bottom:11px;}

-->

</style>

<![endif]-->

 

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved