CSS Specificity: plainly explained.

CSS specificity decides which rule wins when selectors conflict. Learn how it is calculated, with examples, and how to avoid specificity wars.

Website · Updated Nov 7, 2026

DEFINITION

CSS specificity is the scoring system browsers use to decide which CSS rule applies when several selectors target the same element.

CATEGORY

Website

RELATED

CSS variables, Media query

Summarize this page with AI

01

What is CSS specificity?

CSS specificity is the algorithm a browser uses to choose which declaration wins when two or more rules target the same element with conflicting values. Each selector receives a weight, and the heavier selector applies, whatever its position in the stylesheet. Understanding it explains most of the "why is my style not applied?" situations in front-end work.

02

How specificity is calculated

Specificity is usually written as three or four numbers, from the strongest to the weakest category:

  • Inline styles (the style attribute) beat any selector.
  • IDs such as #header count in the first column.
  • Classes, attributes and pseudo-classes such as .card, [type="text"] and :hover count in the second.
  • Elements and pseudo-elements such as div and ::before count in the third.

The universal selector * and combinators add nothing. Columns are compared left to right and never carry over: ten classes never beat one ID.

03

Example in code

p { color: black; } scores (0,0,1).

.intro { color: blue; } scores (0,1,0).

#hero .intro { color: red; } scores (1,1,0).

On a paragraph with the class intro inside #hero, the text is red, even if the black rule is written last.

04

Specificity scores at a glance

Selector
*Score (ID, class, element)0,0,0CommentNo weight
liScore (ID, class, element)0,0,1CommentElement selector
.menu liScore (ID, class, element)0,1,1CommentOne class and one element
a:hoverScore (ID, class, element)0,1,1CommentPseudo-class counts as a class
#nav aScore (ID, class, element)1,0,1CommentOne ID outweighs any number of classes
05

Specificity, the cascade and !important

Specificity is only one step of the cascade. The browser first compares origin and importance (!important wins), then cascade layers, then specificity, and finally source order when scores are equal. Overusing !important is a trap: the only way to override it is another !important with higher specificity, which quickly makes a stylesheet unmaintainable.

06

Modern tools to control specificity

  • :where() always has zero specificity, which is ideal for resets and defaults.
  • :is() and :not() take the specificity of their most specific argument.
  • @layer lets you order groups of rules explicitly, so a base layer never beats a component layer.
  • CSS variables reduce the need for overrides by changing a value instead of a selector.
07

Best practices

  • Style with classes, not IDs or deep descendant chains.
  • Keep selectors short and flat, and use a naming convention such as BEM.
  • Avoid !important except for utilities that must always win.
  • Check the computed rule in the browser DevTools before adding more weight.
08

CSS specificity at BeBranded

When we build sites in Webflow, we follow a class-first approach with a consistent naming system, which keeps specificity low and predictable and makes redesigns safer. Discover our website service.

FAQ

Questions about CSS Specificity.

Another question?

Write to us
What is CSS specificity?
CSS specificity is the weight given to a selector. When several rules conflict on the same element, the one with the highest specificity is applied.
How is CSS specificity calculated?
Count inline styles, then IDs, then classes, attributes and pseudo-classes, then elements and pseudo-elements. Columns are compared from left to right and never carry over.
Does an ID always beat classes?
Yes. A single ID outweighs any number of classes, because the ID column is compared before the class column.
What happens if two selectors have the same specificity?
The rule that appears last in the source order wins, unless a cascade layer or !important changes the outcome.
Why should I avoid !important?
It overrides normal specificity, so the only way to beat it is another !important. This creates conflicts that are hard to debug and maintain.
What does :where() do for specificity?
The :where() pseudo-class always has zero specificity. It is useful for defaults and resets that must stay easy to override.

Related

Related terms, and where to go next.

Illustration for the article “When custom website services are worth the investment”
Development

When custom website services are worth the investment

Learn when custom website services justify the cost, how to assess ROI, avoid scope mistakes and choose the right build approach.

Read the article
Illustration for the article “Cool-looking websites still need a clear conversion path”
Development

Cool-looking websites still need a clear conversion path

Cool-looking websites only convert when the next step is obvious. Use this framework to align design, messaging, proof and conversion paths.

Read the article
Illustration for the article “How to create a web page that moves visitors to action”
Development

How to create a web page that moves visitors to action

Learn how to create a web page that moves visitors to act: clear goal, persuasive structure, focused design, concrete CTAs and a launch checklist.

Read the article

Let’s talk

Tell us what you want to build.

Thirty minutes with the people who will work on your project. You leave with a clear view of the scope, the timeline and the next steps.

What happens next

  1. 1A 30-minute callWe listen to your goals, your constraints and your context.
  2. 2A clear scopeWe map what is worth doing first, and what it takes.
  3. 3A costed proposalYou decide, with no commitment and no sales pitch.