zoukankan      html  css  js  c++  java
  • What does !important mean in CSS?

    What does !important mean in CSS?

    What does !important mean in CSS?

    Is it available in CSS 2? CSS 3?

    Where is it supported? All modern browsers?

    回答1

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

    https://www.w3.org/TR/CSS2/cascade.html#important-rules

    6.4.2 !important rules

    CSS attempts to create a balance of power between author and user style sheets. By default, rules in an author's style sheet override those in a user's style sheet (see cascade rule 3).

    However, for balance, an "!important" declaration (the delimiter token "!" and keyword "important" follow the declaration) takes precedence over a normal declaration. Both author and user style sheets may contain "!important" declarations, and user "!important" rules override author "!important" rules. This CSS feature improves accessibility of documents by giving users with special requirements (large fonts, color combinations, etc.) control over presentation.

    Declaring a shorthand property (e.g., 'background') to be "!important" is equivalent to declaring all of its sub-properties to be "!important".

    The first rule in the user's style sheet in the following example contains an "!important" declaration, which overrides the corresponding declaration in the author's style sheet. The second declaration will also win due to being marked "!important". However, the third rule in the user's style sheet is not "!important" and will therefore lose to the second rule in the author's style sheet (which happens to set style on a shorthand property). Also, the third author rule will lose to the second author rule since the second rule is "!important". This shows that "!important" declarations have a function also within author style sheets.

    /* From the user's style sheet */
    p { text-indent: 1em ! important }
    p { font-style: italic ! important }
    p { font-size: 18pt }
    
    /* From the author's style sheet */
    p { text-indent: 1.5em !important }
    p { font: normal 12pt sans-serif !important }
    p { font-size: 24pt }
    

    回答2

    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.

  • 相关阅读:
    PAT A1094 The Largest Generation (25 分)——树的bfs遍历
    PAT A1055 The World's Richest (25 分)——排序
    PAT A1052 Linked List Sorting (25 分)——链表,排序
    PAT A1076 Forwards on Weibo (30 分)——图的bfs
    辅导员
    辅导员面试
    C程序设计
    Excel VBA 基本概念
    Excel函数
    导入excel表的数据到数据库ssh
  • 原文地址:https://www.cnblogs.com/chucklu/p/14215057.html
Copyright © 2011-2022 走看看