Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

CSS cascade and specificity

The cascade chooses a declaration for a particular property on an element. Specificity is only one criterion: origins, !important, and layers can settle a conflict before selector specificity is compared.

Version: CSS Cascading and Inheritance Levels 5 and 6 and Selectors Level 4 (editor’s drafts). The usage example’s console output was verified in Chromium 157.0.8103.51; this does not establish broader browser compatibility.

Syntax

selector { property: value; }
selector { property: value !important; }
@layer base, overrides;

The cascade resolves conflicts between declarations for the same property on the same element. It considers matching rules and satisfied conditions such as @media. This is not inheritance: inheritance can supply a value from the parent when defaulting calls for it.

Origin and importance come first. From highest priority: active transitions, important browser, user, and author declarations, animations, then normal author, user, and browser declarations. !important does not increase specificity. Encapsulation context, such as Shadow DOM, and declarations attached directly through the style attribute also matter; inline styles do not win unconditionally.

Within a comparable context, normal declarations in later @layer layers beat earlier layers, and unlayered declarations beat layered ones. For !important, layer order reverses, and important layered declarations beat unlayered ones. Only after these decisions is selector specificity compared.

Specificity is a tuple compared left to right: IDs; classes, attributes, and pseudo-classes; types and pseudo-elements. It is not a decimal number. :where() has zero specificity; :is(), :not(), and :has() take the greatest specificity of an argument. When specificity ties, proximity to an @scope root precedes source order; the last declaration wins only if earlier criteria still tie.

In the example, the overrides layer beats base despite the #message selector. Within overrides, the second .note selector wins because it appears later. The expected console output is rgb(0, 128, 0). The cited documents are editor’s drafts, not a guarantee of browser support.

Examples

<!doctype html>
<meta charset="utf-8">
<title>Cascade</title>
<style>
  @layer base, overrides;
  @layer base {
    #message { color: red; }
  }
  @layer overrides {
    .note { color: blue; }
    .note { color: green; }
  }
</style>
<p id="message" class="note">Cascade</p>
<script>
  console.log(getComputedStyle(document.getElementById('message')).color);
</script>

Result

rgb(0, 128, 0)

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources