Sub topic 6
When Methods Overlap: Cascade and Source Order
This chapter assumes you can write a basic HTML page, know the three ways to attach CSS (the style attribute, the style element, and a linked stylesheet), and can read a CSS rule made of a selector, a property, and a value.
Why overlap happens
A real page often gets styles from more than one place. A linked stylesheet sets paragraph color, a style element adds a tweak, and one element carries an inline declaration. When two declarations set the same property on the same element, the browser must pick one. The cascade is the set of rules it uses to decide.
For the cases in this chapter, the browser compares declarations in this order:
- Importance: a declaration marked
!importantbeats a normal one. - Specificity: a more specific selector beats a less specific one. Inline declarations count as the most specific normal kind.
- Source order: if everything above is equal, the declaration that comes later wins.
The cascade has more steps than this (for example, browser default styles and user styles). This list covers what you need when mixing the three methods.
Source order: the last equal rule wins
Source order means the position of a rule in the combined stream of styles the browser reads, from the top of the document to the bottom. The method does not matter. A linked file and a style element are treated alike, and position decides.
<head>
<link rel="stylesheet" href="site.css">
<style>
p { color: blue; }
</style>
</head>
<body>
<p>Blue, if site.css sets p to another color, because the style element comes later and has equal specificity.</p>
<p style="color: red;">Red, because the inline declaration beats the selector rules above.</p>
</body>Suppose site.css contains p { color: darkgreen; }. Both rules use the selector p, so their specificity is equal. The style element comes after the link, so the first paragraph is blue.
Now swap the order of the two in the head:
<head>
<style>
p { color: blue; }
</style>
<link rel="stylesheet" href="site.css">
</head>The linked file is now read last, so the first paragraph turns dark green. Nothing about the rules changed. Only their position did.
Inline declarations beat selector rules
The second paragraph above has style="color: red;". A declaration in a style attribute is more specific than any selector rule, so it wins no matter where the linked file or style element sits. Moving the inline style earlier or later in the file changes nothing, because the attribute belongs to the element and is not part of the ordered stream of rules.
Only !important in a selector rule can override a normal inline declaration:
<style>
p { color: blue !important; }
</style>
<p style="color: red;">This paragraph is blue.</p>Use !important rarely. It makes later changes harder, because the only way to override it is another !important with equal or greater specificity, or a later one.
Combining properties, not replacing rules
The cascade works per property, not per rule. A losing rule is not discarded. Only the properties that conflict are decided.
<style>
p { color: navy; font-size: 1.25rem; }
</style>
<p style="color: crimson;">Crimson text at 1.25rem.</p>The inline color wins, but font-size still comes from the style element, because the inline style never set it.
Common errors
- Editing a rule that is overridden. You change
p { color: blue; }and nothing happens, because a later rule or an inline declaration setscoloragain. Find the winning declaration first. - Wrong link order. Placing your own stylesheet before a shared one, such as a framework file, lets the shared file override yours. Put your own
linklast. - Expecting the method to matter. Inline wins because of specificity, not because it is "inline" in a loose sense. A linked file does not lose just because it is external.
- Overusing
!important. It hides the real problem, which is usually order or specificity.
The next chapters cover how specificity is calculated and how to see the winning declaration in developer tools.
Exercise
A page has <link rel="stylesheet" href="a.css"> followed by <style> h1 { color: green; } </style> in the head. a.css contains h1 { color: purple; }. The h1 has no style attribute. What color is the heading, and what changes if you move the link element after the style element?
Show answer
The heading is green, because both rules use the same selector (equal specificity) and the style element comes later. If the link element moves after the style element, a.css is read last and the heading becomes purple.
Exercise
An element has style="margin: 0;", and a stylesheet rule sets p { margin: 2rem; padding: 1rem; } for it. Which margin and padding values apply, and why?
Show answer
Margin is 0, because the inline declaration is more specific than the selector rule. Padding is 1rem, because the cascade decides each property separately and the inline style never set padding.