Sub topic 6
When Methods Overlap: Cascade and Source Order
This chapter assumes you can write a basic HTML page, know the anatomy of a CSS rule (selector, property, value), and have seen all three ways of attaching CSS: the style attribute, the <style> element, and a linked stylesheet. Here you learn what the browser does when more than one of them sets the same property on the same element.
Why overlap happens
Real pages rarely use a single method. A site might link site.css on every page, add a <style> block for one page, and put a style attribute on one element for a quick fix. Sooner or later two of these set the same property, such as color, on the same element.
The browser does not report an error and does not merge the values. A property on an element has exactly one final value, called the computed winner. The cascade is the algorithm that picks it. The name comes from the idea that declarations "fall" through a series of tie-breaking steps until one remains.
The cascade in four steps
For one element and one property, the browser collects every declaration that applies and compares them in this order. The first step that separates them decides the result.
- Origin and importance. Declarations come from the browser's default stylesheet, from the user, and from the page author. Your CSS is author CSS. Declarations marked
!importantreverse the usual ordering. - Specificity. A more specific selector beats a less specific one. A
styleattribute counts as more specific than any selector. - Source order. If everything else is equal, the declaration that appears later wins.
Most day-to-day conflicts are decided by steps 2 and 3. This chapter focuses on source order. The next chapter covers specificity in detail.
Source order within one stylesheet
Start with the simplest case. Two rules with the same selector sit in the same file:
p {
color: blue;
}
p {
color: red;
}Both selectors are p, so specificity is equal. The second rule comes later, so paragraphs are red. The blue declaration is not an error. The browser keeps it but it loses.
Only the conflicting property is affected. In this example, the later rule does not erase the earlier one entirely:
p {
color: blue;
font-size: 1.25rem;
}
p {
color: red;
}Paragraphs are red and 1.25rem. The cascade resolves each property separately, so font-size has no competitor and survives.
Source order across methods
Source order is not limited to one file. The browser reads the document from top to bottom and treats every <style> element and every <link rel="stylesheet"> as a block of rules placed at that point. Rules from all of them join one ordered list.
So the position of the <link> relative to the <style> element in <head> matters:
<head>
<meta charset="utf-8">
<title>Order matters</title>
<link rel="stylesheet" href="site.css">
<style>
p { color: blue; }
</style>
</head>
<body>
<p>This paragraph is blue.</p>
</body>If site.css contains p { color: green; }, the paragraph is blue. Both selectors are p, and the <style> block comes after the <link>, so it wins.
Now swap the two:
<head>
<meta charset="utf-8">
<title>Order matters</title>
<style>
p { color: blue; }
</style>
<link rel="stylesheet" href="site.css">
</head>
<body>
<p>This paragraph is green.</p>
</body>The result is green. Nothing changed in either rule. Only the position of the <link> changed.
A stylesheet that is loaded late over the network does not change this. The browser places the file's rules at the position of the <link>, not at the moment the file arrives.
Inline styles and the overlap with the other two
A style attribute has no selector, and the cascade treats it as more specific than any selector. That means order does not decide the contest between an inline declaration and a normal rule. The inline one wins wherever the <link> or <style> appears.
<head>
<style>
p { color: blue; }
</style>
<link rel="stylesheet" href="site.css">
</head>
<body>
<p style="color: red;">Red, regardless of order above.</p>
</body>The paragraph is red even though site.css is the last thing loaded. The reasons are covered in the next chapter. For now, treat inline declarations as sitting above ordinary rules.
The exception is !important. A declaration with !important in a stylesheet beats a normal inline declaration:
<head>
<style>
p { color: blue !important; }
</style>
</head>
<body>
<p style="color: red;">Blue, because of !important.</p>
</body>Use !important rarely. It is hard to override later, and a page full of it becomes hard to maintain.
Order of `@media` rules and other wrappers
Rules inside @media blocks follow the same source order logic. A media query does not add specificity. It only decides whether the rules inside apply.
p {
color: black;
}
@media (min-width: 40rem) {
p {
color: navy;
}
}
p {
color: tomato;
}On a wide screen the paragraph is tomato, not navy. All three rules have equal specificity, and the last one comes after the media block. A common mistake is to put a base rule after the media rule that was meant to override it. Place base rules first and overrides after them.
Conflicts that are not conflicts
Two kinds of overlap look like cascade problems but are not.
- Different properties.
p { color: red; }andp { margin: 0; }do not compete. Both apply. - Inheritance. If
body { color: navy; }is set and no rule targetsp, the paragraph is navy becausecoloris inherited from its parent. Any rule that targets the paragraph directly, even a low-priority one, beats the inherited value.
The second point surprises people. In this example the paragraph is not navy:
<style>
body { color: navy; }
p { color: gray; }
</style>
<p>Gray. A rule aimed at p beats the value inherited from body.</p>A worked example with all three methods
Here is a page that uses all three methods. Predict the color of each paragraph before reading the answers.
site.css:
p {
color: green;
}
.note {
color: purple;
}index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Overlap practice</title>
<link rel="stylesheet" href="site.css">
<style>
p { color: blue; }
</style>
</head>
<body>
<p>Paragraph A</p>
<p class="note">Paragraph B</p>
<p style="color: red;">Paragraph C</p>
</body>
</html>- Paragraph A is blue. Two
prules compete. Specificity ties, and the<style>element comes after the<link>. - Paragraph B is purple.
.noteis a class selector, which is more specific thanp. Specificity decides before source order is checked, so the laterp { color: blue; }loses. - Paragraph C is red. The inline declaration beats every ordinary rule.
Paragraph B shows an important lesson. Moving the <link> below the <style> element would not change B, because specificity is checked first.
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| A rule in `site.css` has no effect | A later `<style>` block or stylesheet sets the same property | Check the order of `<link>` and `<style>` in `<head>` |
| An override works in one page but not another | The two pages place `<link>` elements in a different order | Use one consistent order in every page |
| A rule you just added does nothing | A more specific selector elsewhere matches the element | Raise specificity or restructure the selectors |
| A media query override is ignored | The base rule sits after the media block | Move the base rule above the media block |
| An inline style cannot be overridden from CSS | Inline beats ordinary selectors | Remove the inline declaration, or use `!important` as a last resort |
Practical habits
- Put shared, general rules in the earliest stylesheet and page-specific overrides after it.
- Keep a single order for
<link>and<style>elements across all pages. - Avoid repeating the same selector with the same property in many places. If you must override, do it deliberately and nearby.
- When a style does not apply, ask two questions in order. Does a more specific selector match? If not, which matching rule comes last?
Exercises
Exercise
A page has <style>p { color: blue; }</style> followed by <link rel="stylesheet" href="a.css">. The file a.css contains p { color: green; }. What color is a paragraph without a style attribute, and why? What changes if you swap the two lines?
Show answer
The paragraph is green. Both selectors are p, so specificity ties, and the <link> comes later in the document, so its rules come later in the cascade. If you swap the lines, the <style> block comes last and the paragraph becomes blue.
Exercise
Given p { color: blue; } in a <style> block placed after a link to a file containing .intro { color: orange; }, what color is <p class="intro">? Does moving the <link> below the <style> block change the result?
Show answer
It is orange. The class selector .intro is more specific than p, and specificity is compared before source order. Moving the <link> does not change the result.
Exercise
A paragraph has style="color: red;". The only stylesheet rule is p { color: blue !important; }. Which color wins, and what would win if !important were removed?
Show answer
Blue wins, because an !important declaration in a stylesheet beats a normal inline declaration. Without !important, the inline declaration wins and the paragraph is red.