# specificity

> Calculate the specificity of a CSS selector

Latest version **1.0.0** (published 2023-06-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install specificity
pnpm add specificity
yarn add specificity
bun add specificity
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2023-06-23 |
| First published | 2012-08-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 33.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 645 |
| Author | Keegan Street |
| Maintainers | keeganstreet |
| Keywords | CSS, specificity |

## Links

- npm: https://www.npmjs.com/package/specificity
- Repository: https://github.com/keeganstreet/specificity
- Issues: https://github.com/keeganstreet/specificity/issues
- npm.io page: https://npm.io/package/specificity

## Dependencies (1)

- [css-tree](https://npm.io/package/css-tree.md) ^2.3.1

## Alternatives

- [style-dictionary](https://npm.io/package/style-dictionary.md) — 2.0M weekly downloads
- [postcss-merge-idents](https://npm.io/package/postcss-merge-idents.md) — 1.7M weekly downloads
- [@fontsource/noto-sans](https://npm.io/package/@fontsource/noto-sans.md) — 93.0K weekly downloads
- [uglifycss](https://npm.io/package/uglifycss.md) — 71.6K weekly downloads
- [mat4-interpolate](https://npm.io/package/mat4-interpolate.md) — 23.3K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2023-06-23
- 1.0.0-beta (next) — 2023-06-23
- 0.4.1 — 2018-08-28
- 0.4.0 — 2018-07-17
- 0.3.2 — 2017-09-26
- 0.3.1 — 2017-06-30
- 0.3.0 — 2016-10-05
- 0.2.1 — 2016-06-20
- 0.2.0 — 2016-06-19
- 0.1.6 — 2016-06-19
- 0.1.5 — 2016-01-06
- 0.1.4 — 2014-04-14
- 0.1.3 — 2012-09-23
- 0.1.2 — 2012-09-02
- 0.1.1 — 2012-08-20
- … 1 more at https://npm.io/package/specificity/versions

## README

# Specificity Calculator

A JavaScript module for calculating and comparing the [specificity of CSS selectors](https://www.w3.org/TR/selectors-4/#specificity). The module is used on the [Specificity Calculator](https://specificity.keegan.st/) website.

Note that version 1 is a complete re-write with support for CSS Selectors Level 4, and has a different API than earlier versions.

## calculate(selector)

### Parameters

- `selector`: `string` - should be a valid CSS selector

### Returns

A `Specificity` object with a type of `Record<"A" | "B" | "C", number>`.

### Examples

```js
calculate("#id");
{
  A: 1,
  B: 0,
  C: 0
}

calculate(".classname");
{
  A: 0,
  B: 1,
  C: 0
}

calculate("element");
{
  A: 0,
  B: 0,
  C: 1
}

calculate('ul#nav li.active a');
{
  A: 1,
  B: 1,
  C: 3
}
```

## compare(a, b)

### Parameters

- `a`: `Specificity` object with a type of `Record<"A" | "B" | "C", number>`
- `b`: `Specificity` object with a type of `Record<"A" | "B" | "C", number>`

### Returns

Returns a positive number if `a` has a higher specificity than `b`
Returns a negative number if `a` has a lower specificity than `b`
Returns `0` if `a` has the same specificity than `b`

### Examples

```js
[
  "element",
  ".classname",
  "#id",
]
  .map(calculate)
  .sort(compare);

[ { A: 0, B: 0, C: 1 }, { A: 0, B: 1, C: 0 }, { A: 1, B: 0, C: 0 } ]
```

## compareDesc(a, b)

Same as `compare` but returns the opposite value, for use in sorting specificity objects with the highest specificity first.

---
_Source: https://npm.io/package/specificity · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
