一、含义
针对不同的浏览器写不同的CSS 代码的过程,就叫CSS Hack。
CSS Hack常见的有三种形式:CSS属性Hack、CSS选择符Hack以及IE条件注释Hack, Hack主要针对IE浏览器。
1、属性级Hack:比如IE6能识别下划线”_”和星号” * “,IE7能识别星号” * “,但不能识别下划线”_”,而firefox两个都不能认识。
2、选择符级Hack:比如IE6能识别*html .class{},IE7能识别*+html .class{}或者*:first-child+html .class{}。
3、IE条件注释Hack:IE条件注释是从IE5开始就提供的一种非标准逻辑语句。比如针对所有IE:<!–[if IE]><!–您的代码–><![endif]–>,针对IE6及以下版本:<!–[if lt IE 7]><!–您的代码–><![endif]–>,这类Hack不仅对CSS生效,对写在判断语句里面的所有代码都会生效。
PS:条件注释只有在IE浏览器下才能执行,这个代码在非IE浏览下被当做注释视而不见。可以通过IE条件注释载入不同的CSS、JS、HTML和服务器代码等。
二、常用的CSS Hack
/* CSS属性级Hack */
color:red\9; /* IE6、IE7、IE8、IE9 识别 */
color:red\0; /* IE8、IE9 识别*/
color:red\9\0; /* 仅IE9识别 */
color:red \0; /* 仅IE9识别 */
color:red!important; /* IE6 不识别!important*/
-------------------------------------------------------------
/* CSS选择符级Hack */
*html #demo { color:red;} /* 仅IE6 识别 */
*+html #demo { color:red;} /* 仅IE7 识别 */
body:nth-of-type(1) #demo { color:red;} /* IE9+、FF3.5+、Chrome、Safari、Opera 可以识别 */
head:first-child+body #demo { color:red; } /* IE7+、FF、Chrome、Safari、Opera 可以识别 */
:root #demo { color:red\9; } : /* 仅IE9识别 */
--------------------------------------------------------------
/* IE条件注释Hack */
<!--[if IE]>此处内容只有IE可见<![endif]-->
<!--[if IE 6]>此处内容只有IE6.0可见<![endif]-->
<!--[if IE 7]>此处内容只有IE7.0可见<![endif]-->
<!--[if !IE 7]>此处内容只有IE7不能识别,其他版本都能识别,当然要在IE5以上。<![endif]-->
<!--[if gt IE 6]> IE6以上版本可识别,IE6无法识别 <![endif]-->
<!--[if gte IE 7]> IE7以及IE7以上版本可识别 <![endif]-->
<!--[if lt IE 7]> 低于IE7的版本才能识别,IE7无法识别。 <![endif]-->
<!--[if lte IE 7]> IE7以及IE7以下版本可识别<![endif]-->
<!--[if !IE]>此处内容只有非IE可见<![endif]-->
三、IE6对!important的支持
!important一般用来做区分IE6和Firefox等浏览器的基本Hack手法。因为IE6不支持!important,而Firefox能读懂!important,其改变了样式的优先级。其实IE6在某些情况下,也能认识!important。
例如:代码如下:
<style type="text/css">
.demo{
color:red !important;
color:green;
}
</style><pre name="code" class="html"><div class="demo">www.jb51.net</div>
上面代码在FF下字体为红色、IE6下字体为绿色。说明IE6忽视!important的存在。
代码如下:
<style type="text/css">
.demo{ color:red !important; }
.demo { color:green; }
</style><div class="demo">www.jb51.net</div>
.demo的内容显示为红色,说明IE6是认得!important的。
两种情况的区别就在于:当在一个选择器中,利用!important改变样式优先级的时候,IE6下是无效的,后面的样式覆盖了前面的,!important被彻底无视了,利用!import
四、IE6下的多选择符
多类选择符的写法。例如:
<pre name="code" class="css">#my.c1.c2 { color:red;}
.c1.c2 { color:red;}
以上写法在IE7+/FF/Opera/Safari 等浏览器都支持。
但在IE6中,后一个类名会覆盖前一个类名,也就是说,上例被IE6理解为:
<pre name="code" class="css">#my.c2 { color:red;}
.c2 { color:red;}
同理:#my.c1.c2.c3 { color:red;} IE6理解为 #my.c3 {color:red;}
.c1.c2.c3 { color:red;} <span style="color: rgb(68, 68, 68); font-family: Arial, Helvetica, sans-serif; background-color: rgb(247, 252, 255);">IE6理解为:</span><span style="color: rgb(68, 68, 68); font-family: Arial, Helvetica, sans-serif;">.c3 { color:red; }</span><span style="color: rgb(68, 68, 68); font-family: Arial, Helvetica, sans-serif; background-color: rgb(247, 252, 255);">
</span>
所以开发中用多类来组合实现css效果的时候,注意IE6的这个问题。最好的方法就是,不要用类组合的形式。
part2 —— CSS hack技巧大全
兼容范围:
IE:6.0+,FireFox:2.0+,Opera 10.0+,Sarari 3.0+,Chrome
各浏览器常用兼容标记一览表:
|
标记 |
IE6 |
IE7 |
IE8 |
FF |
Opera |
Sarari |
|
[*+><] |
√ |
√ |
X |
X |
X |
X |
|
_ |
√ |
X |
X |
X |
X |
X |
|
\9 |
√ |
√ |
√ |
X |
X |
X |
|
\0 |
X |
X |
√ |
X |
√ |
X |
|
@media screen and (-webkit-min-device-pixel-ratio:0){.bb {}} |
X |
X |
X |
X |
X |
√ |
|
.bb , x:-moz-any-link, x:default |
X |
√ |
X |
√(ff3.5及以下) |
X |
X |
|
@-moz-document url-prefix(){.bb{}} |
X |
X |
X |
√ |
X |
X |
|
@media all and (min-width: 0px){.bb {}} |
X |
X |
X |
√ |
√ |
√ |
|
* +html .bb {} |
X |
√ |
X |
X |
X |
X |
|
游览器内核 |
Trident |
Trident |
Trident |
Gecko |
Presto |
WebKit |
(以上 .bb 可更换为其它样式名)
Tip:
网上很多资料中常常把!important也作为一个hack手段,其实这是一个误区.
实例讲解:
Hack应用情境(一)
适用范围:IE:6.0,IE7.0,IE8.0之间的兼容
实例代码:
.bb{
height:32px;
background-color:#f1ee18;/*所有识别*/
.background-color:#00deff\9; /*IE6、7、8识别*/
+background-color:#a200ff;/*IE6、7识别*/
_background-color:#1e0bd1;/*IE6识别*/
}
/*一个用于展示的class为bb的div标签*/
< div class ="bb"></ div >
Hack应用情境(二)
适用范围:IE:6.0,IE7.0,IE8.0,Firefox之间的兼容
实例说明:
把火狐与IE游览器区分开对了,利用IE中对伪类支持不广泛。(.yourClass,x:-moz-any-link, x:default)注意,这个区分伪类往往IE7也能识别,所以最好还需要把IE7单独识别出来,且此方法对ff3.6 已无效,firefox的区分可以使用@-moz-document url-prefix(){}
实例代码:
.bb{
height:32px;
background-color:#f1ee18;/*所有识别*/
background-color:#00deff\9; /*IE6、7、8识别*/
+background-color:#a200ff;/*IE6、7识别*/
_background-color:#1e0bd1;/*IE6识别*/
}
.bb, x:-moz-any-link, x:default{background-color:#00ff00;}/*IE7 firefox3.5及以下 识别 */
@-moz-document url-prefix(){.bb{background-color:#00ff00;}}/* 仅firefox 识别 */
* +html .bb{background-color:#a200ff;}/* 仅IE7 识别 */
/*一个用于展示的class为bb的div标签*/
< div class ="bb"></ div >
Hack应用情境(三)
适用范围:IE:6.0,IE7.0,IE8.0,Firefox,Safari(Chrome)之间的兼容
实例说明:
我们现在将再次对我们的CSS进行加强了,使其能识别Safari(Chrome)游览器。这是基于它们的内核webkit来识别的,用法为@media screen and (-webkit-min-device-pixel-ratio:0)
实例代码:
.bb{
height:32px;
background-color:#f1ee18;/*所有识别*/
background-color:#00deff\9; /*IE6、7、8识别*/
+background-color:#a200ff;/*IE6、7识别*/
_background-color:#1e0bd1;/*IE6识别*/
}
@media screen and (-webkit-min-device-pixel-ratio:0){.bb{background-color:#f1ee18}}{} /*safari(Chrome) 有效 */
.bb, x:-moz-any-link, x:default{background-color:#00ff00;}/*IE7 firefox3.5及以下 识别 */
@-moz-document url-prefix(){.bb{background-color:#00ff00;}}/*仅firefox 识别*/
* +html .bb{background-color:#a200ff;}/* 仅IE7 识别 */
/*一个用于展示的class为bb的div标签*/
< div class ="bb"></ div >
Hack应用情境(四)
适用范围:IE:6.0+,FireFox:2.0+,Opera 10.0+,Sarari 3.0+,Chrome全兼容
实例代码:
<!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" lang="gb2312">
<head>
<meta http-equiv=Content-Type content="text/html; charset=gb2312"/>
<style type="text/css">
/***************************************** 各游览器兼容CSS **********************************************/
.bb{
height:32px;background-color:#f1ee18;/*所有识别*/
background-color:#00deff\9; /*IE6、7、8识别*/
+background-color:#a200ff;/*IE6、7识别*/
_background-color:#1e0bd1/*IE6识别*/
}
/* Safari(Chrome) 有效 */
@media screen and (-webkit-min-device-pixel-ratio:0){.bb{background-color:#f1ee18}}{}
@media all and (min-width: 0px){
.bb{
background-color:#f1ee18; /*opera and Safari(Chrome) and firefox*/
background-color:#4cac70\0;/* 仅 Opera 有效 */
}
}{}
.bb, x:-moz-any-link, x:default{background-color:#4eff00;/*IE7、Firefox3.5及以下 识别 */}
@-moz-document url-prefix(){.bb{background-color:#4eff00;/*仅 Firefox 识别 */}}
* +html .bb{background-color:#a200ff;}/* 仅IE7 识别 */
/* 一般情况下 我们区分IE7 只用 +background-color 配合 _background-color 就行了 如果必须写 .bb, x:-moz-any-link, x:default 这样的代码区分 Firefox3.5及以下 则谨记此写法对IE7也有效,故在其中要再重写一次 +background-color 或者使用 * +html .bb{background-color:blue;} 方法仅对 IE7 有效。可使用 @-moz-document url-prefix(){} 方法独立区分所有 firefox */
.browsers td{width:8%;text-align:center;padding:8px;}}
.browsercolor{color:#333;font-size:18px;font-weight:bold;}
.ie6{background-color:#1e0bd1}
.ie7{background-color:#a200ff}
.ie8{background-color:#00deff}
.firefox{background-color:#4eff00}
.opera{background-color:#4cac70}
.other{background-color:#f1ee18;}
#tipTable td,#tipTable th{border:1px solid black;width:56px;height:16px;text-align:center;}
#wordTable td{margin-left:8px;}
#firefoxTip{display:none;}
#firefoxTip, x:-moz-any-link, x:default{
display:block;/*IE7 firefox3.5及以下识*/
+display:none /*再区分一次IE7*/
}
@-moz-document url-prefix(){#firefoxTip{display:block; /*仅 firefox 识别 */ }}
#ChromeTip{display:none;}
@media screen and (-webkit-min-device-pixel-ratio:0){#ChromeTip{display:block;}}{} /* safari(Chrome) 有效 */
@media all and (min-width: 0px){#ChromeTip{display:none\0;} /* 仅 Opera 有效 */ }{}
</style>
</head>
<body>
<table class="browsers" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td>IE6</td>
<td></td>
<td>IE7</td>
<td></td>
<td>IE8</td>
<td></td>
<td>Firefox</td>
<td></td>
<td>Opera</td>
<td></td>
<td>Safari(Chrome)</td>
<td></td>
</tr>
<tr class="browsercolor">
<td class="ie6">IE6</td>
<td></td>
<td class="ie7">IE7</td>
<td></td>
<td class="ie8">IE8</td>
<td></td>
<td class="firefox">Firefox</td>
<td></td>
<td class="opera">Opera</td>
<td></td>
<td class="other">Safari(Chrome)</td>
<td></td>
</tr>
</table>
<div class="bb">
<span style="display:none;display:block\0;display:none\9;">
Opera的辨别色是深绿色,Opera游览器很时髦么。
</span >
<span id="firefoxTip">Firefox的辨别色是浅绿色,Firefox是很强大的游览器。</span >
<span id="ChromeTip">
Safari和Chrome的辨别色是金黄色,Safari和Chrome使用的都是Webkit内核
</span >
<!--[if IE 8]>IE8的辨别色是蓝色,新版IE8的功能可是不少呢。<![endif]-->
<!--[if IE 7]>IE7的辨别色是紫色,IE7还可以凑合着用!<![endif]-->
<!--[if IE 6]>IE6的辨别色是红色,不过,IE6可是有点落后了!<![endif]-->
</div>
</body>
</html>
其他兼容技巧
1.IE不认得min-这个定义,如果只用宽度和高度,正常的浏览器里这两个值就不会变,如果只用min-width和min-height的话,IE下面根本等于没有设置宽度和高度。
比如要设置背景图片,这个宽度是比较重要的。要解决这个问题,可以这样:
#box{ width: 80px; height: 35px;}
html>body #box{
width: auto; height: auto; min-width: 80px; min-height: 35px;
}
或是为了让这min一命令在IE上也能用,可以把一个<div> 放到 <body> 标签下,然后为div指定一个类,然后CSS设计:
#container{
min-width: 600px;
width:expression(document.body.clientWidth < 600? “600px”: “auto” );
}
第一个min-width是正常的;但第2行的width使用了Javascript,这只有IE才认得,这也会让你的HTML文档不太正规。
2.清除浮动:
.hackbox{ display:table; //将对象作为块元素级的表格显示}
或者.hackbox{ clear:both;}
或者加入:after(伪对象),设置在对象后发生的内容,通常和content配合使用,IE不支持此伪对象。
#box:after{
content:“.”; display: block; height: 0; clear: both; visibility: hidden;
}
参考地址:http://www.duitang.com/static/csshack.html
本文详细介绍了CSS Hack的不同类型及其在多种浏览器中的应用方法,包括属性级Hack、选择符级Hack以及IE条件注释Hack,并提供了多个示例代码帮助理解和实践。

1万+

被折叠的 条评论
为什么被折叠?



