我可以覆盖!重要吗? [英] Can I override !important?

查看:138
本文介绍了我可以覆盖!重要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试在元素上设置这个CSS

  background:red! 

所以当我尝试这样做。

  background:yellow; 

(我没有使用外部css)它仍然只显示红色而不是黄色



我要求的是如何覆盖它,是否可能?

解决方案

回答是 !重要可以重写,但不能覆盖!important



但是它可以被更高的特异性覆盖!important 宣言。



Firefox解析器中的此代码段将解释


What I am trying is setting this CSS on element

background: red !important;   

so when I try to do this.

 background: yellow;  

(I am not using external css)it still only shows the red and not the yellow for that one field as I would like it to be.

What I am asking is how to override it, is it possible?

解决方案

Ans is YES !important can be overridden but you can not override !important by a normal declaration. It has to be higher specificity than all other declarations.

However it can be overridden with a higher specificity !important declaration.

This code snippet in Firefox's parser will explain how it works:

if (HasImportantBit(aPropID)) {
  // When parsing a declaration block, an !important declaration
  // is not overwritten by an ordinary declaration of the same
  // property later in the block.  However, CSSOM manipulations
  // come through here too, and in that case we do want to
  // overwrite the property.
  if (!aOverrideImportant) {
    aFromBlock.ClearLonghandProperty(aPropID);
    return PR_FALSE;
  }
  changed = PR_TRUE;
  ClearImportantBit(aPropID);
}

Good read


Here's an example to show how to override CSS

HTML

<div id="hola" class="hola"></div>

CSS

div { height: 100px; width: 100px; }
div { background-color: green !important; }
.hola{    background-color:red !important; }
#hola{    background-color:pink !important;}

and output will be

Also we can not override inline !important

HTML

<div id="demo" class="demo" style="background-color:yellow !important;"></div>

CSS

div { height: 100px; width: 100px; }
div { background-color: green !important; }
.demo{    background-color:red !important; }
#demo{    background-color:pink !important;}

the output is

这篇关于我可以覆盖!重要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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