Sub topic 12
Debugging the Cascade with Developer Tools
This chapter assumes you can open your browser's developer tools and inspect an element. It also assumes you know the cascade criteria from earlier chapters: origin and importance, layers, specificity, and source order. Here you will turn those criteria into a repeatable debugging routine.
Ask the right question
When a style looks wrong, don't guess and don't reach for !important. Ask which declarations compete for this property on this element, and which criterion picked the winner. The developer tools answer both questions.
The Styles panel
The Styles panel lists every rule that matches the selected element. Rules are ordered by precedence, so the winner is at the top. Note these details:
- A struck-through declaration lost the cascade for that property.
- Each rule shows its selector and the file and line where it was written.
- Rules from the browser's default stylesheet appear labeled as user agent styles.
- Browsers label rules that belong to a cascade layer, which helps when a less specific selector wins because of its layer.
- Sections titled "Inherited from ..." show rules on ancestors that pass values down.
- A warning icon marks a value the browser could not parse or a property that has no effect here.
You can untick a declaration to see what happens without it, and you can edit values live.
The Computed panel
The Computed panel shows the final value of each property for the selected element. Expand a property to see which rules contributed, with the winning declaration first. This is the quickest way to find where an inherited value came from, such as a color set on body. For layout properties such as width, the panel shows the used value in pixels, not the computed auto.
A worked example
You expect the link to be navy, but it renders crimson.
<aside id="sidebar">
<div class="card"><a href="/docs">Docs</a></div>
</aside>.card a { color: navy; }
#sidebar a { color: crimson; }Inspect the link. In Styles, .card a { color: navy; } is struck through. Its specificity is (0,1,1). The #sidebar a rule scores (1,0,1), so the id selector wins. Source order does not matter, because specificity is compared first.
The fix depends on intent. If the sidebar rule is only a default, lower its specificity with :where():
.card a { color: navy; }
:where(#sidebar) a { color: crimson; }Now the sidebar rule scores (0,0,1) and loses to .card a, so the link is navy. Reload and check Computed to confirm.
Reading common symptoms
| Symptom | Likely cause | What to check |
|---|---|---|
| Declaration is struck through | Another rule won on layer, specificity, or order | The rule at the top of the list for that property |
| Declaration is not listed | Selector does not match, such as a class typo | The element's `class` attribute and the selector |
| Warning icon next to a value | Invalid value or property that does not apply | The property name, value syntax, and `display` type |
| Child shows the initial value | Property is not inherited | Whether it is a box property such as `border` or `margin` |
| `var()` value ignored | Custom property is undefined at that element | The "Inherited from" sections for the `--name` |
Debugging inherited values
If a property has no rule of its own on the element, look under "Inherited from". Only inherited properties appear there. If you expected a value to pass down and it did not, check whether the property inherits. Form controls are a common case, because the browser's default stylesheet sets their own font and color:
button, input {
font: inherit;
color: inherit;
}After adding this, the Styles panel shows your rule beating the user agent rule for those properties. The controls now take their values from the parent.
Summary of the routine
- Select the element and find the property in Styles.
- If your rule is missing, fix the selector. If it is struck through, find the winner and compare its criteria.
- Confirm the final value in Computed.
- Change the source on purpose, then reload and recheck.
Exercise
A paragraph inside .article should be gray, but it renders black. In Styles, .article p { color: gray; } is struck through, and the winning rule is p { color: black; } in a later @layer overrides block, while your rule is in an earlier layer base. Layers are declared as @layer base, overrides;. Why does the less specific rule win, and what is one fix that avoids !important?
Show answer
For normal declarations, a rule in a later layer beats a rule in an earlier layer regardless of specificity. overrides comes after base, so p wins. One fix is to move the gray rule into overrides (or a later layer). Another is to reorder the layer declaration so base comes last.