如何阅读 CSS 中的 !important ?
CSS 属性 !important
是如何读取的?
是否真的很重要
、感叹号重要
、...?
答案:从下面的答案来看,它似乎被简单地理解为重要
,或bang important
。
How is the CSS attribute property !important
read?
Is it really important
, exclamation mark important
, ...?
Answer: From the answers below, it seems to be read simply important
, or bang important
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
http://www.w3.org/TR/CSS2/cascade.html #important-rules
基本上,如果两个样式规则相同...它会赋予标记为
!important
的规则更大的重要性,并将应用这些样式。示例
即使第二条规则更具体(一个元素+一个类而不是一个元素),也会应用第一条规则
注意:IE6忽略
!重要
当您有两个相同的属性并且其中一个很重要时 - 它将始终应用最后一个声明,无论它是否被标记为重要。 **添加自 @BoltClock 下面的评论。警告:
!important
是只在绝对必要时才应使用的锤子。几乎总是,最好使用更具体的选择器来实现更高的特异性并按照您想要的方式应用样式。!important
可能会让未来的开发人员很难找到并更改您的代码。一个很好的用例:
!important
非常适合用户定义的样式,在这种情况下,用户希望以自己的特定方式操作网站页面。浏览器(例如将所有背景设为黑色,文本设为黄色)。无需担心特殊性,用户可以向某些元素(如body
)添加样式并使样式呈现。http://www.w3.org/TR/CSS2/cascade.html#important-rules
Basically, where two style rules are the same... it gives the one marked
!important
greater importance and will apply those styles.Example
The first rule would be applied even though the second rule is more specific (one element + one class as opposed to one element)
Note: IE6 ignores
!important
when you have two of the same property and one of them is important - it'll always apply the last declaration, whether or not it was marked important. **Added from @BoltClock's comment below.Warning:
!important
is a hammer that should only be used when absolutely necessary. Almost always, it is better to use more specific selectors to achieve greater specificity and have your styles applied the way you want.!important
can make it very difficult for future developers to find and make changes to your code.One good use case:
!important
is great for user-defined styles, where a user wants to manipulate Web site pages in specific way in his browser (say make all the backgrounds black and the text yellow). Without having to worry about specificity, the user can add styles to certain elements (likebody
) and make the styles render.只是“重要”或“非常重要。”在这种情况下,
!
绝对不是否定。它不是标签,而是关键字。
Just "important" or "bang important." The
!
is definitely not a negation in this case.It's not a tag, it's a keyword.
body { 颜色:红色!重要; }
在英语中的意思是“红色的文本颜色很重要”。就CSS如何看待它而言,它对该声明应用了更多的“权重”,因此它更有可能成为应用的样式。
举个例子,我们可以使用
现在,任何具有
blue
类的p
将显示蓝色文本,所有其他将显示红色文本。如果我们将其更改为这样...
它们都将显示红色文本(即使它们具有蓝色类),因为我们为第一个选择器赋予了更重要的地位。
body { color: red !important; }
means, in English, "The text-color of red is important".In terms of how CSS sees it, it applies more "weight" to that declaration, so it will be (far) more likely to be the applied style.
For an example of this, we can use
Now, any
p
with a class ofblue
will show blue text, all the others will show red text.If we change it to this...
They will all show red text (even if they have a class of
blue
), as we've given more important to the first selector.我喜欢认为它“不重要”。
这意味着该声明及之后的所有其他内容都不重要,不应予以考虑。这个想法来自于“!”的使用。在许多编程语言中,字符作为布尔值 NOT。这样,当您阅读时,!important 就有意义了。
I like to think of it as "NOT important".
Meaning that everything else from that declaration and on is NOT important and should not be taken into account. The idea came from the usage of the "!" character as a boolean NOT in many programming languages. This way the !important makes sense as you read it.
我想我读过!作为“非常”。
我读到“段落的颜色是红色,这非常重要。
I guess I read the ! as "very".
I read as "Paragraphs have the color red, which is very important.