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.

  • 相关阅读:
    oracle中查询表是否存在
    asp.net webform/mvc导出Excel通用代码
    分享给大家一个500G.Net ftp资料库
    C# 使用TopShelf实现Windows服务部署
    C#基于Quartz.NET实现任务调度并部署Windows服务
    附加进程找不到w3wp.exe进程解决方案
    在vs2015中使用附加进程的方式调试IIS中的页面
    删除datatable的行后,出现“不能通过已删除的行访问该行的信息”的错误,即DeletedRowInaccessibleException
    C# FTP操作类
    C# vb .net实现发光效果
  • 原文地址:https://www.cnblogs.com/chucklu/p/14215057.html
Copyright © 2011-2022 走看看