CSS Specificity Calculator Online. Selector Weight Visualizer

Calculate the specificity and exact weight of any CSS selector. Visual tool to understand which CSS rule wins the cascade and avoid using !important.

VS
This selector wins!
0 IDs
0 Classes / Pseudos
0 Elements
This selector wins!
0 IDs
0 Classes / Pseudos
0 Elements
Both selectors have the same weight. If they compete for the same element, the one written last in the CSS wins.
Utilities Studio

Want this utility on your website?

Customize colors and dark mode for WordPress, Notion or your own site.

Frequently Asked Questions

What is CSS specificity?

Specificity is the algorithm browsers use to decide which CSS rule applies to an element when multiple rules compete. It is represented as a three-column score (A, B, C) indicating IDs, classes/attributes/pseudo-classes, and elements/pseudo-elements respectively.

Can a class ever beat an ID in specificity?

Not directly. An ID (1,0,0) always beats any number of classes (0,N,0) because specificity has no carry-over between columns. A hundred classes (0,100,0) will never beat a single ID (1,0,0).

What happens when two selectors have the same specificity?

When two selectors have exactly the same weight, the one declared last in the CSS file wins. This is known as natural cascade order. This calculator visually alerts you when a tie occurs.

Why is using !important considered bad practice?

The !important directive breaks the natural CSS cascade by forcing a declaration over any other rule. This creates conflicts that are hard to debug in large projects. Methodologies like BEM advocate for keeping specificity flat to avoid ever needing !important.

# What Is CSS Specificity?

CSS specificity is the algorithm by which browsers decide which property values to apply to a particular element. It is essentially a mathematical score that tells the browser "how specific" a rule is.If two rules have different specificity levels, the one with the higher weight will win, regardless of the order in which they were written. If both have the same weight, the last one declared in the source code wins.

# How to Calculate CSS Specificity

Specificity is calculated based on three categories forming a three-column weight, often expressed as (A, B, C):
  • Column A (IDs): Counts the number of unique identifiers. Example: #header counts as 1 in column A.
  • Column B (Classes, Attributes and Pseudo-classes): Counts all classes (.button), attributes ([type="text"]) and pseudo-classes (:hover).
  • Column C (Elements and Pseudo-elements): Counts all HTML elements (div, h1) and pseudo-elements (::before).

# The Golden Rule: No Numeric Carry-Over

A rule with specificity (0,50,0) will never be more specific than a rule (1,0,0). A single ID beats infinite classes. Columns never overflow into each other.

# The Problem With !important and BEM Architecture

The !important directive is an exception to specificity rules. When used, that declaration automatically overrides any other. It is considered bad practice because it destroys the natural cascade.To avoid specificity wars in large projects, methodologies like BEM advocate for using only single-depth class selectors, artificially keeping specificity flat at (0,1,0).

Bibliographic References