Recursive AI

Calculate CSS selector specificity in C#

onehtml is a free, open-source HTML library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Specificity is the weight of a CSS selector when matching rules compete.

Terminal
dotnet add package RecursiveAI.OneHtml

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneHtml.Selectors;
 
var list = SelectorList.Parse(
    ":is(.a,#b), :where(#b), :not(.a)");
for (int i = 0; i < list.Count; i++)
{
    var (ids, classes, types) =
        list.GetSpecificity(i);
    Console.WriteLine(
        $"({ids}, {classes}, {types})");
}

Unzip the example, then dotnet run.

Prints the ID, class and type counts for the three selectors, in input order.

Text
(1, 0, 0)
(0, 0, 0)
(0, 1, 0)

The full program

The download compares the same argument list in three different functions and adds an :nth-child() and a pseudo-element example. If you’re checking user-entered selector text, use SelectorList.TryParse to handle invalid input without a syntax exception.

C#
using RecursiveAI.OneHtml.Selectors;
 
string[] selectors =
[
    "a:is(.nav, #home)",
    "a:where(.nav, #home)",
    "a:not(.nav, #home)",
    "li:nth-child(2 of .nav, #home)",
    "a.nav::before"
];
 
foreach (var selector in selectors)
{
    var (ids, classes, types) = SelectorList.Parse(selector).GetSpecificity(0);
    Console.WriteLine($"{selector}: ({ids}, {classes}, {types})");
}

dotnet run prints:

Text
a:is(.nav, #home): (1, 0, 1)
a:where(.nav, #home): (0, 0, 1)
a:not(.nav, #home): (1, 0, 1)
li:nth-child(2 of .nav, #home): (1, 1, 1)
a.nav::before: (0, 1, 2)

Variations

Score a comma-separated group

Use GetSpecificity for each comma-separated selector.

C#
using RecursiveAI.OneHtml.Selectors;
 
var group = SelectorList.Parse("a, #home");
for (int i = 0; i < group.Count; i++)
{
    var (a, b, c) = group.GetSpecificity(i);
    Console.WriteLine($"({a}, {b}, {c})");
}
Text
(0, 0, 1)
(1, 0, 0)

The short answer

Pass selector text to SelectorList.Parse, then call GetSpecificity(i) for each top-level selector. The result is a triple (A, B, C), not one number. For example, :is(.a,#b) is (1, 0, 0), and :where(#b) is (0, 0, 0). You don’t need a document or a stylesheet to calculate either value.

How do I calculate specificity in CSS?

The Selectors Level 4 rules count IDs in the first column, classes, attributes and ordinary pseudo-classes in the second, and type selectors and pseudo-elements in the third. A universal selector or a combinator such as > adds nothing. In a.nav::before, .nav adds one in the middle column, while a and ::before each add one in the last column. The result is (0, 1, 2).

Count each selector in a top-level comma-separated list separately, as in the variation. a, #home gives a rule two ways to match, not a single selector with both weights added. By contrast, a comma inside :is() separates arguments within a single selector. SelectorList parses both forms, so splitting on commas yourself will give the wrong result for the functional form.

Which CSS selector has the highest specificity?

Compare the columns left to right. An ID outweighs any number of classes when the ID counts differ. An attribute selector like [id=home] still counts in the class column; it doesn’t become an ID selector just because it tests that attribute.

Specificity doesn’t decide whether a selector matches. And even among matching rules, it doesn’t decide which origin, importance or cascade layer wins. The CSS cascade guide explains when to compare specificity. To check a rule that lost on a real page, inspect the computed property and its source rather than only comparing two triples.

How do :is(), :where() and :not() change specificity?

In a:is(.nav, #home) and a:not(.nav, #home), the function contributes the highest specificity of its arguments: the ID beats the class. a:is(.nav, #home) keeps that ID weight even when it matches by .nav. The a outside the function contributes one type, so both selectors score (1, 0, 1).

With a:where(.nav, #home), the function contributes zero regardless of what’s inside it. Only the a counts. Use :where() when you want to restrict which elements a default style targets without making it harder to override. :nth-child(2 of .nav, #home) is different again: it adds its own pseudo-class weight as well as the strongest selector after of. See the onehtml selector rules for the parser’s behavior.

All onehtml recipes