Sub topic 16
Debugging conflicting styles in developer tools
This chapter assumes you can already calculate specificity as an (IDs, classes, types) triple. You should also know the order of the cascade: origin and importance, then layers, then specificity, then source order. Here you will use browser developer tools to find out which rule won and why. The steps are the same in Chrome, Edge, Firefox, and Safari, though the panel names differ slightly.
Why guessing fails
When a style does not apply, the usual reaction is to add more selectors or an !important. That often works by accident and leaves the stylesheet harder to maintain. The browser already knows which declaration won and which lost. Developer tools show you that information, so you can fix the actual cause.
Most unexpected styles come from one of five causes:
- A more specific selector matched the same element.
- A rule with equal specificity appeared later in the source.
- The property was inherited, and a direct rule overrode the inherited value.
- The selector did not match the element at all, often because of a typo or a wrong combinator.
- The declaration itself was invalid, so the browser dropped it.
Reading the Styles pane
Right-click the element and choose Inspect. The pane that lists rules is called Styles in Chrome, Edge, and Safari, and Rules in Firefox. It lists every rule that matches the selected element, with the most important rule at the top.
Look for these signals:
- Strikethrough text. A crossed-out declaration matched the element but lost the cascade for that property.
- Warning icon or dimmed text. The browser rejected the declaration as invalid, for example an unknown property name or a bad value. An invalid declaration never takes part in the cascade.
- Source link. The file name and line number beside each rule tell you where it lives, so you can find the stylesheet to edit.
- "Inherited from" headings. These sections list rules on ancestors whose inheritable properties flow into the selected element.
- Layer labels. Rules inside an
@layerblock are grouped under the layer name, so you can see which layer each rule belongs to. - Rule order. For equal origin and layer, a higher rule in the pane has beaten a lower one through specificity or source order.
If the rule you expected is missing from the list entirely, its selector does not match the element. That is a different problem from losing the cascade, so check the selector first.
The Computed pane
The Computed pane shows the final value of every property on the element. Each property can be expanded to show the declarations that competed for it, and the winner is listed first. Use it to answer the question "which rule produced this value?" when many rules touch the same property.
The Computed pane also shows values you never wrote, such as the resolved font-size in pixels. It is the right place to confirm that an inherited value arrived, because inherited properties appear there with the ancestor's value.
Worked example: the link that stays black
Here is a page where the link should be green.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Debugging example</title>
<style>
#main-nav a { color: black; }
.link { color: green; }
</style>
</head>
<body>
<nav id="main-nav">
<a class="link" href="/docs">Docs</a>
</nav>
</body>
</html>The link renders black. Follow these steps:
- Inspect the link and open the Styles pane.
- You see
#main-nav a { color: black; }at the top and.link { color: green; }below it withcolorcrossed out. - Calculate the two specificities.
#main-nav ais (1,0,1) and.linkis (0,1,0). The ID column decides it, so the black rule wins before source order is ever considered. - Open the Computed pane and expand
color. It lists the black rule as the winner, which confirms your reading.
You now have the cause, and you can choose a fix instead of guessing. Raising the weight of .link would start an escalation. Lowering the weight of the first rule is better:
nav a { color: black; } /* (0,0,2) */
.link { color: green; } /* (0,1,0) */Now .link wins on the class column, and the order of the two rules no longer matters.
The same idea applies to layers. If the losing rule is in a later layer, no change to specificity will help, because a later layer beats an earlier one for normal declarations whatever the selectors are. The Styles pane shows the layer names, so check them before you edit selectors.
Live editing and toggling
Developer tools let you test a fix before you edit any file:
- Click the checkbox beside a declaration to turn it off. If the page changes as expected, that declaration was the cause.
- Click a value to edit it, or click in the empty space at the end of a rule to add a new declaration.
- Add a temporary rule with the "new style rule" button. The tools generate a selector from the element, which is a quick way to test a more specific selector.
- Edits live only in the browser and vanish on reload. Copy the working change into your source files.
Forcing states
Rules such as a:hover or input:focus-visible apply only while the state is active, and you cannot hold the pointer over an element while moving to the panel. Use the state toggle in the Styles pane (labelled :hov in Chrome). It lets you force :hover, :active, :focus, :focus-within, and :visited on the selected element. The matching rules then appear in the pane, and you can inspect them like any others.
Filtering the noise
An element can match dozens of rules. The filter box at the top of the Styles pane narrows the list to declarations that contain your text. Typing color shows only rules that set a colour property. In the Computed pane, a "Group" or "Show all" option controls whether properties that still have their initial values are listed.
When inheritance is the problem
A common surprise looks like this:
body { color: green; }The paragraphs turn green, but links do not. Inspect a link and you will find a rule from a "user agent stylesheet", the browser's built-in defaults, setting a link colour directly on a. Inheritance supplies a value only when no rule targets the element directly. Any direct rule beats an inherited value, even one with specificity (0,0,0), including rules from the browser's own stylesheet. The fix is to write a direct rule for the element:
a { color: inherit; }The inherit keyword makes the link take its parent's computed colour, which turns the inherited value into a direct declaration.
A debugging routine
Use the same order each time, and you will find most problems quickly.
Edge cases to remember
- Several winners. Different properties on one element can have different winning rules. Debug one property at a time.
- Shorthands.
background: grayalso resetsbackground-imageand other longhands, so a later shorthand can erase an earlier longhand without any visible strikethrough on the property you are watching. - Stale CSS. If your edit shows no effect, the browser may be serving a cached stylesheet. Reload without the cache, or check the source link in the pane to confirm which file it loaded.
!importantand layers. For important declarations the layer order reverses, so an earlier layer beats a later one. A crossed-out normal declaration may be losing to an important rule in a layer you did not expect.- Generated content. Pseudo-elements such as
::beforeappear as their own nodes in the Elements panel. Select the pseudo-element node to see its rules.
Exercises
Exercise
A page has .card p { color: gray; } and p.lead { color: navy; }. The paragraph <p class="lead"> inside a .card renders gray. In the Styles pane, which declaration is crossed out, and why?
Show answer
The color: navy declaration from p.lead is crossed out. Both selectors have specificity (0,1,1), so source order decides, and .card p appears later in the stylesheet. If p.lead came later, gray would be crossed out instead.
Exercise
You set body { font-family: Georgia, serif; }, but a <button> still shows the system font. The button's font-family appears in the Computed pane, and its source is the user agent stylesheet. Why did the inherited value not apply, and what is a one-line fix?
Show answer
Inheritance only applies when no rule targets the element directly. The browser's built-in stylesheet sets font-family on form controls, so that direct rule wins over the inherited value. The fix is button { font-family: inherit; }.
Exercise
A declaration is not crossed out, but it also has no effect, and the Styles pane shows a warning icon beside it. What does this tell you about the cascade?
Show answer
The browser rejected the declaration as invalid, for example because of a misspelled property or a bad value. An invalid declaration is dropped before the cascade runs, so it never competes with other rules. Correct the property name or value.