下面來介紹一下關於css hack來解決一些多瀏覽器中不相容的問題介紹。
| |
IE6 |
IE7 |
Firefox2+ |
Firefox3+ |
Opera9.5+ |
Safari3.1+ |
選擇符{ *屬性:值; }[1] |
√ |
√ |
× |
× |
× |
× |
選擇符{ _屬性:值; } |
√ |
×[2] |
× |
× |
× |
× |
選擇符,x:-moz-any-link{ 屬性:值; } |
× |
√ |
√ |
√ |
× |
× |
選擇符,x:-moz-any-link,x:default{ 屬性:值; } |
× |
√ |
× |
√ |
× |
× |
@media all and (min-width:0){ 選擇符{屬性:值;} }[3] |
× |
× |
× |
× |
√ |
√ |
@media all and (-webkit-min-device-pixel-ratio:0){ 選擇符{屬性:值;} }[4] |
1] 此處的*號其實也可以是.號、>號、+號和#號。但它們都不屬於CSS2.1規範合法屬性名稱的一部分,所以不能通過驗證。
[2] 當IE7為quriks mode時和IE6為quriks mode解析相同,所以此時這個運算式在IE7中也能生效。這個hack已經應用了很久了,底線_作為屬性名稱的首碼是符合CSS2.1規範文法的,但不存在於其屬性名稱列表中,所以依舊不能通過標準語法驗證。對於多個IE的CSS Hacks,條件注釋是一個很值得考慮的解決方案。另外對於IE8可以關注其新功能和變化,但現在考慮其CSS Hacks問題有點過早,真的有需求可以通過增加<meta http-equiv=”X-UA-Compatible” content=”IE=EmulateIE7″ />來解決。
[3] 如果把運算式中的and和(中間的空白去掉變成@media all and(min-width:0){選擇符{屬性:值;}},那麼Safari3+將無法識別,Opera9.5+則可以。
[4] 這個運算式Opera9.0是支援的。
上面這些CSS Hacks僅僅是滄海一粟,是一些簡單的通用的CSS Hacks。這個世界上還存在很多很知名很經典的CSS Hacks
在這裡我就不介紹了。
國外網站參考
The syntax for conditional comments is as follows:
Positive
| 代碼如下 |
複製代碼 |
<!--[if condition]> HTML <![endif]--> Negative <!--[if !condition]><![IGNORE[--><![IGNORE[]]> HTML <!--<![endif]-->
|
condition is one of the following:
IE
Any version of IE
lt IE version
Versions less than version
lte IE version
Versions less than or equal to version
IE version
Only version version
gte IE version
Versions greater than or equal to version
gt IE version
Versions greater than version
version is the version of Internet Explorer, typically 5, 5.5, 6, or 7
HTML is the HTML to be included if the condition does or doesn't match, depending on the type of conditional comment. When included, the HTML is placed right where the conditional comment is in the source.
For negative conditions,
| 代碼如下 |
複製代碼 |
| <![IGNORE[--><![IGNORE[]]> can be replaced with --> |
if the condition is simply IE. The longer version is only needed when Internet Explorer might parse the contents.
The <![IGNORE[ ... ]]> directive is not available in XML, so it is illegal to use it in XHTML. A solution would be to split it up into two special conditional comments: <!--[if !condition]> XHTML <![endif]--> <!--[if !IE]>--> XHTML <!--<![endif]--> where XHTML is the same both places. Note that Internet Explorer 7 and below don't yet recognize XHTML as a form of XML, so this is merely forward-looking.
Conditional comments as a CSS hack
Up
Conditional comments can be used as a CSS hack by including links to stylesheets based on the layout engine. Here is an example of how stylesheets can be separated in this way:
| 代碼如下 |
複製代碼 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> <head> <title>Test</title> <link href="all_browsers.css" rel="stylesheet" type="text/css"> <!--[if IE]> <link href="ie_only.css" rel="stylesheet" type="text/css"> <![endif]--> <!--[if lt IE 7]> <link href="ie_6_and_below.css" rel="stylesheet" type="text/css"> <![endif]--> <!--[if !lt IE 7]><![IGNORE[--><![IGNORE[]]> <link href="recent.css" rel="stylesheet" type="text/css"> <!--<![endif]--> <!--[if !IE]>--> <link href="not_ie.css" rel="stylesheet" type="text/css"> <!--<![endif]--> </head> <body> <p>Test</p> </body> </html> |
參考地址:http://www.webdevout.net/css-hacks