liveBook · Manning

Specificity

As explained in Tiny CSS Projects

CSS specificity is the precedence score used to determine which competing selector-based declarations apply to an element.

Specificity is the precedence score CSS uses when competing declarations could style the same element. The browser first considers whether a value is inherited, then compares selector types and their calculated values; the greatest value wins. section 1.5

Selector types contribute different amounts to a specificity value:

  • ID selectors contribute 100. section 1.5
  • Class selectors, attribute selectors, and pseudo-classes contribute 10. section 1.5
  • Type selectors and pseudo-elements contribute 1; universal selectors contribute 0. section 1.5
  • If values tie, stylesheet origin and declaration order decide; a later rule in the same stylesheet wins. section 1.4 section 1.5

Override only the columns that need it

A general th rule can left-align every table header, while the more specific th:nth-of-type(n+3) rule overrides text-align for later columns and keeps them right-aligned. This uses the cascade deliberately instead of adding stronger selectors everywhere. section 8.4.2

:where() always has specificity 0, while :is() takes the specificity of its most specific argument. Thus input[type="radio"]:checked and :is(input[type="radio"], input[type="checkbox"]):checked calculate to 021, overriding a 011 selector. section 10.3.9

Inline styles take very high priority but couple presentation to HTML and become repetitive. Use !important cautiously: it short-circuits the normal cascade, complicates maintenance, and should generally be avoided in author stylesheets. section 1.3.1 section 1.4.6

How Tiny CSS Projects covers it

How deep it goes
Explains selector scoring, inheritance, stylesheet order, !important, and modern :is() and :where(), without formal browser internals.
Examples
Uses inherited body and paragraph colors, table header alignment, inline headings, and styled radio buttons and checkboxes.
What it assumes
Assumes basic HTML and CSS rules while introducing inheritance, selector categories, and precedence incrementally.
What you can do after
Diagnose conflicting declarations and apply targeted selector, order, inheritance, or cascade changes.
How it connects
Connects specificity with the cascade, inheritance, selector types, stylesheet origins, responsive design, and accessibility.

Also explained in

Compare how 6 books explain specificity →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Tiny CSS Projects. The section links point to the parts of the book it draws on.