Css specificity calculator cropped

Examples:

How specificity works: every selector gets four numbers (inline styles, IDs, classes, elements). Inline styles are always 0 here, because these are selectors, not inline styles. Count 1 for each ID selector (#header), 1 for each class selector (.nav), attribute selector ([type="text"]) and pseudo-class (:hover), and 1 for each type selector (div, a) and pseudo-element (::before). The universal selector (*) counts 0. :is(), :not() and :has() take the specificity of their most specific argument — so :is(.a, #b) scores the same as #b — while :where() always scores 0, no matter what is inside it. When two selectors have equal specificity, the one that comes later in the stylesheet wins. !important is not part of this score: a declaration with !important beats any normal declaration, no matter how specific its selector is, and among !important declarations specificity applies again.

What is the CSS Specificity Calculator?

The CSS Specificity Calculator is a free online tool that works out the specificity of any CSS selector — or several at once — and tells you which one wins. Enter one selector per line, and each one is shown in the four-part form (inline styles, IDs, classes, elements), with the selectors compared and the winner highlighted.

It is made for anyone who has ever stared at a stylesheet wondering why a rule is not applying: beginners learning how CSS decides which rule wins, developers debugging a stubborn style, and anyone cleaning up a stylesheet full of competing selectors. There is nothing to install and nothing to sign up for. Everything runs in your browser, and your selectors never leave your device.

The calculator handles modern selectors properly. :is(), :not() and :has() take the specificity of their most specific argument, and :where() always contributes nothing, no matter what is inside it. Invalid selectors are reported gracefully instead of breaking the tool.

How to use the CSS Specificity Calculator

  1. Enter one CSS selector per line in the box labelled “Enter one CSS selector per line”. The tool comes with three sample selectors already loaded.
  2. Try an example if you want to see a modern selector in action. The example buttons load :is(.a, #b), :where(.a, #b), a:not(.active) and div:has(> img) with one click.
  3. Click “Calculate”. Each selector is shown with its four-part specificity, the selectors are compared, and the winner is highlighted.
  4. Copy results copies the results as text. Clear empties the box, and Reset brings back the sample selectors.

Worked example

The three sample selectors are:

#header .nav a
ul li a:hover
div#main .content p

Click “Calculate” and the tool reports:

SelectorSpecificity
#header .nav a(0, 1, 1, 1)
ul li a:hover(0, 0, 1, 3)
div#main .content p(0, 1, 1, 2)

The winner is div#main .content p, with (0, 1, 1, 2). Here is why: all three are compared from left to right. The first number is inline styles, which is 0 for all of them because these are selectors, not inline styles. The second number is IDs: #header .nav a and div#main .content p both have one ID, while ul li a:hover has none, so it is out. The third number is classes: both remaining selectors have one class each (.nav and .content). The fourth number is elements: div#main .content p has two (the div and the p), while #header .nav a has one (the a). Two beats one, so div#main .content p wins.

How the four numbers work

Every selector gets four numbers: (inline styles, IDs, classes, elements).

  • Count 1 for each ID selector (#header).
  • Count 1 for each class selector (.nav), attribute selector ([type="text"]) and pseudo-class (:hover).
  • Count 1 for each type selector (div, a) and pseudo-element (::before).
  • The universal selector (*) counts 0.
  • Inline styles are always 0 here, because these are selectors, not inline styles.

When two selectors have equal specificity, the one that comes later in the stylesheet wins.

Why specificity is worth understanding

It explains why your rule is not applying

This is the use case that brings most people here. You write a rule, it does nothing, and you cannot see why. Nine times out of ten the answer is specificity: another rule with a higher score is winning. Paste both selectors into the calculator and the winner is obvious — and so is the fix, which is usually to make your selector more specific or to remove the competing rule.

It keeps stylesheets maintainable

A stylesheet full of IDs and long selector chains is hard to change, because every new rule has to beat the old ones, and the only way to beat them is to be even more specific. That is how stylesheets end up with selectors five levels deep and !important scattered everywhere. Understanding specificity helps you write flatter, simpler selectors that are easy to override later.

Modern selectors have surprising scores

:is() and :where() look similar but score very differently. :is(.a, #b) takes the specificity of its most specific argument, so it scores the same as #b — (0, 1, 0, 0). :where(.a, #b) always scores 0, no matter what is inside it. That makes :where() a genuinely useful tool: it lets you group selectors without adding any weight to them. :not() and :has() behave like :is(), taking the most specific argument’s score.

Tips for working with specificity

  • Keep selectors as short as you can. A single class usually beats a long chain in maintainability, even if the chain wins the specificity contest. Short selectors are easier to override later.
  • Use :where() when you want grouping without weight. If you need to write a selector that matches several things but should not be hard to override, wrap it in :where(). It contributes nothing to the score, which is exactly what you want for a low-priority rule.
  • Remember that equal specificity means the later rule wins. If two selectors score the same, the one that appears later in the stylesheet applies. That is why the order of your rules matters, and why moving a rule can change the result.
  • Do not use !important to win a specificity fight. !important is not part of the specificity score at all: a declaration with !important beats any normal declaration, no matter how specific its selector is, and among !important declarations specificity applies again. It is a sledgehammer. Fix the specificity instead, and save !important for the rare cases where nothing else works.
  • IDs are expensive. One ID outweighs any number of classes. That is powerful, but it makes the rule almost impossible to override without another ID or !important. Prefer classes for styling, and reserve IDs for JavaScript hooks and anchor targets.

Frequently asked questions

What do the four numbers mean?

They are (inline styles, IDs, classes, elements). Each number counts how many of that kind of selector the full selector contains. Inline styles are always 0 in this tool, because you are entering selectors, not inline styles. The numbers are compared from left to right: more IDs beats any number of classes, and more classes beats any number of elements.

Does !important count towards specificity?

No. !important is not part of the specificity score. A declaration with !important beats any normal declaration, no matter how specific its selector is. Among !important declarations, specificity applies again to decide between them. That is why !important feels like it breaks the rules — it sits outside them.

What is the difference between :is() and :where()?

They match the same elements, but they score differently. :is(.a, #b) takes the specificity of its most specific argument, so it scores the same as #b. :where(.a, #b) always scores 0, no matter what is inside it. Use :is() when you want the grouped selector to carry weight, and :where() when you want grouping without weight.

Why did my rule lose even though it comes later in the stylesheet?

Because order only decides ties. If the other selector has a higher specificity score, it wins regardless of order. The later rule only wins when the scores are equal. Check both selectors in the calculator — the winner is usually obvious once you see the numbers.

What happens if I enter an invalid selector?

The tool reports it gracefully instead of breaking. You will see that the selector could not be calculated, and the other selectors still work. Fix the typo and calculate again.

Conclusion

Specificity is the part of CSS that feels like magic until you see the numbers — and then it is just arithmetic. This CSS Specificity Calculator shows you those numbers for any selector, compares them, and highlights the winner, including the modern selectors like :is(), :not(), :has() and :where() that most cheat sheets get wrong. Paste your competing selectors, find out which one wins, and fix the stylesheet instead of reaching for !important. And when you want to tidy the stylesheet itself, the CSS Formatter will lay it out cleanly — and the HTML Validator will check that the markup your selectors target is sound in the first place.

Related Tools