Q5: How is CSS Specificity calculated?
Specificity is calculated as a 3-column vector score: IDs (1,0,0), Classes / Attributes / Pseudo-classes (0,1,0), and Elements / Pseudo-elements (0,0,1). Inline styles carry (1,0,0,0) weight. !important bypasses standard specificity weight comparisons entirely.
Specificity Weight Calculation
/* Score: (0,0,1) */
p { color: black; }
/* Score: (0,1,1) */
p.highlight { color: blue; }
/* Score: (1,0,0) - Wins over class selectors */
#main-content { color: red; }#main-content wins because its ID score (1,0,0) outweighs class and tag scores.
Key Takeaways
Specificity hierarchy: Inline Styles > IDs > Classes/Attributes/Pseudo-classes > Elements.
Equal specificity rules resolve by giving precedence to whichever rule appears last in stylesheet order.
Knowledge Check
Which selector carries the highest calculated specificity score?