Tutorials Courses Interview Prep (100 Q&A) AI Tools (50) Developer Tools (50) Tracks Pricing Plans Blog & Articles Contact Us
CSS Syntax, Selectors & The Cascade 16 mins read Free Lesson

CSS Selectors, Combinators & Specificity

Master type, class, ID, attribute selectors, descendant/child combinators, and the specificity calculation hierarchy.

Specificity Calculation (0, 0, 0, 0)

When multiple CSS rules target the same element, the browser applies the rule with the highest specificity score:

  1. Inline styles (style="...") = (1, 0, 0, 0)
  2. IDs (#header) = (0, 1, 0, 0)
  3. Classes, attributes, pseudo-classes (.btn, [type="text"], :hover) = (0, 0, 1, 0)
  4. Elements & pseudo-elements (h1, div, ::after) = (0, 0, 0, 1)
/* Specificity: (0, 0, 0, 1) */
p { color: black; }

/* Specificity: (0, 0, 1, 0) - WINS over element */
.text-muted { color: gray; }

/* Specificity: (0, 1, 1, 0) - WINS over class */
#main-content .text-muted { color: slateblue; }