我的一个 friend 说,无论您在层叠样式表中的 !
键后使用什么单词,它仍然会增加额外的重量,只要它是 Internet Explorer 6或 7。
这意味着我可以写 !notimportant
并且它仍然会被认为是重要的。我对此主题的问题是:这是真的吗?
我无法自己尝试,因为我没有任何 Windows 计算机(不在工作或家里)。不过,了解一下会很有趣。
提前致谢。
最佳答案
这是正确的。 IE6/IE7 错误地将 !
后跟任何标识符和空格视为 !important
标记,而不是完全忽略该声明。一些引用资料:
- http://www.javascriptkit.com/dhtmltutors/csshacks3.shtml
- Bang IE7 - What does it mean? (在该网站上有
!ie7
示例以及上述引用文献的链接)
规范规定 !
后面只能跟不区分大小写的 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 及更高版本将正确忽略它们,尽管这可能会也可能不会在兼容性 View 中的这些版本中重现。
请注意,虽然 Jigsaw W3C CSS Validator将 !/**/important
报告为无效(!
后紧跟注释而不是空格),这是验证器解析器的错误。根据语法它肯定是有效的。
关于css - !important 或 !notimportant 在 IE6 和 7 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13489438/