Skip to content
tezvyn:

CSS Specificity: The Browser's Tie-Breaking Rule

Source: developer.mozilla.orgEasyHow cards are made

CSS Specificity: The Browser's Tie-Breaking Rule

CSS Specificity is the browser's tie-breaking system for conflicting styles. Think of it as a score: an ID is worth more than any number of classes, and a class is worth more than any number of elements. The footgun: using !important to win.

Why it exists

A single HTML element can be targeted by multiple CSS rules, potentially with conflicting properties. The browser needs a deterministic way to decide which style to apply. Specificity is the algorithm that calculates a selector's 'weight' to resolve these conflicts predictably.

The mental model

Think of specificity as a three-digit score for each CSS selector, written as ID-CLASS-TYPE. When comparing two selectors, the browser checks the score from left to right. A single point in the ID column is worth more than any number of points in the CLASS column. For example, a score of 1-0-0 (one ID) will always beat a score of 0-20-0 (twenty classes).

How it works

The score is calculated by counting the number of components in each category:

Id column

This column counts the number of ID selectors in the full selector (e.g., #main-nav). Each ID adds one to this column.

Class column

This column counts class selectors (.btn), attribute selectors ([type="radio"]), and pseudo-classes (:hover). Each one adds one to this column.

Type column

This column counts element/type selectors (div, h1, p) and pseudo-elements (::before, ::placeholder). Each one adds one to this column.

The universal selector (*), combinators (>, +, ~), and the :where() pseudo-class have a score of 0-0-0 and do not affect specificity. If two selectors have the exact same specificity score, the one that appears later in the CSS file wins.

When to use it

You use specificity every time you write CSS, whether you realize it or not. You leverage it intentionally when you need to override a general style with a more targeted one. For example, you might have a global style for all links (a) but use a more specific selector (.footer a) to make links in the footer a different color.

When not to use it

Avoid creating overly specific selectors, like #page .sidebar ul li a. This practice, known as a 'specificity war,' forces you to write even more specific selectors to override styles later, often leading to the use of !important. This makes the codebase brittle and difficult to maintain. Always aim for the lowest specificity that correctly applies your style.

One canonical example

Consider this CSS and HTML:

Css

p { color: blue; } / Score: 0-0-1 / .lead { color: green; } / Score: 0-1-0 / #intro p { color: red; } / Score: 1-0-1 /

Html

Hello!

The text 'Hello!' will be red. The selector #intro p has a score of 1-0-1, which beats .lead (0-1-0) because its ID column value is higher. It also beats p (0-0-1).

Interview question

An HTML div element has id="main" and class="active". Which CSS rule will apply its color?

  • a.#main { color: blue; }
  • b.div.active { color: purple; }
  • c.div#main { color: red; }Correct
  • d..active { color: green; }
Why?

The rule div#main has a specificity score of 1-0-1 (one ID, one type selector), which is higher than #main (1-0-0), .active (0-1-0), and div.active (0-1-1) when comparing the scores from left to right. The ID column takes precedence over the class and type columns.

Just read this? Test yourself on what you have been reading.

Read the original → developer.mozilla.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on css — each one lists the topics its interview covers.

See open roles