!important 在 CSS 中是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9245353/
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
What does !important mean in CSS?
提问by Itay Moav -Malimovka
What does !important
mean in CSS?
!important
在 CSS中是什么意思?
Is it available in CSS 2? CSS 3?
它在 CSS 2 中可用吗?CSS 3?
Where is it supported? All modern browsers?
哪里支持?所有现代浏览器?
采纳答案by David says reinstate Monica
It means, essentially, what it says; that 'this is important, ignore subsequent rules, and any usual specificity issues, apply thisrule!'
本质上,它的意思是它所说的;'这很重要,忽略后续规则和任何通常的特殊性问题,应用此规则!
In normal use a rule defined in an external stylesheet is overruled by a style defined in the head
of the document, which, in turn, is overruled by an in-line style within the element itself (assuming equal specificity of the selectors). Defining a rule with the !important
'attribute' (?) discards the normal concerns as regards the 'later' rule overriding the 'earlier' ones.
在正常使用中,外部样式表中定义的规则会被head
文档中定义的样式覆盖,而文档中定义的样式又会被元素本身内的内嵌样式覆盖(假设选择器具有相同的特异性)。使用!important
“属性”(?)定义规则会丢弃关于“稍后”规则覆盖“较早”规则的正常问题。
Also, ordinarily, a more specific rule will override a less-specific rule. So:
此外,通常,更具体的规则将覆盖不太具体的规则。所以:
a {
/* css */
}
Is normally overruled by:
通常被以下条件否决:
body div #elementID ul li a {
/* css */
}
As the latter selector is more specific (and it doesn't, normally, matter where the more-specific selector is found (in the head
or the external stylesheet) it will stilloverride the less-specific selector (in-line style attributes will alwaysoverride the 'more-', or the 'less-', specific selector as it's alwaysmore specific.
由于后一个选择器更具体(并且通常情况下,在何处找到更具体的选择器(在head
样式表中或外部样式表中)并不重要,它仍然会覆盖不太具体的选择器(内联样式属性将始终覆盖“more-”或“less-”特定选择器,因为它总是更具体。
If, however, you add !important
to the less-specific selector's CSS declaration, it will have priority.
但是,如果您添加!important
到不太具体的选择器的 CSS 声明中,它将具有优先权。
Using !important
has its purposes (though I struggle to think of them), but it's much like using a nuclear explosion to stop the foxes killing your chickens; yes, the foxes will be killed, but so will the chickens. And the neighbourhood.
使用!important
有它的目的(虽然我很难想到它们),但这很像使用核爆炸来阻止狐狸杀死你的鸡;是的,狐狸会被杀死,但鸡也会被杀死。还有小区。
It also makes debugging your CSS a nightmare (from personal, empirical, experience).
它还使调试 CSS 成为一场噩梦(来自个人、经验和经验)。
回答by nicosantangelo
The !important rule is a way to make your CSS cascade but also have the rules you feel are most crucial always be applied. A rule that has the !important property will always be applied no matter where that rule appears in the CSS document.
!important 规则是一种让你的 CSS 级联的方法,但也有你认为最重要的规则总是被应用。无论该规则出现在 CSS 文档的何处,都将始终应用具有 !important 属性的规则。
So, if you have the following:
所以,如果你有以下几点:
.class {
color:red !important;
}
.outerClass .class {
color:blue;
}
the rule with the important will be the one applied (not counting specificity)
具有重要的规则将是应用的规则(不计算特殊性)
I believe !important
appeared in CSS1 so every browser supports it (IE4 to IE6 with a partial implementation, IE7+ full)
我相信!important
出现在 CSS1 中,所以每个浏览器都支持它(IE4 到 IE6 部分实现,IE7+ 完整)
Also, it's something that you don't want to use pretty often, because if you're working with other people you can override other properties.
此外,您不希望经常使用它,因为如果您与其他人一起工作,您可以覆盖其他属性。
回答by Cyclone
!important
is a part of CSS1.
!important
是 CSS1 的一部分。
Browsers supporting it: IE5.5+, Firefox 1+, Safari 3+, Chrome 1+.
支持的浏览器:IE5.5+、Firefox 1+、Safari 3+、Chrome 1+。
It means, something like:
这意味着,类似于:
Use me, if there is nothing important else around!
使用我,如果周围没有其他重要的东西!
Cant say it better.
不能说更好。
回答by Fabian Barney
It is used to influence sorting in the CSS cascade when sorting by origin is done. It has nothing to do with specificity like stated here in other answers.
当按原点排序完成时,它用于影响 CSS 级联中的排序。它与其他答案中所述的特异性无关。
Here is the priority from lowest to highest:
这是从低到高的优先级:
- browser styles
- user style sheet declarations (without !important)
- author style sheet declarations (without !important)
- !important author style sheets
- !important user style sheets
- 浏览器样式
- 用户样式表声明(不带 !important)
- 作者样式表声明(不带 !important)
- !important 作者样式表
- !important 用户样式表
Afterthat specificity takes place for the rules still having a finger in the pie.
在那之后,规则仍然在馅饼中占有一席之地。
References:
参考:
回答by Don Roby
It changes the rules for override priority of css cascades. See the CSS2 spec.
它改变了 css 级联覆盖优先级的规则。请参阅CSS2 规范。