Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages

Free tool · Developer tools

CSS specificity calculator

Paste selectors, one per line, and the page scores each one as three numbers and ranks them. In the example list, #nav .item:hover scores 1,2,0 and beats the other four.

  • Free
  • No sign-up
  • Runs in your browser

By Maaz Zindani · Updated

A comma outside brackets starts a new selector. Paste only selectors, without the braces or the rules.
Highest specificity (A,B,C)
1,2,0Highest of 5: #nav .item:hover
  • 5 selectors counted
IDs (A)
1
Classes, attributes, pseudo-classes (B)
2
Types, pseudo-elements (C)
0
Every selector
#SelectorA,B,CRank
1#nav .item:hover1,2,01
2ul li a0,0,34
3a:not(#main, .x)1,0,12
4.card > p::first-line0,1,23
5:where(.a, #b) p0,0,15

How to use the CSS Specificity Calculator

  1. Type or paste your selectors in Selectors, one per line. Leave out the braces and the declarations.
  2. Read the highest score in the large figure, with the count of IDs, classes and types for the winner underneath.
  3. Check the table for every selector with its A,B,C score and rank. Rank 1 is the most specific.
  4. Press Copy all to copy each selector with its score. A line that is not a valid selector is listed in a note and left out of the ranking.

How is CSS specificity calculated?

A selector gets three numbers, written A,B,C. A counts the ID selectors. B counts the class selectors, the attribute selectors and the pseudo-classes. C counts the type selectors and the pseudo-elements. The universal selector adds nothing, and combinators such as a space, > or + add nothing either. The three numbers are compared in order, so any ID beats any number of classes, and any class beats any number of types.

Take #nav .item:hover from the example. It holds one ID, one class and one pseudo-class, so the score is 1,2,0. The selector ul li a has three types and no classes, so it scores 0,0,3. The pseudo-element in .card > p::first-line counts as a type, which makes its score 0,1,2: one class, plus the type p, plus the pseudo-element. The scores are not a single number in base 10, because 11 classes still lose to 1 ID.

Selectors and their A,B,C scores
SelectorA,B,C
li0,0,1
ul li0,0,2
li.red.level0,2,1
h1 + *[rel=up]0,1,1
#x34y1,0,0
#s12:not(p)1,0,1
:is(em, #foo)1,0,0
:where(.a, #b) p0,0,1
li:nth-child(2n+1 of .x, #y)1,1,1
a:hover0,1,1
p::first-line0,0,2

How do :is(), :not(), :has() and :where() count?

Four functional pseudo-classes work differently from the plain ones. :is(), :not() and :has() take the score of the most specific selector in their list and add it, so the pseudo-class itself adds nothing. In a:not(#main, .x) the list holds an ID and a class, the ID wins, and the selector scores 1,0,1 with the type a. :where() is always zero, which is why it is used for low-weight default styles: :where(.a, #b) p scores only 0,0,1.

The :nth-child() and :nth-last-child() pseudo-classes count as one pseudo-class, plus the most specific selector after the word of, if there is one. For li:nth-child(2n+1 of .x, #y) that is the type li, one pseudo-class and the ID #y, giving 1,1,1. Without the of part, li:nth-child(2n+1) scores 0,1,1.

Which rule wins when two selectors match the same element?

For two normal declarations of the same property, the one with the highest specificity wins. When the scores are equal, the last declaration in document order wins, which is why the page marks a tie in the result. Declarations in a style attribute take precedence over declarations from selectors of the same importance, and an important declaration takes precedence over a normal one. The score on this page covers only the selector part of that sort.

A tie is common in practice. The selectors .a and .b both score 0,1,0, so the one written later in the stylesheet applies. If you need a rule to win without adding an ID, raise its score by one class or lower the other rule with :where().

What does the calculator accept?

Enter one selector per line, or several separated by commas. A comma inside brackets, such as the one in an :is() list or an attribute value, stays in the same selector. The calculator reads IDs, classes, attributes in square brackets with quoted values, type selectors, namespaces, escapes and comments. It reports a message for text that is not a selector, such as a missing bracket, a selector that ends with a combinator, or the nesting selector &, whose score depends on the parent rule.

Related pages on this site cover other parts of a stylesheet. The CSS minifier shrinks the file once the rules are settled, and the CSS grid generator builds the layout rules that these selectors apply to.

Use it with

Questions

How do I calculate CSS specificity?

Count IDs (A), then classes, attributes and pseudo-classes (B), then types and pseudo-elements (C). #nav .item:hover has one ID, one class and one pseudo-class: 1,2,0.

Does the universal selector add specificity?

No. The universal selector and the combinators add nothing, so * scores 0,0,0 and ul > li scores 0,0,2.

What is the specificity of :where()?

Zero. :where(#a) scores 0,0,0, while :is(#a) takes the score of its most specific argument, 1,0,0.

What happens when two selectors have the same specificity?

The last declaration in document order wins, so the rule written later in the stylesheet applies.

Do 11 classes beat 1 ID?

No. The numbers are compared in order, A first. One ID scores 1,0,0 and eleven classes score 0,11,0, and the ID wins.

Does the page keep my selectors?

No. Your selectors are scored locally in the page and never leave it.

The tool's results are computed in your browser by the code on this page, using the sources listed above; the explanations are checked against the tool's own output.