Skip to content
Published on
2 min read

Why this CSS rule lost

Authors
  • Ritenta
    Name
    Ritenta

When a CSS declaration is crossed out in DevTools, adding a longer selector feels tempting. That can fail because specificity is only one stage of the cascade. First ask whether the rule applies at all. Then inspect its origin, importance and layer before comparing selectors.

Follow the browser's decision

For one property on one element, the browser filters out irrelevant rules: selectors that do not match and media conditions that are false. It then compares origin and importance, including cascade layers. Only declarations still competing at the same precedence reach specificity and source order. MDN lays out the cascade algorithm.

Consider this intentionally surprising example:

@layer components {
  #account .notice { color: navy; }
}

.notice { color: teal; }

For a normal author declaration, the unlayered .notice wins over the more specific selector inside components. Increasing the ID count inside that layer does not solve the ordering problem. You could move the override into the intended layer, put both rules in ordered layers, or deliberately leave the override unlayered. Choose one strategy and document it. With !important, layer precedence reverses, so check the actual declarations instead of memorizing a single shortcut.

A repeatable DevTools check

Select the element and property in the Styles pane. Confirm the losing rule matches the element and that its media or container condition is active. Look for a different origin, an !important marker, a layer name, and only then the selector weight and later source order. Toggle the winning declaration off to confirm the losing one takes effect; that makes the causal relationship visible.

This procedure is especially useful when a design system introduces layers or a component library injects styles. A selector that looks “stronger” in isolation may be competing in a different part of the cascade.