常用的CSS过滤器

本文详细解析了如何使用CSS条件注释和过滤器来针对不同版本的IE浏览器提供定制化的样式表现,包括如何区分IE5.x、IE6、IE7,并通过CSS hack 实现对IE6及以下版本的特定样式处理。文章还介绍了如何利用条件注释来根据不同IE版本提供不同的CSS规则,从而在减少HTTP请求的同时,确保各种IE版本的浏览器能够获得最佳的用户体验。
摘要由CSDN通过智能技术生成
几乎都是针对IE 6的,毕竟IE 6的BUG最多,更老的浏览器就忽略了。

IE的条件注释

<!-- [if IE]

<style>...</style>

--!>

神奇的是还可以指定IE的版本


<!-- [if IE 6]

<style>...</style>

--!>


说明:这种方式极其有效,而且简单,很多大型网站仍然在使用这种方式。

基于选择器的过滤器

IE6不支持后代选择器和属性选择器,所以利用好浏览器不支持的选择器也是一种方式。但是会造成不同浏览器上用户体验的差异。

星号属性

div{*color:red;}

IE6和IE7可以识别星号属性,但是IE8和标准浏览器都不行。

下划线属性


div{_color:red;}

IE6可以识别下划线属性,但是IE7、IE8和标准浏览器都不行。


!important

!important用于提高规则的优先级,但是IE6在处理单一规则中多个相同属性时存在BUG

#nav{position:fixed !important;position:static;}

IE6会忽略第一个设置而应用第二个。此招数的功能和下划线属性功能相同。

另外,想看更高级更完整的过滤器技巧,请猛击下面的链接:

http://centricle.com/ref/css/filters/


------------------------------------以下为另一篇文章---------------------------------------

CSS Tip: Targeting IE 5.x, 6 and 7 Separately

In rare situations it may be necessary to provide different rules, not only to the Internet Explorer family in general, but also to each individual version. We can combine 3 CSS Hacks to achieve this.
Differentiating between IE 6 and below and IE 7

Firstly we can target IE 6 and IE 7 separately using the underscore hack and far less well documented star property hack (commonly mistaken for the star HTML hack).

.box {
background: #00f; /* all browsers including Mac IE */
*background: #f00; /* IE 7 and below */
_background: #f60; /* IE 6 and below */
padding: 7px;
color: #fff;
}

View Example

In this example all non IE browsers (which also includes Mac IE) see the first background rule. This sets the box colour to blue. Both IE 6 & 7 then see the next rule (prefixed with a star) which overrides the first rule and sets the background colour to red. Finally IE 6 and below also see the final rule (prefixed with an underscore) and set the background colour to orange.

Differentiating between IE 6 and IE 5.x

We can expand on this ruleset by making use of the backslash part of the Simplified Box Model Hack described here. Escaping any letter within the property name which isn't in the range a-f, A-F, 0-9 will hide that rule from IE 5.x. We can therefore define a rule for IE 5.x, which will also be seen by IE 6, and then override that with a backslash commented rule for IE 6.

.box {
background: #00f; /* all browsers including Mac IE */
*background: #f00; /* IE 7 and below */
_background: #0f0; /* IE 6 and below */
_bac\kground: #f60; /* IE 6 only */
padding: 7px;
color: #fff;
}

View Example

The background colour in IE 5.x will now be green rather than the orange specified for IE 6.
Conditional Comments

Of course IE already provides the ability to target different versions via conditional comments. You should generally favour these over the method described above. There are, however, situations where you may want to consider it, such as when the scope of changes are so small that you don't want to incur the overhead of an additional HTTP request if included in an external file or if don't want to include the IE specific rules inline.
Disclaimer

You should always be careful when implementing CSS hacks and first make sure that the problem you're trying to solve is in fact something that needs hacking around and not something you've implemented incorrectly. Tantek Çelik article on the Web Standards Project Website provides interesting reading on the history of hacks and when and when not to use them.

* Back to index
* Bookmark It

Comments

*

Nice hack, you can take this a step further for IE 5.0, and IE 5.5 by using comments in your rules. So your example would be:

.box {
background: #00f; /* all browsers including Mac IE */
*background: #f00; /* IE 7 and below */
_background/**/: #0f0; /* IE 5.0 */
_background:/**/ #f62; /* IE 5.5 only */
_background/**/:/**/ #f61; /* IE 6 only */
padding: 7px;
color: #fff;
}

From: http://www.ejeliot.com/blog/63


<p><!--[if IE]>
According to the conditional comment this is Internet Explorer<br />
<![endif]-->
<!--[if IE 5]>
According to the conditional comment this is Internet Explorer 5<br />
<![endif]-->
<!--[if IE 5.0]>
According to the conditional comment this is Internet Explorer 5.0<br />
<![endif]-->
<!--[if IE 5.5]>
According to the conditional comment this is Internet Explorer 5.5<br />
<![endif]-->
<!--[if IE 6]>
According to the conditional comment this is Internet Explorer 6<br />
<![endif]-->
<!--[if IE 7]>
According to the conditional comment this is Internet Explorer 7<br />
<![endif]-->
<!--[if gte IE 5]>
According to the conditional comment this is Internet Explorer 5 and up<br />
<![endif]-->
<!--[if lt IE 6]>
According to the conditional comment this is Internet Explorer lower than 6<br />
<![endif]-->
<!--[if lte IE 5.5]>
According to the conditional comment this is Internet Explorer lower or equal to 5.5<br />
<![endif]-->
<!--[if gt IE 6]>
According to the conditional comment this is Internet Explorer greater than 6<br />
<![endif]-->
</p>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值