How do CSS specificity levels between classes/pseudo-classes and elements/pseudo-elements work?

前端 未结 2 1110
遇见更好的自我
遇见更好的自我 2021-01-12 00:07

I\'m using the following definitions (adapted from the CSS2 spec http://www.w3.org/TR/CSS21/cascade.html#specificity )

  • a = using the style attribute on an elem
相关标签:
2条回答
  • 2021-01-12 00:51

    I guess that :first-line is more specific than just .content. So the first line of the text is pink, but the bullet of the list is green (and yellow on hover). Everything is good, as for me.

    Imagine that the :first-line selector is just a nested text node selector, like:

    <li class="content">
         <text:text>The first line</text:text><br />
         The second line
    </li>
    

    It operates on the nested element, so it is more important than any other selector.

    0 讨论(0)
  • 2021-01-12 00:59

    Your understanding of specificity is completely correct. Pseudo-classes and classes are equal to each other in specificity, and both of them rank higher than pseudo-elements and elements (which are also equal to each other). This is explained pretty clearly at the spec you already linked to.

    So why do the rules you set in li:first-line take precedence over the ones you set in .content:hover, if the latter is more specific?

    Because, from CSS's perspective, pseudo-elements are elements. That means that you have a li:first-line element which - if you didn't style it - would inherit color: green or color: yellow from the .content and .content:hover rules. But rules that target an element directly always take precedence over inherited rules, and your :first-line selector is targeting a pseudo-element within your li. The :first-line rules win simply because they are not inherited and the rules from .content and .content:hover selectors are inherited (by the pseudo-element contained within the li). Specificity rules are a red herring; they don't even come into play.

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