Does The Order of CSS Stylesheet Definitions Matter

Does the order of css stylesheet definitions matter?

There is a defined cascade in which the styles are sorted and applied.
When declarations have the same importance (weight), origin and specificity then the latter declaration wins. Most answers cover importance and specificity here but not origin.

Here are some very good slides about CSS Cascades. (Overview all resources)

does the order of styles matter?

If the rules are equal in specificity (in this case they are), individual rules get overridden in the order they're defined in the CSS, so in your example red wins because it comes later in the CSS definitions. The same rule applies in other cases as well, for example:

<div class="red green">

Which of these wins?

.green { color: green; }
.red { color: red; }

.red wins here, it doesn't matter the order in the class attribute, all that matters is the order the styles are defined in the CSS itself.

Should the order of CSS files in a HTML page matter?

Short answer: Yes.

This is actually a subject I taught just last week, and I'll tell you a brief version of a one-hour class I told my students:

  • Bootstrap first to establish the framework
  • Follow with any supporting stylesheet (Owl.css, plugins.css, etc)
  • Next is your custom stylesheet. This is to override all of the above.
  • Lastly, the responsive stylesheet. This one will override all of the above systematically and programatically according to the media queries conditions being satisfied in the browser.

Doing this type of architecture will reduce the amount of (important!) drastically.

Does the order of rules in a CSS stylesheet affect rendering speed?

After some more testing and reading I came to the following conclusion, no, it does not matter. Even after some ‘extreme’ testing, I could not find anything that supports the idea that the order matters.

There were no 'flashed of unstyled content' or the likes, it just took way longer to load the page ( way way longer :D )

Tests I ran
I created a test page with 60.000 div elements, each having a unique ID attribute. Each of these ID’s had their own css rule applied to it. Below that I had a single span element with a CLASS attribute, which was also had a css rule linked to it.

These tests created a html file of 2MB with a corresponding css file of 6MB.

At first I attempted these tests with 1.000.000 divs and css rules, but Firefox did not approve and started crying, begging me to stop.

I generated these elements and their css with the following simple php snippets.

<?PHP

for ($i = 0; $i < 60000; $i++) {
echo "
#test$i {
position: absolute;
width: 1px;
height: 1px;
top: " . $i . "px;
left: 0;
background: #000;
} <br />
";
}

?>

And

<?PHP

for ($i = 0; $i < 60000; $i++) {
echo "
<div id=\"test$i\"></div>
";
}

?>

The result was put in a html and css file afterwards to check the results.

Mind you, my browser ( Firefox 5 ) really did not appreciate me playing around with this, it really had some issues generating the output, the occasional this program is not responding message was not afraid to show it's face.

These tests were ran on a localhost, ran by a simple XAMPP installation, it might be possible that external servers result in a different resultset, but I am currently unable to test that.

I tested a few variations on the above:

  • Placing the element before all the generated divs, in the
    middle and at the end
  • Placing the span’s css definition before, in the middle or at the end
    of the css file.

Oh and may I suggest: http://www.youtube.com/watch?v=a2_6bGNZ7bA while it doesn't exactly cover this question, it does provide some interesting details about how Firefox ( and possibly other browsers )work with the stuff we throw at it.

Does order of class selectors matter?

All of these would be applied, since the order is not relevant. The later rules will overwrite the previous ones if they contain settings for the same parameters, just due to the order in the stylesheet.

In which order do CSS stylesheets override?

The rules for CSS rule cascading are complex -- rather than trying to paraphrase them badly, I'll simply refer you to the spec:

http://www.w3.org/TR/2011/REC-CSS2-20110607/cascade.html#cascade

In short: more specific rules override more general ones. Specificity is defined based on how many IDs, classes, and element names are involved, as well as whether the !important declaration was used. When multiple rules of the same "specificity level" exist, whichever one appears last wins.

What is the order of precedence for CSS?

There are several rules ( applied in this order ) :

  1. inline css ( html style attribute ) overrides css rules in style tag and css file
  2. a more specific selector takes precedence over a less specific one
  3. rules that appear later in the code override earlier rules if both have the same specificity.
  4. A css rule with !important always takes precedence.

In your case its rule 3 that applies.

Specificity for single selectors from highest to lowest:

  • ids (example: #main selects <div id="main">)
  • classes (ex.: .myclass), attribute selectors (ex.: [href=^https:]) and pseudo-classes (ex.: :hover)
  • elements (ex.: div) and pseudo-elements (ex.: ::before)

To compare the specificity of two combined selectors, compare the number of occurences of single selectors of each of the specificity groups above.

Example: compare #nav ul li a:hover to #nav ul li.active a::after

  • count the number of id selectors: there is one for each (#nav)
  • count the number of class selectors: there is one for each (:hover and .active)
  • count the number of element selectors: there are 3 (ul li a) for the first and 4 for the second (ul li a ::after), thus the second combined selector is more specific.

A good article about css selector specificity.

Order of prioritization when using multiple contradictory css files

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.

Not sure how "specific" something is?
Try some of the many online CSS specificity tools:

  • https://polypane.app/css-specificity-calculator
  • https://specificity.keegan.st/
  • https://www.codecaptain.io/tools/css-specificity-calculator

Does the link tag order make a difference in html?

If you wanna use the font you are getting from Google in styles.css you would wanna take the first approche, this one:

<!-- 1st case -->
<link rel="stylesheet" href="google font url">
<link rel="stylesheet" href="./styles.css">

Otherwise, you couldn't, cause CSS is getting interpreted top to bottom. If you wanna see a difference use for example this font in this order:

<link href="https://fonts.googleapis.com/css2?family=Shizuru&display=swap" rel="stylesheet">
<link rel="stylesheet" href="./styles.css">

And in your styles.css, define the following:

 *{
font:inherit;
}
html{
font-family: 'Shizuru', cursive;
}

Take a look at the page, and then change the order like so and see the difference:

<link rel="stylesheet" href="./styles.css">
<link href="https://fonts.googleapis.com/css2?family=Shizuru&display=swap" rel="stylesheet">


Related Topics



Leave a reply



Submit