Sub topic 2
Why conflicts happen and what the cascade does about them
Prerequisites
This chapter assumes you can write a CSS rule with a selector and declarations, know how classes, ids and nesting work in HTML, and can open your browser's developer tools to inspect an element. Later chapters cover each ordering criterion in detail. Here you get the overall model.
Why conflicts happen
A browser needs exactly one value for each property of each element. A single element often receives several declarations for the same property. They come from different places:
- The browser's built-in default stylesheet.
- Your stylesheets, which may include a reset, a framework and your own code.
- Several rules in one stylesheet that match the same element.
- Inline
styleattributes. - Users who set their own preferences, such as a larger default font.
Nothing is wrong when two rules disagree. This is normal, and CSS has a defined procedure for choosing a winner. That procedure is the cascade.
p { color: black; }
.note { color: blue; }For <p class="note">, both rules match and both set color. The cascade decides which one applies.
What the cascade resolves
The cascade works per element and per property. It does not pick a winning rule. It picks a winning declaration for each property. A rule that loses on color can still win on margin.
The cascade takes all declarations that apply to an element and compares them with a fixed sequence of criteria. It stops at the first criterion that separates them.
- Origin and importance. Who wrote the declaration (browser, user or author), and whether it uses
!important. - Cascade layers. Which
@layerthe declaration belongs to, if any. - Specificity. How precisely the selector targets the element.
- Order of appearance. Which declaration comes last in the source.
Because the process stops early, a higher criterion always beats a lower one. A rule in a later layer beats a rule with a more specific selector in an earlier layer. Specificity does not matter until origin, importance and layers are tied. Source order matters only when everything above it is tied.
Worked example
Consider this markup and CSS.
p { color: black; }
.note { color: blue; }
#intro { color: green; }
/* <p id="intro" class="note">Hello</p> renders green:
id (1,0,0) beats class (0,1,0) beats element (0,0,1). */All three rules come from the author, none uses !important, and none is in a layer. The first two criteria tie. Specificity separates them, so #intro wins. The order of the rules in the file never gets consulted. You could move #intro to the top and the paragraph would still be green.
Now a case where specificity ties:
.button { background: gray; }
.button { background: steelblue; }The selectors are identical, so specificity ties. Source order decides, and the button is steelblue.
Properties are resolved independently
Take this rule pair:
.card { color: navy; padding: 1rem; }
#sidebar .card { color: crimson; }For a .card inside #sidebar, color is crimson because the second selector is more specific. padding is still 1rem, because nothing else declares it. The losing rule is not discarded. Only its losing declaration is ignored.
What happens when no declaration wins
Sometimes no declaration applies to a property at all. The browser then falls back to a different mechanism. If the property is inherited, such as color, the element takes its parent's value. Otherwise it takes the property's initial value. Inheritance is covered in a later chapter. For now, remember that it applies only after the cascade finds no declaration.
Common errors
- Assuming the later rule always wins. Source order is the last tiebreaker. A more specific selector earlier in the file beats a less specific one later.
- Treating a rule as a unit. The cascade compares individual declarations, so one rule can win some properties and lose others.
- Raising specificity to fix a conflict. This works until the next conflict. It leads to longer selectors and
!important. Later chapters show cleaner tools, such as cascade layers. - Forgetting origin. An author
!importantdeclaration can beat a normal declaration no matter how specific the other selector is.
Exercise
A paragraph <p id="intro" class="note"> matches #intro { color: green; } at the top of a stylesheet and .note { color: blue; } at the bottom. Both are normal author declarations outside any layer. Which color does the paragraph get, and which criterion decides it?
Show answer
Green. Origin and importance tie and there are no layers, so specificity decides. The id selector (1,0,0) outranks the class selector (0,1,0). Source order is never consulted.
Exercise
A .card element is matched by .card { color: navy; padding: 1rem; } and later by #sidebar .card { color: crimson; }. What are its color and padding?
Show answer
color is crimson, because the second selector is more specific. padding is 1rem, because the cascade resolves each property separately and no other declaration sets it.