How does CSS specificity decide which styles to apply?

后端 未结 3 1801
春和景丽
春和景丽 2020-12-03 22:52

How does CSS determine when to apply one style over another?

I have been through the W3 CSS3 selectors document a few times, and that has helped me understand how to

3条回答
  •  天涯浪人
    2020-12-03 23:46

    It's based on IDs, classes and tags. IDs have the highest specificity, then classes then tags, so:

    .item a     == 0 1 1      0 (id) 1 (class=item) 1 (tag=a)
    .special    == 0 1 0
    #foo        == 1 0 0
    #foo .bar a == 1 1 1
    #foo #bar   == 2 0 0
    

    whichever has the most wins :) If it's a tie, whichever one comes last in the document wins. Note that 1 0 0 beats 0 1000 1000

    If you want .special to apply, make it more specific: .item a.special

提交回复
热议问题