---
title: "Calculate CSS selector specificity in C#"
url: https://recursiveai.org/onehtml/cookbook/calculate-css-selector-specificity/
description: "Calculate CSS selector specificity in C# without loading HTML. Get the ID, class and type counts for :is(), :where(), :not() and other selectors."
version: "1.6.0"
---

# 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneHtml) · [GitHub](https://github.com/recursive-ai-oss/onehtml-csharp) · [Docs](https://recursiveai.org/onehtml/docs/) · [Download example (.zip)](https://recursiveai.org/onehtml/cookbook/calculate-css-selector-specificity/calculate-css-selector-specificity.zip)

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

```shell
dotnet add package RecursiveAI.OneHtml
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onehtml/cookbook/calculate-css-selector-specificity/calculate-css-selector-specificity.zip), 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.

```csharp
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.

```csharp
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)
```

## Related recipes

- [Select HTML elements with CSS](https://recursiveai.org/onehtml/cookbook/css-selectors/)
- [Inspect before and after styles](https://recursiveai.org/onehtml/cookbook/inspect-css-pseudo-element-styles/)

---

## 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](https://www.w3.org/TR/selectors-4/#specificity-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](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Cascade/Specificity) 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](https://recursiveai.org/onehtml/docs/selectors/#specificity) for the parser’s behavior.
