你如何阅读 CSS 中的 !important ?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7369216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you read !important in CSS?
提问by Randomblue
How is the CSS attribute property !important
read?
CSS 属性属性是如何!important
读取的?
Is it really important
, exclamation mark important
, ...?
难道really important
,exclamation mark important
......?
Answer:From the answers below, it seems to be read simply important
, or bang important
.
答:从下面的回答来看,好像读起来很简单important
,或者bang important
。
回答by Jason Gennaro
an "!important" declaration (the delimiter token "!" and keyword "important" follow the declaration) takes precedence over a normal declaration.
"!important" 声明(分隔符标记“!”和关键字“important”跟在声明之后)优先于普通声明。
http://www.w3.org/TR/CSS2/cascade.html#important-rules
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.
基本上,在两个样式规则相同的情况下......它赋予了一个!important
更重要的重要性,并将应用这些样式。
Example
例子
div{
opacity:0 !important;
}
div.jason{
opacity:1;
}
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.
注意:!important
当您有两个相同的属性并且其中一个很重要时,IE6 会忽略- 它总是应用最后一个声明,无论它是否被标记为重要。**从@BoltClock下面的评论中添加。
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.
警告:!important
是只应在绝对必要时使用的锤子。几乎总是,最好使用更具体的选择器来实现更大的特异性并按照您想要的方式应用您的样式。 !important
可能会使未来的开发人员很难找到并更改您的代码。
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 (like body
) and make the styles render.
一个很好的用例:!important
非常适合用户定义的样式,用户希望在浏览器中以特定方式操作网站页面(例如将所有背景设为黑色,文本设为黄色)。无需担心特殊性,用户可以为某些元素(如body
)添加样式并渲染样式。
回答by Matt Ball
回答by Joe
body { color: red !important; }
means, in English, "The text-color of red is important".
body { color: red !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.
就 CSS 如何看待它而言,它对该声明应用了更多的“权重”,因此它(远)更有可能是应用的样式。
For an example of this, we can use
例如,我们可以使用
p { color: red; }
p.blue { color: blue; }
Now, any p
with a class of blue
will show blue text, all the others will show red text.
If we change it to this...
现在,任何p
类blue
将显示蓝色文本,所有其他将显示红色文本。如果我们改成这样...
p { color: red !important; }
p.blue { color: blue; }
They will all show red text (even if they have a class of blue
), as we've given more important to the first selector.
它们都将显示红色文本(即使它们有一个 类blue
),因为我们已经给第一个选择器赋予了更重要的意义。
回答by Michael Ressler
I guess I read the ! as "very".
我想我读了!作为“非常”。
p { color: red !important }
I read as "Paragraphs have the color red, which is very important.
我读为“段落的颜色为红色,这非常重要。
回答by Takis Bouyouris
I like to think of it as "NOT important".
我喜欢将其视为“不重要”。
p {
color: red !important; /* The rest is NOT important for this CSS property. */
}
Meaning that everything else from that declaration and on is NOT importantand 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.
这意味着该声明及之后的所有其他内容都不重要,不应考虑在内。这个想法来自“!”的用法。字符作为布尔值在许多编程语言中都没有。这样 !important 在您阅读时就有意义了。