liveBook · Manning

Specificity

As explained in Web Components in Action

CSS specificity is the precedence system that determines which competing stylesheet declarations apply to an element.

CSS specificity determines which competing declarations apply. In component-based pages, a selector such as `sample-component .increment-number` narrows styling, but styles can still leak into descendants or creep in from parent and page-level rules. section 9.1

Use specificity as a practical debugging lens:

  • Scope ordinary element rules to the component that owns them instead of styling every matching element globally. section 9.1
  • Check both directions of style creep: component rules can affect nested components, while page-level rules can affect component internals. section 9.1
  • Remember that CSS custom properties inherit; a more specifically scoped value takes precedence, while `:root` provides a low-specificity baseline. section 10.2

Narrowing a button rule

A global `button` rule can restyle the stepper's buttons. Replacing it with `sample-component button.big-button` targets the component's buttons, and the added declarations remove effects such as shadows and padding that broke the component. section 9.1

sample-component button.big-button {
  box-shadow: none;
  text-shadow: none;
  padding: 0;
}

Inspect the selector and declarations used to neutralize global button styling inside the component. section 9.1

How Web Components in Action covers it

How deep it goes
Explains selector scoping, conflicting button rules, inherited variables, and `:root` baselines through working examples.
Examples
Uses a styled stepper, global button rules, scoped button overrides, and inherited theme variables.
What you can do after
Diagnose style creep by narrowing selectors and tracing inherited custom-property values across component boundaries.
How it connects
Connects specificity with CSS selectors, inheritance, CSS variables, Web Components, and Shadow DOM.

Also explained in

Compare how 6 books explain specificity →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Components in Action. The section links point to the parts of the book it draws on.