1.0.0 • Published 10 months ago

specificity v1.0.0

Weekly downloads
2,321,938
License
MIT
Repository
github
Last release
10 months ago

Specificity Calculator

A JavaScript module for calculating and comparing the specificity of CSS selectors. The module is used on the Specificity Calculator 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

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

[
  "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.

accessibility-layer@everything-registry/sub-chunk-2804@emotion/jest@leetcode/stylelint@leoko/svg2pdf.js@michaelallenwarner/tailwind-grid-inner@paperbits/emails@paperbits/amp@petergok/tempo-devtools@petergok/tempo-devtools-test@rai/react-components@realrisman/svg2pdf@mybricks/editors-pc-common@nodaomg/cssstats@fabwcie/ckeditor5-preview@opticss/simple-template@oriduk/ckeditor5-rebuild-musicanote@oriduk/ckeditor5-rebuild-oriduk@thundersolutions/access@jackdbd/eleventy-plugin-text-to-speech@silver0542/directus-extension-cma-components-editor@sbaldwin24/stylelint-config@hanwenbo/web-editor@gui-one/react-settings-formanalyze-cssmprdev@whinc/web-consoleblessed-cssbrowser-xpostcss-sort-style-rulespostcss-basscsspsyclone-studiockeditor5-build-classic-v-editckeditor5-build-classic-ticketpostcss-remove-mutationsposthtml-css-inlineposthtml-inline-cssrc-editor-list@yankovsky/subfontdfeuk-frontenddfeuk-frontend-manualresolve-cascaderepaintdelenitiveritatisreplace-importantlongest-selectors@webbitjs/webbitassetgraphapim-developer-portalbase-editorcomponent-stylesopticsscss-querycss-sortcssstatsreact-mirrorcss-specificity-mapcss-statisticsreact-round-divec0lint-cssec0lint-styleec0lint-style-config-postcssscss-codemodsget-specified-style.jstempo-devtoolsfusion-theme-configsvg2pdf.jssvg-icon-toolboxfont-tracersubfonteleventy-img-helpershadow-dom@alifd/fusion-theme-configspecificity-graphsort-specificitytheme-config-kit@dimanech/cssstat-coreunfonthtml-tuiuse-theme-editorvisual-htmlimmutable-csstyprodgrumpkininline-cssinline-react-style
1.0.0

10 months ago

1.0.0-beta

10 months ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.2

7 years ago

0.3.1

7 years ago

0.3.0

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.6

8 years ago

0.1.5

8 years ago

0.1.4

10 years ago

0.1.3

12 years ago

0.1.2

12 years ago

0.1.1

12 years ago

0.1.0

12 years ago