Overriding CSS properties for a specific html element

后端 未结 4 2058
星月不相逢
星月不相逢 2020-11-30 13:12

I have the following:

...
相关标签:
4条回答
  • 2020-11-30 13:42

    You could use !important, like this:

    .special-bg {
        background-color: #276a7f !important;
    }
    
    0 讨论(0)
  • 2020-11-30 13:52

    Use this instead:

    .main_section .special-bg {
        background-color: #276a7f;
    }
    

    see this fiddle for example: http://jsfiddle.net/Ckxsw/

    Something like this would be a useful read: http://coding.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/

    0 讨论(0)
  • 2020-11-30 13:53

    That's a CSS specificity issue.

    .main_section article has a higher specificity value than .special-bg selector.

    In terms of value: Inline Style > IDs > Classes, Attributes, and Pseudo-classes > Element Types and Pseudo-elements, So the calculation of Specificity of these two CSS selectors would be:

    .special-bg

    Inline Style    ID   (Pseudo-)Class    (Pseudo-)Element
    0               0    1                 0
    

    .main_section article

    Inline Style    ID   (Pseudo-)Class    (Pseudo-)Element
    0               0    1                 1
    

    11 > 10 => .main_section article selector wins!

    You could use the following:

    .main_section .special-bg {
      /* Styles goes here... */
    }
    

    Further reading:

    • http://coding.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/
    • http://reference.sitepoint.com/css/specificity
    • http://cssspecificity.com/

    And a great tool to calculate the specificity value:

    • http://specificity.keegan.st/
    0 讨论(0)
  • 2020-11-30 14:05

    You can use this css

    .main_section .special-bg{
    
      background-color: #276a7f !important;
    
    }
    
    0 讨论(0)
提交回复
热议问题