What are the priorities among CSS selectors

前端 未结 9 1559
日久生厌
日久生厌 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:56

    The priority between selectors is controlled by how specific they are. More specific selectors win over less specific.

    If two selectors are equally specific, the later one wins over the first one.

    There are three levels of specificity, id, class and element. So #elem wins over .elem as an id is more specific. .elem .cont wins over .elem as it has more selectors at the same level.

    Read more under "What happens when conflicts occur?" at Selectutorial.

提交回复
热议问题