当前位置: 代码迷 >> HTML/CSS >> 惯用的CSS过滤器
  详细解决方案

惯用的CSS过滤器

热度:165   发布时间:2012-09-10 11:02:32.0
常用的CSS过滤器
几乎都是针对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>


  相关解决方案