Sub topic 2
Why Conflicts Happen and What the Cascade Does About Them
This chapter assumes you can write CSS rules with selectors and declarations. It also assumes you know basic HTML structure (elements, classes, ids, nesting) and can inspect an element in your browser's developer tools. Here you get the overall model of how the browser settles conflicting declarations. Later chapters cover each criterion in detail.
Where conflicts come from
A browser renders each element using many style sources at once. A single <p> can be styled by all of these:
- The browser's built-in default stylesheet, such as the margins on paragraphs and the blue underline on links.
- Your site's stylesheets, often several files from different authors or libraries.
- A
<style>element in the page. - A
styleattribute on the element itself. - Optionally, a stylesheet the user configured, for example to enlarge text.
Each source can contain several rules, and one element can match many of those rules. Some of them will set the same property. When two matching declarations disagree about, say, color, the browser must pick one. An element can only have one color.
This is not a bug. It is how CSS is meant to work. You write general rules ("all paragraphs are black") and specific exceptions ("notes are blue"), and the browser needs a deterministic way to rank them. That ranking procedure is the cascade, which is where the "C" in CSS comes from.
<p id="intro" class="note">Hello</p>p { color: black; font-size: 1rem; }
.note { color: blue; }Both rules match the paragraph. They conflict on color (black or blue) and do not conflict on font-size (only one rule sets it). The cascade picks blue for color. font-size stays at 1rem, because no other declaration competes for it.
The cascade works per property, per element
Two facts about scope prevent most early confusion.
- The cascade runs separately for every property on every element. It does not pick a winning rule and then apply all of it. In the example above, the
.noterule winscolor, but theprule still suppliesfont-size. Rules are not replaced as units. Each declaration competes only against other declarations for the same property on the same element. - Only matching rules compete. If a selector does not match the element, its declarations are ignored for that element, however specific or important they look.
The result of the cascade for one property is called the cascaded value. If no declaration matches at all, there is no cascaded value, and the browser falls back to inheritance or to the property's initial value. That fallback is covered in the chapters on value processing and inheritance.
Shorthands expand before they compete
A shorthand property such as margin or background is expanded into its longhand properties (margin-top, margin-right, and so on) before the cascade runs. The competition then happens per longhand.
.card { margin-top: 2rem; }
.card-wide { margin: 0; }If an element has both classes, both selectors have the same weight. Whichever rule comes later in the stylesheet wins, so the order of these two lines decides whether the top margin is 2rem or 0. Writing margin: 0 after margin-top: 2rem silently resets the top margin. This is a very common source of "my style disappeared" bugs.
The ordering criteria
The cascade compares competing declarations by running them through a fixed sequence of questions. The first question that produces a single winner ends the comparison. Later questions are never consulted.
| Step | Criterion | Question it answers |
|---|---|---|
| 1 | Relevance | Does the rule's selector actually match this element? |
| 2 | Origin and importance | Who wrote the declaration (browser, user, author), and is it marked `!important`? |
| 3 | Element-attached styles | Is the declaration in the element's own `style` attribute? |
| 4 | Cascade layers | Which `@layer` is the declaration in, if any? |
| 5 | Specificity | How specific is the selector (ids, then classes, then elements)? |
| 6 | Order of appearance | Which declaration comes last in the source? |
The order is the important part. Specificity is only looked at if the earlier steps tie. Source order is only looked at if specificity also ties. That is why a plain p { color: red !important; } beats #intro { color: green; }: the importance check at step 2 decides the outcome, and the id selector's higher specificity at step 5 is never consulted.
Step 3 is a simplification of how the specification phrases it, but it matches what you see in practice. A normal declaration in a style attribute beats a normal declaration from any selector in a stylesheet, regardless of that selector's specificity, unless layers or importance change the picture.
Following one conflict through the steps
Here is a small page where several sources compete for the color of a link.
<nav id="main-nav">
<a href="/docs" class="link">Docs</a>
</nav>a { color: teal; }
.link { color: crimson; }
#main-nav a { color: navy; }The browser's default stylesheet also sets a link color. The competing declarations for color on the anchor are:
- The browser default (user-agent origin).
a { color: teal; }.link { color: crimson; }#main-nav a { color: navy; }
Walking through the steps:
- Relevance. All four match the anchor.
- Origin and importance. None is
!important. For normal declarations, author styles beat user styles, which beat browser defaults. The browser default is eliminated. Three author declarations remain. - Style attribute. There is none, so nothing changes.
- Layers. None of the rules is in a layer, so they tie.
- Specificity.
ascores (0,0,1),.linkscores (0,1,0), and#main-nav ascores (1,0,1). The last one is highest, so the link is navy. - Order of appearance. Not needed, because specificity already produced a winner.
The .link rule appears after a in the file, but that never matters here, because the comparison ended at step 5. Source order only breaks ties.
Why the order of criteria matters in practice
Knowing the order lets you predict results quickly and choose the right tool for a fix.
- If a rule seems to be ignored, work from the top of the table. Check that the selector matches, then whether an
!importantor a more specific rule is involved, then source order. - Raising specificity is not always the answer. If the competing declaration is
!important, or sits in a later layer, adding more classes to your selector changes nothing. - Source order is the weakest criterion. Rearranging a file only helps when selectors have equal specificity and equal origin, importance, and layer.
The !important flag deserves one warning now. For important declarations, the origin ranking reverses. Important browser-default declarations beat important user ones, and those beat important author ones. Layers also reverse for important declarations. The details come in the next chapters, but keep in mind that "later wins" and "author beats browser" hold only for normal declarations.
What the cascade does not do
The cascade picks among declarations that target the same element. It does not move values between elements. Inheritance, where a child takes a value from its parent, is a separate mechanism that runs after the cascade.
The sequence for a given property on a given element looks like this:
- The cascade finds the winning declaration, if any. That gives the cascaded value.
- If there is no winning declaration, the browser checks whether the property is inherited. If it is, the element takes its parent's computed value. If not, it takes the property's initial value.
- The result is then resolved into a computed value and finally a used value, for example turning
2eminto pixels.
Common errors
- Assuming the last rule always wins. Source order is the final tie-breaker, not the first rule. A later rule with a lower specificity loses to an earlier, more specific one.
- Assuming the most specific selector always wins. An
!importantdeclaration, astyleattribute, or a later cascade layer can beat a more specific selector. - Thinking the whole rule wins or loses. Only the conflicting properties compete. Other declarations in the same rule still apply.
- Forgetting shorthands reset longhands. A later
background: noneremoves an earlierbackground-color, because the shorthand sets every background longhand. - Blaming the cascade for inheritance. If a property is not inherited, a child will not pick up the parent's value, however the parent's rule ranks.
- Debugging by guessing. The Styles panel in developer tools shows overridden declarations crossed out. Use it before changing selectors.
A short exercise in prediction
Try to predict the result before you read the answer in the exercise below. Work through the steps in the table: relevance, origin and importance, style attribute, layers, specificity, then order.
.box { padding: 1rem; background: white; }
.box { background: lightgray; }
div.box { padding: 2rem; }For <div class="box">, the padding declarations come from .box (0,1,0) and div.box (0,1,1). The higher specificity gives 2rem. The two background declarations come from selectors with equal specificity, so the later one gives lightgray.
Exercise
A paragraph <p id="intro" class="note"> is matched by #intro { color: green; } and by p.note { color: blue !important; }, and it also has style="color: orange". Which color does the paragraph get, and which cascade step decides it?
Show answer
Blue. The !important flag decides it at the origin-and-importance step. An important author declaration beats every normal author declaration, including the id selector and the style attribute. Specificity and source order are never consulted.
Exercise
In the stylesheet .btn { margin-top: 1rem; } followed by .btn { margin: 0; }, what is the top margin of <button class="btn">, and why? What changes if the two rules are swapped?
Show answer
The top margin is 0. Both selectors have the same specificity, so source order decides, and the later margin: 0 (which expands to margin-top: 0 and the other longhands) wins. If the rules are swapped, margin-top: 1rem comes last and wins, so the top margin is 1rem, while the other three margins stay 0.
Exercise
A child <span> inside a parent with color: red has no color rule of its own. Is this a cascade conflict between the parent and child? Where does the span's color come from?
Show answer
No. The cascade only compares declarations that target the same element, and no declaration targets the span's color. Because color is an inherited property, the span takes its parent's computed value, red, through inheritance after the cascade finds no winner.