Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Custom Properties, Cascade and Global Keywords

The cascade and specificity

How the CSS cascade picks a winner: origin and importance, cascade layers, specificity and source order, with a live example and a quick scoring guide.

When several rules set the same property on the same element, the browser has to pick one value. The process is called the cascade. It does not guess: it runs a fixed list of tie-breakers in a fixed order, and it stops at the first one that finds a difference.

Knowing the order turns "why is my style ignored?" from a mystery into a short checklist.

The order of the tie-breakers

  1. Relevance. Only declarations whose selectors match the element, in applicable media, are considered.
  2. Origin and importance. Browser defaults, user styles and your styles are separate origins. Normal declarations rank user-agent < user < author. Declarations marked !important reverse that order, so important browser styles beat important author styles.
  3. Context. Shadow DOM and scoping rules decide which tree wins. Outer context beats inner for normal declarations.
  4. Element-attached styles. Inline style="..." beats stylesheet rules of the same importance.
  5. Cascade layers. Within one origin, declarations in later @layers beat earlier layers. Unlayered styles beat all layers. For !important declarations the order is reversed.
  6. Specificity. The more specific selector wins.
  7. Order of appearance. If everything else is equal, the last declaration wins.

Try it

Specificity and order

Specificity scoring

Specificity is three counters written (ids, classes, types):

Selector part Counts as
#id one ID
.class, [attr], :hover and most pseudo-classes one class
div, ::before one type
*, combinators (>, +, space), :where() nothing
:is(), :not(), :has() the most specific argument

Compare the counters left to right: (1,0,0) beats (0,10,0). Counters never carry over, so no number of classes beats one ID. Inline styles sit above all selector specificity.

Usage notes

  • Order only matters last. If one selector is more specific, it wins no matter where it is in the file.
  • Layers beat specificity. A weak selector in a later layer beats a strong selector in an earlier layer. This is why layers are a good way to organize resets, libraries and your own code.
  • Keep selectors flat. Single-class selectors are easy to override later. Avoid IDs for styling and avoid deep descendant chains.
  • Lower specificity on purpose. Wrap base selectors in :where() so they count as zero.
  • Inherited values lose to anything. Inheritance only applies when no declaration at all targets the element's property; even a low-specificity rule beats an inherited value.
  • Debug with DevTools. Crossed-out declarations are losers; the Computed panel shows which rule won.