In which order do CSS stylesheets override?

CssOverridingStylesheet

Css Problem Overview


In an HTML header, I've got this:

<head>
<title>Title</title>
<link href="styles.css" rel="stylesheet" type="text/css"/>
<link href="master.css" rel="stylesheet" type="text/css"/>

styles.css is my page-specific sheet. master.css is a sheet I use on each of my projects to override browser defaults. Which of these stylesheets takes priority? Example: first sheet contains specific

body { margin:10px; }

And associated borders, but the second contains my resets of

html, body:not(input="button") {
    margin: 0px;
    padding: 0px;
    border: 0px;
}

In essence, does the cascading element of CSS work the same in terms of stylesheet references as it does in typical CSS functions? Meaning that the last line is the one displayed?

Css Solutions


Solution 1 - Css

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.

Solution 2 - Css

The most specific style is applied:

div#foo {
  color: blue; /* This one is applied to <div id="foo"></div> */
}

div {
  color: red;
}

If all of the selectors have the same specificity, then the most recent decleration is used:

div {
  color: red;
}

div {
  color: blue; /* This one is applied to <div id="foo"></div> */
}

In your case, body:not([input="button"]) is more specific so its styles are used.

Solution 3 - Css

Order does matter. The last declared value of multiple occurrence will be taken. Please see the same one I worked out: http://jsfiddle.net/Wtk67/

<div class="test">Hello World!!</div>


<style>
    .test{
        color:blue;
    }

    .test{
        color:red;
    }
</style>

If you interchange the order of .test{}, then you can see the HTML takes value of the last one declared in CSS

Solution 4 - Css

The last loading CSS is THE MASTER, which will override all css with same css settings

Example:

<head>
	<link rel="stylesheet" type="text/css" href="css/reset.css">
	<link rel="stylesheet" type="text/css" href="css/master.css">
</head>

reset.css

h1 {
    font-size: 20px;
}

master.css

h1 {
    font-size: 30px;
}

The output for the h1 tag will be font-size: 30px;

Solution 5 - Css

Lets try to simplify the cascading rule with an example. The rules goes more specific to general.

  1. Applies rule of the ID's one first (over class and/or elements regardless of the order)
  2. Applies classes over elements regardless of order
  3. If no class or id, applies the generic ones
  4. Applies the last style in the order (declaration order based on file load order) for the same group/level.

Here is the css and html code;

<style>
    h2{
        color:darkblue;
    }
    #important{
        color:darkgreen;
    }
    .headline {
        color:red;
    }
    article {
        color:black;
        font-style:italic;
    }
    aside h2 {
        font-style:italic;
        color:darkorange;
    }
    article h2 {
        font-style: normal;
        color: purple;
    }
</style>

Here is the css style

<body>
<section>
    <div>
        <h2>Houston Chronicle News</h2>
        <article>
            <h2 class="headline" id="important">Latest Developments in your city</h2>
            <aside>
                <h2>Houston Local Advertisement Section</h2>
            </aside>
        </article>
    </div>

    <p>Next section</p>
</section>

Here is the result. No matter the order of style files or the style declaration, id="important" applies at the end (note the class="deadline" declared last but does not take any effect).

The <article> element contains <aside> element, however last declared style will take effect, in this case article h2 { .. } on third h2 element.

Here is the result on IE11: (Not enough rights to post image)DarkBlue: Houston Chronicle News, DarkGreen: Latest Developments in your city, Purple: Houston Local Advertisement Section, Black: Next section

enter image description here

Solution 6 - Css

It depends on both load order and the specificity of the actual rules applied to each style. Given the context of your question you want to load your general reset first, then the page specific. Then if youre still not seeing the intended effect you need to look into the specificity of the selectors involved as others have already pointed out.

Solution 7 - Css

The best way is to use classes as much as possible. Avoid ID selectors (#) anyway. When you write selectors with just single classes, the CSS inheritance is way more easy follow.

Update: Read more about CSS specificity in this article: https://css-tricks.com/specifics-on-css-specificity/

Solution 8 - Css

> EDIT: Apr 2020, according to @LeeC's comment, this is not longer the case

Yes, it works the same as if they were in one sheet, however:

Load Order Matters!

<link href="styles.css" rel="stylesheet" type="text/css"/>
<link href="master.css" rel="stylesheet" type="text/css"/>

In the above code, there is no guarantee that master.css will load after styles.css. Therefore, if master.css is loaded quickly, and styles.css takes a while, styles.css will become the second stylesheet, and any rules of the same specificity from master.css will be overwritten.

Best to put all your rules into one sheet (and minify) before deploying.

Solution 9 - Css

I suspect from your question that you have duplicate selections, a master set, that goes for all pages, and a more specific set that you wish to override the master values for each individual page. If that is the case, then your order is correct. The Master is loaded first and the rules in subsequent file will take precedence (if they are identical or have the same weight). There is a highly recommended description of all rules at this website http://vanseodesign.com/css/css-specificity-inheritance-cascaade/ When I started with front end development, this page cleared up so many questions.

Solution 10 - Css

It is the cascade that defines the precedence of declarations. I can recommend the official specification; this part of it is well-readable.

https://www.w3.org/TR/css-cascade-3/#cascading

The following have an effect on the sorting, in descending order of priority.

  1. A combination of origin, and importance:

  2. Transition declarations

  3. User agent’s declarations marked with !important

  4. User’s declarations marked with !important

  5. Page author’s declarations marked with !important

    1. Definitions on the element with style attribute
    2. Document-wide definitions
  6. Animation declarations

  7. Page author’s declarations

    1. Definitions on the element with style attribute
    2. Document-wide definitions
  8. User’s declarations

  9. User agent’s declarations

  10. If two declarations have the same origin and importance, the specifity, meaning how clearly the element is defined, comes into play, calculating a scoring.

  • 100 points for every identifier (#x34y)
  • 10 points for every class (.level)
  • 1 point for every element type (li)
  • 1 point for every pseudo-element (:hover) excluding :not

For example, the selector main li + .red.red:not(:active) p > * has a specifity of 24.

  1. The order of appearance only plays a role if two definitions have the same origin, importance and specificity. Later definitions precede earlier definitions in the document (including imports).

Solution 11 - Css

I believe when executing the code, it is read top to bottom, meaning the last CSS link would be override similar styles in any style sheets above it.

For example, if you created two style sheets

<link rel="stylesheet" href="style1.css"> 
<link rel="stylesheet" href="style2.css">

and in both of these, you set the body background-color to two different colors - the color of the body in style2.css would take priority.

You could avoid the styling priority issue by using !IMPORTANT inside the class style you would like to take priority, or possibly re-arranging your <link> order.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionian5vView Question on Stackoverflow
Solution 1 - Cssuser149341View Answer on Stackoverflow
Solution 2 - CssBlenderView Answer on Stackoverflow
Solution 3 - CssRoy M JView Answer on Stackoverflow
Solution 4 - CssDoodlView Answer on Stackoverflow
Solution 5 - CssKagan AgunView Answer on Stackoverflow
Solution 6 - CssprodigitalsonView Answer on Stackoverflow
Solution 7 - CssJeroen OomsView Answer on Stackoverflow
Solution 8 - Cssn_i_c_kView Answer on Stackoverflow
Solution 9 - CssRollingInTheDeepView Answer on Stackoverflow
Solution 10 - CssMichael SchmidView Answer on Stackoverflow
Solution 11 - CssEnnam HoqueView Answer on Stackoverflow