别说IE6不认识!important
日期:2008-12-27 分类:CSS
别说IE6不认识 !important ,它们只是不够默契。
IE6 对 !important 的支持存在 bug ,有很多人因此作为 IE6 的 hack 。用多了,传多了,很多人就说 IE6 不支持 !important ,给新人带来一些影响。
在《IE6下!important的bug》中已经说了这个问题,现在再举几个例子说明这个问题,以下面的 XHTML 代码为例:
<ul id="list">
<li class="first">这里是第一行文字</li>
<li>那这里就是第二行文字咯</li>
<li>这里应该是第三行里吧</li>
<li>这里绝对是第四行了</li>
</ul>
<li class="first">这里是第一行文字</li>
<li>那这里就是第二行文字咯</li>
<li>这里应该是第三行里吧</li>
<li>这里绝对是第四行了</li>
</ul>
第一个例子的 CSS 代码:
ul{
margin:20px auto;
padding:0;
width:500px;
list-style:none;
line-height:24px;
}
#list li{
color:blue;
}
.first{
color:red !important;
}
margin:20px auto;
padding:0;
width:500px;
list-style:none;
line-height:24px;
}
#list li{
color:blue;
}
.first{
color:red !important;
}
在 IE6 里,第一行文字为红色,!important 起作用了。如果 .first 不加 !important ,那么在所有浏览器中第一行都显示蓝色,因为 #list li 的权重比 .fisrt 高。当然 .first 改写成 #list .first 会更好,但为了举例子,所以不这样写。
第二个例子的 CSS 代码:
ul{
margin:20px auto;
padding:0;
width:500px;
list-style:none;
line-height:24px;
}
.first{
color:red !important;
}
.first{
color:blue;
}
margin:20px auto;
padding:0;
width:500px;
list-style:none;
line-height:24px;
}
.first{
color:red !important;
}
.first{
color:blue;
}
同样,!important 在 IE6 里起效果了。
再看第三个例子的 CSS 代码:
ul{
margin:20px auto;
padding:0;
width:500px;
list-style:none;
line-height:24px;
}
.first{
color:red !important;
color:blue;
}
margin:20px auto;
padding:0;
width:500px;
list-style:none;
line-height:24px;
}
.first{
color:red !important;
color:blue;
}
这次 !important 在 IE6 里就不起效果了。
综合上面三个例子得出这么一个结论:在同一个选择器内有重复的属性的话,此选择器内的此属性的 !important 在 IE6 里无效。
所以,并不能说 IE6 不支持 !important ,只是支持的不够好。
我也发现了,汗..
实在是不想弄IE6的效果,之前弄过,搞得IE7和FF等其他浏览器显示的都不好,用HACK还接触不深..
其实就是懒啦!
原来如此,最近才懂要开始使用!important..
谢谢指点.
我比较少用到 !important 。
另:你的博客我这里在 IE6 里右边掉下来了。。。
受益匪浅,谢谢。 现在知道了。 : )
因为有很多地方都说 IE6 不支持 !important ,所以写出来希望大家重新认识这个问题。