What are the priorities among CSS selectors

前端 未结 9 1571
日久生厌
日久生厌 2020-11-27 22:27

CSS Question: If two different selectors apply to an element, who wins?

I know this shouldn\'t happen, but I want to tweak a legacy application, and the CSS is getti

9条回答
  •  借酒劲吻你
    2020-11-27 22:52

    The gory details in the spec are actually reasonably readable. In summary:

    1. !important rules and inline style rules win most.

    2. Otherwise, normally the more specific wins. #id is a more specific selector than .classname is a more specific selector than tagname.

    3. Where rules are equally specific, the one declared last wins.

    There is no particular reason why this ‘shouldn't happen’. It's normal to specify a broad-brush rule and then add a more specific rule to target one case; multiple same-property rules on a single element are not unusual or undesirable.

提交回复
热议问题