!important在CSS中是什么意思? [英] What does !important mean in CSS?

查看:105
本文介绍了!important在CSS中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

!important在CSS中是什么意思?

在CSS 2中可用吗? CSS 3?

在哪里支持?所有现代的浏览器?

解决方案

从本质上讲,它意味着什么;

这很重要,请忽略后续规则以及任何常见的特殊性问题,请应用规则!"

在正常使用中,外部样式表中定义的规则被文档的head中定义的样式所取代,而该样式又被元素本身内的内联样式所取代(假设选择器).用!important'attribute'(?)定义规则将放弃关于后继"规则取代早先"规则的常规顾虑.

通常,更具体的规则将覆盖较不具体的规则.所以:

 a {
    /* css */
}
 

通常由以下规则否定:

 body div #elementID ul li a {
    /* css */
}
 

由于后一个选择器更具体(通常,在哪里找到更具体的选择器(在head或外部样式表中)并不重要,它将仍然覆盖较不特定的选择器(内嵌样式属性将始终覆盖'more-'或'less-'特定选择器,因为它始终更具体.

但是,如果将!important添加到不太特定的选择器的CSS声明中,它将具有优先级.

使用!important有其目的(尽管我很难想到它们),但这很像使用核爆炸来阻止狐狸杀死您的鸡;是的,狐狸将被杀死,鸡也将被杀死.还有邻居.

这也使调试CSS成为一场噩梦(来自个人,经验和经验).

What does !important mean in CSS?

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!'

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 */
}

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.

If, however, you add !important to the less-specific selector's CSS declaration, it will have priority.

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.

It also makes debugging your CSS a nightmare (from personal, empirical, experience).

这篇关于!important在CSS中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆