How does CSS specificity decide which styles to apply?

后端 未结 3 1787
春和景丽
春和景丽 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:41

    I would suggest you get familiar with this for future reference. For this particular case, note point 3 under Cascading Order:

    1. Count the number of ID attributes in the selector.
    2. Count the number of CLASS attributes in the selector.
    3. Count the number of HTML tag names in the selector.

    If these are applied to your code, .item a has 1 class attribute + 1 HTML tag name, while .special has only one class attribute. Hence, the former wins the right to style the special link.

提交回复
热议问题