Understanding CSS Specificity (Without the Headaches)

How the three-part system actually works—and why it matters more than you think

Conceptual CSS artwork for Understanding CSS Specificity (Without the Headaches).

CSS specificity has a reputation.

It is one of those things you learn early, sort of understand, and then occasionally fight with when something refuses to behave.

You add a class. It does not work. You make the selector more specific. It still does not work. Eventually you reach for !important and move on.

The problem is not that specificity is complicated—it is that it is easy to misunderstand how it actually works.

Why specificity feels unpredictable

At first glance, specificity seems simple. More specific selectors win.

The confusion comes from what “more specific” actually means.

A long selector is not necessarily more specific. A nested selector is not automatically stronger. And sometimes a single ID will beat a selector that looks far more complex.

Without a clear mental model, it starts to feel like guesswork.

What specificity actually is

Specificity is how the browser decides which CSS rule wins when multiple rules target the same element.

It is not about order. It is not about file structure. It is a scoring system.

Every selector is assigned a value based on what it contains, and the browser compares those values to determine the winner.

That value is usually represented as three numbers.

0, 0, 0

Once you understand what those numbers represent, the system becomes much easier to reason about.

The three-part system

Specificity is made up of three components, read from left to right.

The first number represents IDs. The second represents classes, attributes, and pseudo-classes. The third represents element selectors and pseudo-elements.

ID, CLASS, ELEMENT

Each part is counted separately. They are not combined into a single number—they are compared in order, from left to right.

That detail is what trips people up the most.

How each part is calculated

Let’s look at what contributes to each value.

IDs are the strongest contributors. Every ID selector adds one to the first number.

#header {
  /* 1, 0, 0 */
}

Classes, attributes, and pseudo-classes all contribute to the second number.

.card,
[type="text"],
:hover {
  /* 0, 1, 0 */
}

Element selectors and pseudo-elements contribute to the third number.

div,
h1,
::before {
  /* 0, 0, 1 */
}

When you combine them, the counts add up within each category.

#nav .item a {
  /* 1, 1, 1 */
}

One ID, one class, one element.

How the browser compares specificity

The browser does not add these numbers together. It compares them from left to right.

That means the first value (IDs) is the most important. If two selectors differ there, the rest does not matter.

For example:

#nav a {
  /* 1, 0, 1 */
}

.main .link {
  /* 0, 2, 0 */
}

Even though the second selector has more classes, the first one wins—because it has an ID.

Only when the first values are equal does the browser move to the second number, and then the third.

Think of it less like math, and more like a priority system.

Where things usually go wrong

One common misunderstanding is assuming that longer selectors are stronger.

In reality, repeating element selectors does very little compared to adding a single class or ID.

div div div {
  /* 0, 0, 3 */
}

.card {
  /* 0, 1, 0 */
}

The class selector wins, even though the first one looks more complex.

Another source of confusion is pseudo-classes. They count the same as classes, which means something like :hover can influence specificity more than expected.

These small details are where specificity starts to feel unpredictable—until you see the pattern.

Why specificity matters less—and more—today

Modern CSS gives you tools like @layer, :where(), and better component patterns that reduce the need to fight specificity.

But that does not make specificity irrelevant.

It still determines how rules interact inside a layer, inside a component, and across a codebase.

Understanding it means you can avoid problems instead of reacting to them.

The goal is not to “win” specificity—it is to control it.

What to watch for

The biggest trap is escalation. Once you start increasing specificity to fix a problem, it becomes harder to override things later.

That is how stylesheets become fragile over time.

It is also worth remembering that !important sits outside this system entirely. It overrides specificity, which is why it should be used carefully.

Rule of thumb:
Keep specificity as low as possible, and only increase it intentionally.

Quick answers

Does order ever matter?

Yes—but only when specificity is equal. In that case, the later rule wins.

Are IDs bad?

Not inherently, but they are very strong. Using them makes styles harder to override later.

What is the easiest way to manage specificity?

Keep selectors simple, use classes consistently, and rely on structure rather than escalation.

Want to go deeper?

Specificity is one of those concepts that becomes clearer the more you see it in real code. Once the three-part system clicks, it stops feeling like guesswork and starts feeling predictable.

It is not about memorizing rules—it is about understanding how the browser thinks.