Skip to content

Search

Articles, tools, solutions and knowledge base guides

Start typing to search across articles, tools and apps.

CSS Specificity, Cascade and Inheritance: Why Are My Styles Not Working?

A matching selector is not a guarantee that its declaration will be used. When a button retains a vendor color, a margin disappears, or a theme token produces an unexpected result, adding !important is rarely the right starting point. Identify whether a declaration lost the cascade or failed later during value computation. Each example below has a reproducible browser outcome.

Published Written by Reading time 7 min read
On this page Contents: quick navigation between sections

Why does a later selector lose?

<button id="save" class="button primary" type="button">OK</button>
#save {
  color: #1d4ed8;
}

.button.primary {
  color: #be123c;
}

button {
  color: #047857;
}

Specificity is an ordered tuple of ID selectors, class-like selectors (including attributes and pseudo-classes), and type selectors or pseudo-elements. Compare the columns from left to right, not as a decimal score: 1-0-0 beats 0-2-0 and 0-0-1. Combinators and the universal selector add no weight. [3]

The button is blue even though button appears last. With equal origin, importance and layer, #save wins. Source order is a tie-breaker after specificity and, where applicable, @scope proximity. Inline style declarations have special precedence within author styles rather than an ordinary selector weight. [2][3]

How can you override vendor CSS without stronger selectors?

<button class="button" type="button">OK</button>
@layer vendor, components, utilities;

@layer vendor {
  button.button {
    background-color: #e2e8f0;
  }
}

@layer components {
  .button {
    background-color: #1d4ed8;
  }
}

@layer utilities {
  .button {
    background-color: #047857;
  }
}

The cascade filters nonmatching or irrelevant rules first, then evaluates origin, importance and layers. Specificity matters only within the remaining group. Keyframe animations outrank normal declarations but lose to important ones; active transitions can outrank important declarations too. [1][2]

The background is green despite the heavier vendor selector. For normal declarations, later layers outrank earlier ones, and unlayered author rules outrank named layers. Import an external sheet with @import url("vendor.css") layer(vendor); after declaring the layer order. Avoid accidentally leaving legacy rules unlayered during migration. [4]

Why does !important sometimes lose?

<button class="button" type="button">OK</button>
@layer safeguards, vendor, components;

@layer safeguards {
  .button {
    color: #ffffff !important;
    background-color: #1e293b;
  }
}

@layer vendor {
  button.button {
    color: #be123c !important;
  }
}

@layer components {
  .button {
    color: #1d4ed8;
  }
}

.button {
  color: #047857 !important;
}

Within an origin, important declarations beat normal ones, but important layers have the opposite order. Important user declarations also outrank important author declarations, which enables user accessibility overrides. [1][5]

The text is white: the important safeguards rule beats both the vendor's important rule and the unlayered important declaration. Adding more classes cannot fix a layer-order conflict. Reserve an explicit safeguards layer for exceptional vendor overrides; ordinary styling should rely on the normal cascade. [4][5]

How can :where() keep specificity low?

<div class="panel">
  <a class="link" href="#details">A</a>
  <a class="link strong" href="#details">B</a>
</div>

<div id="details"></div>
:where(.panel) a {
  color: #64748b;
}

.link {
  color: #1d4ed8;
}

:is(.panel, #legacy) .strong {
  color: #047857;
}

.panel .strong {
  color: #be123c;
}

:where() contributes zero weight regardless of its arguments. In contrast, :is(), :not() and :has() take the weight of their most specific argument, even if a different branch is the one that matches. [3]

The first link is blue because :where(.panel) a has weight 0-0-1 and .link has 0-1-0. The second link is green: :is(.panel, #legacy) .strong has 1-1-0, beating 0-2-0. Avoid unnecessary IDs in these lists. Component libraries can use :where() to retain context without making overrides difficult.

Why does a child not inherit color or spacing?

<div class="panel">
  <p class="message">A</p>
  <p class="notice">B</p>
</div>
.panel {
  color: #1d4ed8;
  padding: 24px;
}

.message {
  color: #be123c;
}

.notice {
  color: inherit;
  padding: inherit;
}

Inheritance supplies a parent's value when a property is inherited and there is no winning declaration on the child. Color and many font properties inherit by default; padding and margin do not. A declaration directly on the child beats an inherited value, even when the parent's selector contains an ID. [6][9]

The first paragraph is red with zero padding. The second is blue with 24px padding because inherit explicitly copies the parent's computed value, even for a normally non-inherited property. initial selects the specification's initial value; unset chooses inherited or initial behavior depending on the property. revert rolls back the current origin's influence. [1][6]

Why does a var() fallback fail to repair an invalid color?

<div class="theme">
  <p class="message">A</p>
</div>
.theme {
  color: #047857;
  --accent: 24px;
}

.message {
  color: #1d4ed8;
  color: var(--accent, #be123c);
}

Unregistered custom properties usually inherit and hold token sequences. var(--accent, #be123c) uses the fallback when the variable has the guaranteed-invalid value (for example, when missing or cyclic), not when an existing token sequence fails the grammar of color. [7][10]

Here --accent is 24px. Substitution makes the color invalid at computed-value time, so the paragraph inherits its parent's green rather than reverting to the earlier blue or the red fallback. Fix the token contract, for instance by registering it through @property with syntax: "<color>", inherits: true and a valid initial-value. [7][9]

How can you roll back a theme override without duplicating values?

<button class="button" type="button">A</button>
<button class="button button--default" type="button">B</button>
@layer base, theme;

@layer base {
  .button {
    background-color: #475569;
  }
}

@layer theme {
  .button {
    background-color: #1d4ed8;
  }

  .button--default {
    background-color: revert-layer;
  }
}

revert-layer removes the current layer's contribution for that property, revealing a declaration in an earlier layer when present. Unlike initial, it does not force the property's specification default. [1]

Button A has the theme's blue background; B uses the slate background from base. No duplicate base color is needed in the component variant. This is useful when a design-system variant must opt out of a theme override while retaining its base component styling.

How do you diagnose a conflict in DevTools?

<div class="card">
  <p class="description">A</p>
</div>
.description {
  margin-top: 24px;
  margin: 0;
}

.card .description {
  margin-top: 12px;
}

Select the element in Elements and filter Styles by the affected property. Crossed-out declarations indicate cascade overrides; invalid or layout-inactive declarations have separate indicators. Expand the property in Computed to trace the resolved value. [8]

In the example, margin: 0 overrides the earlier margin-top: 24px because the shorthand sets all four sides. The later .card .description changes only the top margin to 12px; the bottom remains zero. After fixing the cause, test hover, focus and applicable media queries. A targeted correction is safer than a global !important.

Resolve conflicts at their source: establish layer order, keep selectors lightweight and make theme boundaries explicit. Specificity is no substitute for CSS architecture, and inheritance is not another specificity category. Use DevTools to identify the winning declaration before changing the code. A local correction is easier to maintain than a stylesheet-wide escalation.

Tools for this topic

CSS CSS Specificity CSS Cascade CSS Inheritance Cascade Layers CSS Selectors CSS Custom Properties DevTools

Frequently asked questions

Does an ID selector always win?

No. Origin, importance and layers are considered first. An ID only wins in a comparable group of declarations. [2][3]

Does !important increase specificity?

No. It changes declaration importance, while specificity remains a separate criterion. [5]

How do the cascade and inheritance differ?

The cascade chooses a declaration for a property on an element; inheritance supplies the parent's value according to the property's rules. [1][6]

Why is CSS not working despite a matching selector?

Check rule conditions, origin, layer, importance, shorthands and computed values. Compare Styles and Computed in DevTools. [8][9]

Sources and references

  1. W3C, CSS Cascading and Inheritance Level 5↩1↩2↩3↩4↩5↩6
  2. MDN, Introduction to the CSS cascade↩1↩2↩3↩4
  3. MDN, Specificity↩1↩2↩3↩4↩5
  4. MDN, Cascade layers↩1↩2↩3↩4
  5. MDN, !important↩1↩2↩3↩4
  6. MDN, Inheritance↩1↩2↩3↩4
  7. MDN, Using CSS custom properties↩1↩2↩3
  8. Chrome DevTools, Find invalid, overridden, inactive, and other CSS↩1↩2
  9. MDN, CSS property value processing↩1↩2↩3↩4
  10. MDN, var()↩

Was this helpful?

Get new articles by email

One short email per new Learning article. No spam, unsubscribe in one click.

We only use your email to send new articles. No third-party sharing.

Back to Learning