53

When using multiple css files on the same page and they collide, how do i know which one is going to be used? For example, if one says blue body background and the other one says red.

Filip Haglund
  • 12,351
  • 10
  • 60
  • 102
  • If you do not already have it, download the Firebug plugin for Mozilla Firefox. It has a CSS explorer that allows you to view all styles and classes on a specific DOM element. It even allows you to edit them ad-hoc and visually see the results on any page. This more than anything helped me to learn how CSS interacts with a page. – maple_shaft May 05 '11 at 19:14
  • Firebug and chrome inspector (and opera firefly) are invalueable tools when designing web pages! – Filip Haglund Nov 06 '13 at 11:58

1 Answers1

88

Quick Answer:

If both pieces of CSS have the same specificity (for example, they're both body{), then whichever gets called LAST will override the previous one.

BUT, if something has higher specificity (a more specific selector), it will be used regardless of the order.


Example 1:

<div class="container">
    <div class="name">Dave</div>
</div>

<style>
    .name { color: blue; }
    .name { color: red; }
</style>

The above example will make the color red. Both selectors are the same, and therefore also have the same specificity. And because CSS reads top-to-bottom, we first tell it to be blue, but then we override that by telling it "nevermind, make it red".


Example 2:

<div class="container">
    <div class="name">Dave</div>
</div>

<style>
    #container .name { background-color: blue; }
    .name { background-color: red; }
</style>

The above example will make the background color blue, even though blue was first because the selector is more "specific".


Exception (the use of !important):

The inclusion of !important will override both specificity and order, but in my opinion, should only be used if you're trying to mess with a third party code in which you don't have access to change it any other way.


External CSS:

Overwrite rules work the same on external CSS files. Just imagine putting them first-to-last, top-to-bottom. The selectors called in the first file(s) will get overwritten by same-specificity-selectors in any subsequent files. But specificity will still trump order within the same file or in multiple files.


How to test:

In Chrome, Firefox, and modern versions of IE (probably Safari too), you can right click on something and click "Inspect Element". This will show you the HTML as well as any applied CSS. As you scroll down the CSS (usually on the right), you'll see things that are crossed out - that means they're either incorrect CSS or have been overwritten. To test, you can modify the CSS selectors (either in your own code or right there in the developer tools box) to make them more specific and see if that makes then un-crossed out...etc. Play around w/ that tool - it's VERY helpful.

Dave
  • 27,341
  • 18
  • 106
  • 177
  • 7
    +1 for mention of specificity - I would only add a mention of `!important`. @Filip, for details, see [http://www.w3.org/TR/CSS2/cascade.html](http://www.w3.org/TR/CSS2/cascade.html). – Ian Pugsley May 05 '11 at 19:12
  • 1
    @Ian Valid point - I didn't think of that because I HATE using it, but certainly true. I'll edit my answer to include. (thanks!) – Dave May 05 '11 at 19:15
  • 1
    Oh yeah, absolutely - it's not pretty, but it is valid. – Ian Pugsley May 05 '11 at 20:00
  • thanks! trying to let the users customize their version of the site through css :) – Filip Haglund May 06 '11 at 04:49
  • Worth noting: sometimes it's a bit tricky to guess the specificity of a long chained selection such as `div.top > div.mid p+span` vs a basic class selector (such as if the span above had a class, and I used `.myspanclass`) and `#myspanid`. Specially if the long chain starts with an id selector! – Filip Haglund Mar 28 '14 at 19:03
  • 1
    thanks for mentioning the case of multiple external css files – hafan96 Oct 17 '20 at 23:21