Sub topic 12
Debugging the cascade with developer tools
This chapter assumes you know how the cascade picks a winner: origin and importance, then cascade layers, then specificity, then source order. It also assumes you know which properties inherit and how var() resolves. Here you will use browser developer tools to find out which rule won and why, instead of guessing.
The panel names below follow Chrome and Edge. Firefox has the same features under slightly different names, and the differences are noted where they matter.
The two panels that matter
Two panels do most of the work in cascade debugging.
- The Styles panel (called Rules in Firefox) lists every rule that matches the selected element. It shows the rules in cascade order, with the winner nearest the top. It answers the question "which declarations compete here?"
- The Computed panel lists the final value of every property on the element. It answers the question "what did the browser actually use?"
Use them in that order. Start with the Computed panel to confirm what is wrong, then use the Styles panel to find out why.
Reading the Styles panel
Select an element in the Elements (Inspector) panel and look at the Styles panel. Each block is one matching rule. The tool shows the selector, the declarations, and a link to the file and line where the rule is defined.
Some markings are worth recognizing:
- A struck-through declaration lost the cascade for that property. Some other declaration, usually in a block above it, won.
- Rules appear roughly from highest to lowest precedence. The
element.styleblock (inline styles) sits at the top, and user-agent rules sit at the bottom, labeled "user agent stylesheet". - A warning icon next to a declaration means the browser dropped it. The property name may be misspelled, or the value may be invalid for that property. A dropped declaration does not take part in the cascade at all. That means an earlier valid declaration for the same property can still apply.
- Sections titled "Inherited from
<ancestor>" show inherited properties only. Declarations for non-inherited properties are not listed there. - When a rule lives inside a cascade layer, the panel labels the block with the layer name. Chrome also gives you a layer view of the whole cascade. In Firefox, check the rule's origin and layer information in the Rules panel.
Hovering over a var() reference shows what it resolved to in Chrome. Hovering over a color swatch or a value can show more detail.
Reading the Computed panel
The Computed panel shows the final value per property. For lengths, that is the resolved value, such as 16px for 1rem at a 16px root size. Layout values such as width can also show used values.
Each property row has a small arrow (Chrome) or can be expanded (Firefox). Following it takes you to the declaration that produced the value. Firefox lists every competing rule for that property in order, with the winner first.
Two settings help:
- Filter by property name to find the one you care about, for example
color. - Chrome hides properties that still have their initial value unless you tick "Show all". Firefox hides browser (user-agent) styles unless you tick "Browser Styles".
A worked example
This page has a call-to-action button that should be green but renders blue. The markup and styles are below.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Cascade debugging</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<main id="page">
<p class="intro">Ready to start?</p>
<button class="btn cta">Sign up</button>
</main>
</body>
</html>@layer base, components;
@layer base {
button { background-color: gray; color: white; }
}
@layer components {
.btn { background-color: steelblue; }
.cta { background-color: seagreen; }
}
#page button { background-color: steelblue; }
.intro { colour: red; }The .cta rule comes later than .btn and has equal specificity, so it looks like it should win. The button is still blue.
Step 1: Confirm the symptom in the Computed panel
Select the button and filter the Computed panel for background-color. It reports rgb(70, 130, 180), which is steelblue. The button is not green, so something else is winning. Click the arrow next to the value. The tool jumps to #page button { background-color: steelblue; } in the Styles panel.
Step 2: Look at the order of rules
In the Styles panel, the #page button block sits above the two @layer components blocks, and .cta is struck through. Compare the three competing selectors:
| Selector | Specificity | Where it lives |
|---|---|---|
| `button` | (0,0,1) | layer `base` |
| `.btn` and `.cta` | (0,1,0) | layer `components` |
| `#page button` | (1,0,1) | no layer (unlayered) |
The unlayered rule beats any layered rule for normal declarations, whatever the specificity. Specificity and source order are never compared across that boundary. So .cta loses without ever being weighed against #page button.
Step 3: Test a fix in the tool first
Before you edit any files, try the change in the browser.
- Untick the checkbox next to
background-colorin the#page buttonblock. The button turns green, which proves that this rule is the cause. - Decide how to fix it for real. Putting the
#page buttonrule inside@layer componentsmakes.ctawin, because it is later and more specific thanbutton... and the id selector would still beat it within the layer. Removing the rule is cleaner.
Edits made in DevTools are temporary. They disappear on reload, so copy the final change into your source files.
Step 4: Check for dropped declarations
Select the paragraph with class intro. In the Styles panel, colour: red has a warning icon and is struck through, because colour is not a CSS property. The paragraph keeps its inherited color. Fix the spelling to color.
@layer base, components;
@layer base {
button { background-color: gray; color: white; }
}
@layer components {
.btn { background-color: steelblue; }
.cta { background-color: seagreen; }
}
.intro { color: red; }With the #page button rule removed, the Computed panel now reports rgb(46, 139, 87) (seagreen) for the button.
A repeatable method
Follow the same sequence each time.
- Select the element. Use the element picker on the page rather than hunting in the tree, so you are sure you have the right one.
- Find the property in the Computed panel. Note the value you got and the value you wanted.
- Follow the arrow to the winning declaration.
- Find your intended declaration in the Styles panel. Is it present, struck through, or missing?
- If it is present and struck through, work through the cascade criteria in order to find the reason.
- If it is missing, the selector did not match, the declaration was invalid, or the stylesheet never loaded.
The diagram shows the questions to ask, in the order the cascade uses them.
Problems the panels reveal
Many cascade bugs match one of a few patterns. This table maps what you see to the usual cause.
| What you see | Likely cause | What to check |
|---|---|---|
| Your rule is not listed at all | The selector does not match, or the file did not load | The Network panel, a typo in the selector, or an element state such as `:hover` |
| Your declaration has a warning icon | Invalid property name or value | Spelling, units, and missing semicolons on the line above |
| Your declaration is struck through, and the winner has a higher-specificity selector | Specificity | Compare the id, class, and element counts of each selector |
| Your declaration is struck through, and the winner has the same specificity | Source order | Which stylesheet loads later, and the `<link>` order |
| An unlayered rule beats a more specific layered one | Layers | Whether either rule sits in an `@layer` block |
| A value is inherited when you expected a reset | Inheritance | The "Inherited from" section, and whether the property inherits |
| A `var()` value is wrong | Custom property scope | Which ancestor defines the property, and the Computed value |
A missing semicolon is a common cause of dropped declarations. The parser reads the next line as part of the value, and the browser then discards the whole declaration.
Debugging states and inherited values
Forcing element states
Rules such as a:hover and input:focus only match while the state is active, and moving the mouse to the DevTools panel ends a hover. Use the state toggle in the Styles panel (the :hov button in Chrome, the :hov button in Firefox's Rules panel). It lets you force :hover, :active, :focus, and :visited, and then inspect which rules apply.
Tracing an inherited value
When a child shows a color or font you did not set, look at the "Inherited from" sections in the Styles panel. The nearest ancestor that sets the property wins, because the child inherits that ancestor's computed value. Follow the Computed panel's arrow to be sure. If a non-inherited property such as border shows up on a child, it was set directly on that child, not inherited.
Custom properties
A var() that resolves to something unexpected usually means the custom property is defined on the wrong ancestor, or is defined nowhere. Custom properties inherit, so select the element and read the "Inherited from" sections for -- names. If a var() reference has no definition and no fallback, the declaration is invalid at computed-value time. The property then behaves as unset: it inherits if it is an inherited property, and otherwise takes its initial value. The panel may show the declaration struck through or the property with an unexpected value.
.card {
--gap: 1rem;
padding: var(--gap, 0.5rem);
margin: var(--space);
}In this example, padding falls back to 0.5rem only when --gap is not defined. margin has no fallback, so with no --space defined anywhere the Computed panel shows margin at its initial value, 0.
Testing changes safely
DevTools lets you experiment without touching the source.
- Tick and untick declarations to see their effect immediately.
- Click a value and use the arrow keys to adjust it.
- Add a new declaration inside any block, or add a new rule with the
+button. The tool puts the new rule in an inspector stylesheet that loads after your own, so it wins ties on source order. - Use the Changes panel (Chrome) or Changes tab (Firefox) to see a diff of everything you changed, then copy those edits back into your files.
Be careful with that last point about source order. A rule created through the + button may win in the tool and still lose in production once your real stylesheet order and layers apply. Always reload and retest after you edit the source.
Exercise
A link inside <nav id="menu"> should be white, but the Computed panel shows color: rgb(0, 0, 238), the browser default for links. The Styles panel shows nav a { color: white; } with no strikethrough and no warning icon, listed above a "user agent stylesheet" block. What do you check next?
Show answer
A matching, unstruck declaration that is not winning is unusual, so check whether it is actually the one being applied. Look at the Computed panel's arrow for color. If it leads to the user-agent rule, then your rule is not matching the element in its current state. For example, the link may be the :visited state, or your selector may match a different element than the one you selected. If the arrow leads to your rule and the value still differs, the rule may be inside a layer that loses to a later layer. Compare the layer labels and the order in which the layers were declared.
Exercise
In the Styles panel you see .card { padding: 2rm; } with a warning icon, and an earlier rule .card { padding: 1rem; } that is struck through. What does the Computed panel report for padding, and why?
Show answer
The Computed panel reports 1rem resolved to pixels (16px with a default root size). 2rm is not a valid unit, so the browser drops that declaration before the cascade runs. Because the invalid declaration is dropped, the earlier valid padding: 1rem is not overridden and it applies. The Styles panel may show the earlier rule struck through only if another valid declaration beats it, so check which block the Computed arrow points to.