14

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.

flybywire
  • 261,858
  • 191
  • 397
  • 503

9 Answers9

44

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.

bobince
  • 528,062
  • 107
  • 651
  • 834
  • 1
    user/author's stylesheets also need to be considered: `By default, rules in an author's style sheet override those in a user's style sheet. Both author and user style sheets may contain "!important" declarations, and user "!important" rules override author "!important" rules.` – laurent Dec 13 '12 at 03:49
7

you have to find #no of id =A ,#no of class =B and #no of tag =c in the selector

ABC with higher value wins.

.wrapper .title  p {  
  //some other rules
}

A=0 B=2 C=1 =021

\#foo {
  // some more rules
}

A=1 = 100

.bar .head div li{
  // some more rules
}


A=0 B=2 C=2 =022

100>022>021

so #foo wins

jegan
  • 71
  • 1
  • 1
5

It should happen! That's why it's called CASCADING style sheets. You can find an example of the priorities here

Manu
  • 28,753
  • 28
  • 75
  • 83
5

"#id" is more powerful than ".class" name and ".class" is more powerful than "tag" name. But if we apply "!important" than its priority is most of them.

  • !important
  • inline style
  • id
  • class
  • tag
Gaurav Tripathi
  • 1,265
  • 16
  • 20
2

See the specificity order section of the specification (along with the rest of that chapter as !important rules and the order the rules appear in the stylesheet have an impact too).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

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.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

Order in the CSS file only matters if the selectors share the same specificity.

For more on selector specificity: Andy Clarke penned Specificity Wars which is the best overview of how they work.

Paul Irish
  • 47,354
  • 22
  • 98
  • 132
0

CSS stands for Cascading Style Sheets. This means that rules apply to elements in a cascading way. It's perfectly normal that different selectors apply to an element. Thinks, for example, to the following:

<div class="wrapper">
  <div id="foo" class="bar" style="some rules">Test</div>
</div>

The following rules would affect to the "foo" element:

.wrapper {
  //some other rules
}

#foo {
  // some more rules
}

.bar {
  // some more rules
}

Rules for priorities can be found here.

I always advise to use the Firefox "firebug" plugin. It will show you exactly which properties are evaluated for a specific element and why, emphasizing overrides during the cascade.

Sudarshan
  • 18,140
  • 7
  • 53
  • 61
Roberto Aloi
  • 30,570
  • 21
  • 75
  • 112
0
  1. !important
  2. inline style
  3. #id
  4. class
  5. tag
Undo
  • 25,519
  • 37
  • 106
  • 129
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). - [From Review](/review/low-quality-posts/11251760) – Sebastian Brosch Feb 13 '16 at 09:39
  • 1
    @sebastianbrosch How does this not attempt to provide an answer? It might be completely wrong or incorrectly formatted, [but that doesn't mean we should abuse the review queues to delete it.](http://meta.stackoverflow.com/q/287563/1849664) – Undo Feb 14 '16 at 01:36