← Back to HTML & CSS, page 1

html-css-0015 · Medium mid

How does CSS specificity work, and how do you resolve a conflict between two rules?

Specificity scores a selector in three columns: ids, then classes, attributes and pseudo-classes, then types and pseudo-elements. Columns are compared from left to right and not added together. Equal scores are settled by source order, inline styles beat selectors, and !important is checked before specificity.

Full answer

When two rules set the same property on the same element, the browser picks one by comparing specificity. Specificity is a score with three columns, compared from left to right:

  • Column A: number of ID selectors (#menu).
  • Column B: number of class selectors, attribute selectors and pseudo-classes (.item, [type="text"], :hover).
  • Column C: number of type selectors and pseudo-elements (li, ::before).

#menu .item a scores (1, 1, 1). ul li a scores (0, 0, 3). Columns are not added into one number. One ID beats any number of classes, and one class beats any number of types. The universal selector * and combinators add nothing. Inline style beats all selectors, and !important jumps over normal declarations.

If the score is equal, the rule that appears later in the stylesheet wins.

The link is green: #menu .item a has the highest score. The paragraph is black: both purple and black have the same score and black comes later.

Two functional selectors are worth knowing. :is() and :not() take the specificity of their most specific argument. :where() always counts as zero, so it is a good way to write low-specificity defaults that are easy to override.

html
<style>
  #menu .item a   { color: green; }   /* (1,1,1) */
  .item a:hover   { color: red; }     /* (0,2,1) */
  a.link.link     { color: blue; }    /* (0,2,1) */
  ul li a         { color: orange; }  /* (0,0,3) */
  :where(#menu) .item2 { color: purple; } /* (0,1,0): :where adds nothing */
  .item2          { color: black; }   /* (0,1,0), later, so it wins */
</style>
<ul id="menu"><li class="item"><a id="a" href="#" class="link">Home</a></li></ul>
<p class="item2" id="p">Text</p>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Score columns: ids, then classes/attributes/pseudo-classes, then types/pseudo-elements.
  • One id beats any number of classes.
  • Equal specificity: the later rule wins.
  • :where() adds zero; :is() and :not() use their most specific argument.
  • Importance and layers are compared before specificity.

Common mistakes

  • believing ten classes beat one ID. They do not, because columns are compared in order.
  • raising specificity with ever longer selectors or !important. This creates arms races that are hard to undo. Lower the other rule's specificity or use cascade layers.

Follow-ups

  • where does !important sit relative to specificity?
  • What is the specificity of an inline style?
  • How do cascade layers change the way you resolve conflicts?

Tips

  • When a rule will not apply, open DevTools Styles. Crossed-out declarations show which rule lost. Do not reach for !important first.
  • Practise scoring three selectors aloud as (1,1,1) style triples. It is the usual live exercise.
  • Mention :where() for low-specificity defaults to show current knowledge.