More CSS answers

by Danny
Published 3 min read

When a CSS rule does nothing, it is usually losing on specificity, cascade order or @layer, has a typo, or the browser is using a cached file. Here is how to debug it in DevTools.

The short answer

#

Inspect the element in DevTools and find your rule in the Styles pane. If it is crossed out, another rule is winning, usually through higher specificity, a later rule, !important or cascade layers. If it has a warning icon, the value is invalid. If it is not listed at all, the selector does not match or the stylesheet did not load.


_10
/* Your rule */
_10
.button { background: teal; }
_10
_10
/* Wins because an ID plus a class beats a single class */
_10
#header .button { background: grey; }
_10
_10
/* Fix: match or beat the specificity, or simplify the other rule */
_10
.header .button { background: teal; }

Why it happens

#

When several declarations set the same property on the same element, the browser picks one using the cascade. In simplified order, it compares:

  1. Importance. !important declarations beat normal ones.
  2. Cascade layers. For normal declarations, styles outside any @layer beat styles inside layers, and later layers beat earlier ones. For !important declarations the layer order is reversed.
  3. Specificity. IDs beat classes, attributes and pseudo-classes, which beat element names. Inline style attributes beat all selectors.
  4. Order. If everything else is equal, the rule that appears last wins, including across files, so the order of your link elements matters.

The other common causes are not about the cascade at all: a typo makes the declaration invalid and the browser skips it, the selector does not match the element you think it does, a media query is not active, or the browser is still using an old copy of the file.

How to fix it

#

Read the Styles pane

Right-click the element and choose Inspect. The Styles pane lists every rule that matches, with the rules that win the cascade at the top.

  • Crossed out: overridden. The winning rule is above it. Hover a selector to see its specificity in Chrome and Edge.
  • Warning triangle or strikethrough with an icon: invalid property or value, for example colour: red or width: 100 with no unit.
  • Greyed out: valid but has no effect here, such as justify-content on an element that is not a flex or grid container. Chrome and Firefox explain why on hover.
  • Not there at all: the selector does not match this element, or the file is not loaded.

The Computed tab shows the final value of each property; expand it to see which rule set it.

Fix specificity without an arms race

Rather than adding !important or an ID, lower the specificity of the rule that is winning, or use :where(), which has zero specificity, for base styles that should be easy to override.


_10
:where(.nav) a {
_10
color: inherit; /* specificity of just the a element */
_10
}

Check your cascade layers

If you use @layer, or a framework does, styles in a layer lose to any unlayered style, however specific the layered selector is. Put your own styles in a later layer, or declare the order up front.


_10
@layer reset, base, components, utilities;
_10
_10
@layer components {
_10
.button { background: teal; }
_10
}

Confirm the file loaded and is fresh

In the Network panel, filter by CSS and reload. A 404 means the path is wrong. If the file loads but your change is missing, it is probably cached: tick Disable cache in the Network panel while DevTools is open, or do a hard reload. In production, use file names with a content hash so each deploy gets a new URL.

Look for syntax errors

A missing closing brace can swallow every rule after it, and a missing semicolon breaks the next declaration. A stray character at the start of a selector list makes the whole rule invalid. Running a linter such as Stylelint catches these before the browser does.

Common pitfalls

#
  • Editing a different file from the one the page loads, such as a source file when the page uses a built copy.
  • CSS Modules or scoped styles renaming your class, so a plain .title selector in a global file no longer matches.
  • Expecting a property to inherit when the child has its own rule, such as browser default styles on button and input, which do not inherit font unless you set font: inherit.
  • A media query or container query that is not active at the current width.
  • Fixing everything with !important. It works once, then the next fix needs another one.
#

Was this resource helpful?