2

我的一个朋友说,!在层叠样式表中的键之后使用什么词并不重要,它仍然会增加额外的重量,只要它是 Internet Explorer 6 或 7。

这意味着我可以写作!notimportant并且它仍然被认为是重要的。我对这个线程的问题是:这是真的吗?

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

提前致谢。

4

1 回答 1

5

那是对的。IE6/IE7 错误地将!后跟任何标识符和空格视为!important标记,而不是完全忽略声明。一些参考资料:

规范声明!只能在不区分大小写的情况下important使用可选的空格和将它们分开的注释,以便将其识别为重要声明。您可以在语法中看到这一点:

"!"({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为无效(!紧接着是注释而不是空格),但这是验证器解析器的错误。根据语法,它肯定是有效的。

于 2012-11-21T08:52:21.460 回答