Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the priorities among CSS selectors

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 getting in the middle.

like image 950
flybywire Avatar asked Oct 28 '09 13:10

flybywire


People also ask

Which has more priority class or ID in CSS?

Class and ID selector example If more than one rule applies to an element and specifies the same property, then CSS gives priority to the rule that has the more specific selector. An ID selector is more specific than a class selector, which in turn is more specific than a tag selector.


1 Answers

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.

like image 162
bobince Avatar answered Oct 11 '22 14:10

bobince