!important 在 CSS 中是什么意思? [英] What does !important mean in CSS?
问题描述
!important
在 CSS 中是什么意思?
What does !important
mean in CSS?
它在 CSS 2 中可用吗?CSS 3?
Is it available in CSS 2? CSS 3?
哪里支持?所有现代浏览器?
Where is it supported? All modern browsers?
推荐答案
本质上,它意味着什么;'这很重要,忽略后续规则和任何常见的特殊性问题,应用这个规则!'
It means, essentially, what it says; that 'this is important, ignore subsequent rules, and any usual specificity issues, apply this rule!'
在正常使用中,外部样式表中定义的规则会被文档head
中定义的样式覆盖,而该样式又会被元素本身内的内嵌样式覆盖(假设选择器具有相同的特异性).使用 !important
'attribute' (?) 定义规则会丢弃关于覆盖较早"规则的稍后"规则的正常问题.
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.
此外,通常更具体的规则将覆盖不太具体的规则.所以:
Also, ordinarily, a more specific rule will override a less-specific rule. So:
a {
/* css */
}
通常被以下因素否决:
body div #elementID ul li a {
/* css */
}
由于后一个选择器更具体(通常,在何处找到更具体的选择器(在 head
或外部样式表中)并不重要,它仍然 覆盖非特定选择器(内嵌样式属性将总是覆盖more-"或less-"特定选择器,因为它总是> 更具体.
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 still override the less-specific selector (in-line style attributes will always override the 'more-', or the 'less-', specific selector as it's always more specific.
但是,如果您将 !important
添加到不太具体的选择器的 CSS 声明中,它将具有优先权.
If, however, you add !important
to the less-specific selector's CSS declaration, it will have priority.
使用!important
有其目的(虽然我很难想到它们),但这很像使用核爆炸来阻止狐狸杀死你的鸡;是的,狐狸会被杀死,但鸡也会被杀死.还有附近.
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.
它还使调试 CSS 成为一场噩梦(来自个人、经验、经验).
It also makes debugging your CSS a nightmare (from personal, empirical, experience).
这篇关于!important 在 CSS 中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!