什么!在CSS中的重要意义? [重复]

本文翻译自:What does !important mean in CSS? [duplicate]

Possible Duplicate: 可能重复:
What are the implications of using “!important” in CSS? 在CSS中使用“!important”有什么含义?
How do you read !important in CSS? 你如何阅读!在CSS中很重要?

What does !important mean in CSS? 什么!important在CSS中的!important意义?

Is it available in CSS 2? 它在CSS 2中可用吗? CSS 3? CSS 3?

Where is it supported? 它在哪里支持? All modern browsers? 所有现代浏览器?


#1楼

参考:https://stackoom.com/question/cn8b/什么-在CSS中的重要意义-重复


#2楼

It means, essentially, what it says; 它本质上意味着它所说的; that 'this is important, ignore subsequent rules, and any usual specificity issues, apply this rule!' “这很重要,忽略后续规则,任何通常的特殊性问题,都适用这条规则!”

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). 在正常使用中,外部样式表中定义的规则被文档head中定义的样式否决,而该样式又被元素本身内的样式所取代(假设选择器具有相同的特异性)。 Defining a rule with the !important 'attribute' (?) discards the normal concerns as regards the 'later' rule overriding the 'earlier' ones. 使用!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 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. 由于后一个选择器更具体(并且它通常不重要,找到更具体的选择器(在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; 使用!important有其目的(虽然我很难想到它们),但它就像使用核爆炸阻止狐狸杀死你的鸡一样; yes, the foxes will be killed, but so will the chickens. 是的,狐狸会被杀死,但鸡也会被杀死。 And the neighbourhood. 和邻居。

It also makes debugging your CSS a nightmare (from personal, empirical, experience). 它还使调试CSS成为一场噩梦(来自个人,经验,经验)。


#3楼

!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. 不能说它更好。


#4楼

It changes the rules for override priority of css cascades. 它改变了css级联的覆盖优先级规则。 See the CSS2 spec . 请参阅CSS2规范


#5楼

The !important rule is a way to make your CSS cascade but also have the rules you feel are most crucial always be applied. !important规则是一种制作CSS级联的方法,但也包含您认为最重要的规则。 A rule that has the !important property will always be applied no matter where that rule appears in the CSS document. 无论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. 此外,这是你不想经常使用的东西,因为如果你和其他人一起工作,你可以覆盖其他属性。


#6楼

It is used to influence sorting in the CSS cascade when sorting by origin is done. 当按原点排序时,它用于影响CSS级联中的排序。 It has nothing to do with specificity like stated here in other answers. 它与其他答案中所述的特异性无关。

Here is the priority from lowest to highest: 以下是从最低到最高的优先级:

  1. browser styles 浏览器样式
  2. user style sheet declarations (without !important) 用户样式表声明(没有!重要)
  3. author style sheet declarations (without !important) 作者样式表声明(没有!重要)
  4. !important author style sheets !重要的作者样式表
  5. !important user style sheets !重要的用户样式表

After that specificity takes place for the rules still having a finger in the pie. 之后 ,仍然有一个手指在馅饼中的规则发生了特殊性。

References: 参考文献:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值