Sub topic 7
Specificity and Why Inline Styles Usually Win
This chapter assumes you can write a CSS rule, know the three ways to attach CSS (the style attribute, the style element, and a linked stylesheet), and have seen that a later rule beats an earlier one when both match. Here you will learn what happens when two rules match the same element and neither is simply "later".
The problem source order does not solve
Source order only decides ties. Before the browser checks order, it compares specificity, a measure of how precisely a selector targets an element. A more specific selector wins even if it appears earlier.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Specificity</title>
<style>
#intro { color: green; }
p { color: blue; }
</style>
</head>
<body>
<p id="intro">Which color am I?</p>
</body>
</html>The paragraph is green. The p rule comes later, but an ID selector is more specific than a type selector, so #intro wins.
How specificity is calculated
Think of specificity as three counters, compared from left to right:
| Selector type | Example | Counter |
|---|---|---|
| ID | `#intro` | first (IDs) |
| Class, attribute, pseudo-class | `.note`, `[type="text"]`, `:hover` | second |
| Type, pseudo-element | `p`, `::before` | third |
The universal selector * and combinators such as > add nothing.
Some examples, written as (IDs, classes, types):
| Selector | Specificity |
|---|---|
| `p` | (0, 0, 1) |
| `.note` | (0, 1, 0) |
| `p.note` | (0, 1, 1) |
| `#intro` | (1, 0, 0) |
| `#intro p.note` | (1, 1, 1) |
Compare the first counter, and only if it ties, the second, and then the third. One ID beats any number of classes. Ten classes do not add up to an ID, because the counters never carry over into each other.
Where inline styles fit
A declaration in a style attribute sits above all selector-based rules. You can picture it as a fourth, leftmost counter: (1, 0, 0, 0). That is why it beats a rule with an ID selector, whether the rule comes from a style element or a linked file.
<head>
<style>
#intro { color: green; }
</style>
</head>
<body>
<p id="intro" style="color: red;">I am red.</p>
</body>The paragraph is red. Placement does not matter here. A rule in site.css and a rule in a style element have the same standing, and specificity is compared first. Source order only applies when specificity is equal.
The diagram is simplified. It ignores other origins, such as the browser's default styles and user styles, which rank below author styles in normal cases.
The role of !important
A declaration ending in !important beats normal declarations, including inline ones.
<head>
<style>
p { color: blue !important; }
</style>
</head>
<body>
<p style="color: red;">I am blue.</p>
</body>The paragraph is blue. An !important declaration in a stylesheet defeats a normal inline declaration. If both are !important, the inline one wins again.
Common errors
- Expecting the later rule to win. If a rule seems to be ignored, compare specificity before checking order.
- Overriding an inline style from a stylesheet. A normal rule cannot do it, because the inline declaration outranks it. Either remove the inline style or use
!importantas a last resort. - Adding IDs to win fights. This makes later overrides harder. Prefer class selectors and keep specificity low and even.
- Confusing inheritance with specificity. A value inherited from a parent always loses to any rule that targets the element directly, even a rule with specificity (0, 0, 1).
Exercise
A page has .card p { color: blue; } in a linked file, p { color: green; } in a style element placed after the link, and <p class="x"> inside a .card element. What color is the paragraph, and why? Then, what changes if you add style="color: red;" to the paragraph?
Show answer
Blue. .card p has specificity (0, 1, 1) and p has (0, 0, 1), so the first rule wins even though the style element comes later. Adding the inline declaration makes the paragraph red, because an inline declaration outranks any selector, unless a competing declaration uses !important.
Exercise
Rank these selectors from lowest to highest specificity: #nav a, .menu a.active, a, .menu.
Show answer
a (0, 0, 1), then .menu (0, 1, 0), then .menu a.active (0, 2, 1), then #nav a (1, 0, 1).