Reputation: 2118
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?
Upvotes: 173
Views: 204222
Reputation: 1534
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.
Upvotes: 1
Reputation: 35
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.
Upvotes: 0
Reputation: 5005
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.
A combination of origin, and importance:
Transition
declarations!important
!important
!important
style
attributeAnimation
declarationsstyle
attributeIf two declarations have the same origin and importance, the specifity, meaning how clearly the element is defined, comes into play, calculating a scoring.
#x34y
).level
)li
):hover
) excluding :not
For example, the selector main li + .red.red:not(:active) p > *
has a specifity of 24.
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).
Upvotes: 1
Reputation: 731
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.
Upvotes: 1
Reputation: 247
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;
Upvotes: 22
Reputation: 116
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/
Upvotes: 2
Reputation: 6938
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
Upvotes: 27
Reputation: 589
Lets try to simplify the cascading rule with an example. The rules goes more specific to general.
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
Upvotes: 9
Reputation: 298364
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.
Upvotes: 37
Reputation: 60403
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.
Upvotes: 6
Reputation:
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.
Upvotes: 176