What does !important mean in CSS?

后端 未结 5 1331
后悔当初
后悔当初 2020-11-21 23:32

What does !important mean in CSS?

Is it available in CSS 2? CSS 3?

Where is it supported? All modern browsers?

相关标签:
5条回答
  • 2020-11-22 00:06

    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).

    0 讨论(0)
  • 2020-11-22 00:15

    !important is a part of CSS1.

    Browsers supporting it: 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.

    0 讨论(0)
  • 2020-11-22 00:18

    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.

    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)

    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.

    0 讨论(0)
  • 2020-11-22 00:22

    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.

    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:

    • http://www.w3.org/TR/CSS2/cascade.html#cascade
    • https://russmaxdesign.github.io/maxdesign-slides/02-css/207-css-cascade.html
    0 讨论(0)
  • 2020-11-22 00:29

    It changes the rules for override priority of css cascades. See the CSS2 spec.

    0 讨论(0)
提交回复
热议问题