completefrontend

What is CSS specificity?

When your CSS doesn't apply, another rule is usually winning. Here's how the browser scores selectors, why one ID beats any number of classes, where inline styles and !important fit, and how to keep specificity low.

Title card for what is CSS specificity, with selectors ranked from weakest to strongest: p at 0-0-1, .note at 0-1-0, #intro at 1-0-0, an inline style, then !important

You add a class to make a button red. You save, refresh, and the button is still blue. There's no typo, and the class is on the button. Another rule is winning the fight, and the browser has a clear reason for picking it.

CSS specificity is the score a browser gives each selector, so it can decide which rule wins when two rules set the same property on the same element. A selector is the part of a rule before the curly braces, like .button or #menu a, that picks which elements to style. IDs score highest, then classes, then element names. Inline styles beat all of them, and when two scores tie, the last rule wins.

Why isn't my CSS style applying?

When a style doesn't show up, it's usually one of these:

  • Another rule has a higher specificity. It targets the same element with a stronger selector, so yours loses.
  • Another rule has the same specificity and comes later. On a tie, the last rule wins.
  • The element has an inline style. A style attribute in the HTML beats any selector in your stylesheet.
  • Another rule uses !important. That flag lifts a declaration above all normal ones.
  • Your selector doesn't match. A typo in a class name means the rule never applies at all.

To find out which, right-click the element and choose Inspect. In Chrome DevTools, the Styles tab crosses out every declaration that lost. The same property without a line through it is the winner. Hover over a selector there to see its specificity score. The debugging in the browser lesson shows how to find your way around DevTools.

How does the browser pick the winning rule?

The process is called the cascade. It's the C in CSS, which stands for Cascading Style Sheets. When several rules give the same property different values, the browser asks a few questions in order. It stops at the first one that settles it.

  1. Does the rule apply? Rules whose selector doesn't match the element are dropped. So are rules inside a media query (a condition like "only on wide screens") that's false.
  2. Is it !important, and who wrote it? Declarations marked !important beat normal ones. Your styles also beat the browser's built-in defaults.
  3. Is it an inline style? A style written in the element's style attribute beats any selector.
  4. Which selector is more specific? The higher specificity score wins.
  5. Which comes last? If everything else ties, the declaration that appears last wins.

Steps 4 and 5 are the two to understand well. They decide between normal rules in your own stylesheets.

How do you calculate CSS specificity?

Write the score as three numbers: ID, CLASS, and TYPE. Look at each part of the selector and add 1 to its column.

  • ID column: each ID, like #menu.
  • CLASS column: each class like .button, attribute selector like [type="email"], and pseudo-class like :hover. A pseudo-class picks an element in a certain state, such as when the mouse is over it.
  • TYPE column: each element name like p or a, and each pseudo-element like ::before. A pseudo-element styles one part of an element, such as its first line.
  • No column at all: the universal selector *, and combinators. Combinators are the symbols between selectors: a space, >, +, or ~.
Selector ID CLASS TYPE Score
p 0 0 1 0-0-1
.note 0 1 0 0-1-0
p.note 0 1 1 0-1-1
nav a:hover 0 1 2 0-1-2
input[type="email"] 0 1 1 0-1-1
#menu .link 1 1 0 1-1-0
ul > li::before 0 0 3 0-0-3

How do you compare two scores?

Compare the columns from left to right, like a medal table at the Olympics. Countries are ranked by gold medals first. Silver only counts when the golds tie, and bronze only when the silvers tie too. One gold beats any pile of silver. In the same way, one ID beats any number of classes.

Three specificity columns, ID, CLASS and TYPE, with examples of each. Below them, #menu a scores 1-0-1 and beats .nav .link at 0-2-0, so the link renders green. A bar ranks what wins, from inherited values up to !important
Compare the ID column first, then CLASS, then TYPE.

Here's a link inside a menu, with two rules that disagree about its color:

<nav id="menu" class="nav">
  <a class="link" href="/">Home</a>
</nav>
#menu a {
  color: green; /* 1-0-1 */
}

.nav .link {
  color: red; /* 0-2-0 */
}

The link is green. The second rule has more classes, but the first rule has an ID, and the ID column is compared first.

When the scores are equal, order decides:

.button {
  background: blue;
}

.button {
  background: red;
}

Both scores are 0-1-0, so the later rule wins and the button is red. The same goes for whole files. A stylesheet linked later in the HTML wins ties against one linked earlier.

Do inline styles beat IDs?

Yes. An inline style is CSS written right on the element, in its style attribute:

<p id="intro" style="color: purple">Hello</p>

No selector in a stylesheet can beat it, not even #intro. You can think of an inline style as scoring 1-0-0-0, with a fourth column in front of the ID column. The only way to override it is !important.

That's why inline styles are best kept for values that JavaScript changes, like the position of something you drag. The styles and classes lesson shows when to set element.style and when to switch a class instead.

What does !important do, and why should you avoid it?

Adding !important after a value lifts that declaration above every normal declaration, whatever its specificity:

.button {
  color: red !important;
}

#save {
  color: blue; /* loses, even with an ID */
}

On a <button id="save" class="button">, the text is red.

It feels like a quick fix, but it starts a fight. The next person who needs to change that color has to write another !important. When two important declarations clash, specificity decides again. You're back where you started, with louder rules.

Using !important to win a specificity battle is a bad practice. Fix the selector instead. The fair use is overriding styles you can't edit, such as inline styles added by a third-party script. When you do, leave a comment that says why.

Why do inherited styles lose to direct rules?

Some properties, like color and font-size, pass down from a parent to the elements inside it. That's called inheritance. An inherited value always loses to a rule that targets the element directly, however weak that rule's selector is.

#page {
  color: green; /* 1-0-0, but the h1 only inherits it */
}

h1 {
  color: purple; /* 0-0-1, targets the h1 directly */
}

With <body id="page"> and an h1 inside it, the heading is purple. Specificity only compares rules that match the same element.

How do you avoid specificity problems?

  • Style with classes. When most rules use a single class, most scores are 0-1-0 and easy to override.
  • Avoid IDs in CSS. An ID beats any number of classes, so an ID rule is hard to override later. IDs are still fine in HTML, for links and form labels.
  • Keep selectors short. .card-title is easier to override than main .content .card h2.
  • Use order on purpose. Put general rules first and the specific overrides after them.
  • Use :where() for gentle defaults. Anything inside :where() scores 0-0-0, so it adds no weight and stays easy to override.

Web components add a twist. Styles inside a shadow root, a sealed-off part of a page, follow extra cascade rules. Specificity across the shadow DOM explains how they compete with page styles.

What should you learn next?

Specificity is one part of how CSS decides what an element looks like. Next, see how size and spacing work in the CSS box model, and how px, em, and rem differ. If you're weighing a utility framework, Tailwind CSS vs CSS shows how Tailwind keeps most selectors at one class. For the bigger picture, HTML vs CSS vs JavaScript explains what each language does.

When you want to change styles from code, start the JavaScript course. And for big pages, style recalculation shows how the browser matches all those selectors quickly.

Written by Shrikanth PS

Senior Software Engineer at Microsoft, working on Copilot Enterprise, with more than nine years in frontend engineering.