!重要还是!在IE6和7中不重要?

时间:2012-11-21 08:49:22

标签: css internet-explorer

我的一位朋友说,在级联样式表中!键之后使用哪个词并不重要,只要它是Internet Explorer 6或7,它仍然会获得额外的重量。

这意味着我可以写!notimportant,但它仍然被认为是重要的。我对这个帖子的问题是:这是真的吗?

我没有自己尝试,因为我没有任何Windows机器(不在工作或家中)。虽然知道会很有趣。

提前致谢。

1 个答案:

答案 0 :(得分:5)

这是正确的。 IE6 / IE7错误地将!后跟任何标识符和空格视为!important令牌,而不是完全忽略声明。一些参考文献:

规范声明!只能跟不区分大小写的important一起使用可选的空格和注释将它们分开,以便将其识别为重要声明。您可以在grammar

中看到这一点
"!"({w}|{comment})*{I}{M}{P}{O}{R}{T}{A}{N}{T}  {return IMPORTANT_SYM;}

所以虽然这些是有效的重要声明:

background: transparent !important;
background: transparent !IMPORTANT;
background: transparent !ImPoRtAnT;
background: transparent ! important;
background: transparent !   important;
background: transparent !   /**/important;
background: transparent ! /**/ /**/ important;
background: transparent !/**/important;

这些是无效的,应该被忽略(甚至不尝试应用背景):

background: transparent !notimportant;
background: transparent !NOTIMPORTANT;
background: transparent !NoTiMpOrTaNt;
background: transparent ! notimportant;
background: transparent !   notimportant;
background: transparent !ie7;
background: transparent !abc;
background: transparent !_;

但IE6 / 7会将所有视为有效的!important语句,并应用背景知识。 IE8及更高版本将正确地忽略它们,尽管在兼容性视图中这些版本可能会或可能不会重现。

请注意,虽然Jigsaw W3C CSS Validator报告!/**/important无效(!后面紧跟注释而不是空格),但这是验证程序解析器的错误。根据语法,它绝对有效。