Why can't I override existing pseudo-elements?

前端 未结 3 1715
既然无缘
既然无缘 2020-12-30 11:17

I have two CSS rules following each other:

.some td:first-child:before {
    content:url(\'path/to/image.png\')\" \" ;
}
.some .other:before {
    content:ur         


        
3条回答
  •  执念已碎
    2020-12-30 11:37

    The first rule is more specific than the second one, so in a case when both the selectors are valid, the more specific one overrides other.

    Read this article to know how can we overcome such complications of having conflicting styles. To brief them, Here is how specificity are calculated.

    +--------------------+--------------------+-----------------------------------+
    |    Type            |   Specific Value   |  Example                          |
    +--------------------+--------------------+-----------------------------------+
    |  Inline Style      |   1000             | style="color: #f00;"              |
    +--------------------+--------------------+-----------------------------------+
    |  Id                |   100              | #text { color: #f00; }            |
    +--------------------+--------------------+-----------------------------------+
    |  Classes           |   10               | .text { color: #f00; }            |
    +--------------------+--------------------+-----------------------------------+
    |  Pseudo Classes    |   10               | a:hover { color: #f00; }          |
    +--------------------+--------------------+-----------------------------------+
    |  Pseudo Elements   |   10               | a:first-child { color: #f00; }    |
    +--------------------+--------------------+-----------------------------------+
    |  Elements (tag)    |   1                | a { color: #f00; }                |
    +--------------------+--------------------+-----------------------------------+
    

    Basically, Class Selectors are more specific than tag selectors. Lets calculate your specificity

    • For first rule: 31
    • For second rule: 30

    SO the first rule wins.

    You can increase the specificity of the second rule like

    .some tr td.other:before {
        content:url('path/to/image2.png') ;
    }
    

    Its calculate to 33, to override the style first rule.

提交回复
热议问题