Sub topic 9
Calculating Specificity
This chapter assumes you can read a CSS rule, know the basic selector types (type, class, ID), and have seen combinators like > and +. Here you learn the arithmetic the browser uses when two rules with different selectors target the same element and set the same property.
What specificity measures
Specificity is a weight the browser gives each selector. When two declarations compete for the same property on the same element, the declaration whose selector has the higher specificity wins. This holds as long as other cascade factors, such as origin and !important, are equal. Those factors come in a later chapter.
Specificity belongs to a selector, not to a property or an element. The browser computes it from the text of the selector alone.
The three columns
Write specificity as a triple, (A, B, C):
| Column | Counts | Examples |
|---|---|---|
| A | ID selectors | `#main` |
| B | Class selectors, attribute selectors, pseudo-classes | `.note`, `[href]`, `:hover` |
| C | Type selectors, pseudo-elements | `p`, `li`, `::before` |
Some parts add nothing:
- The universal selector
*contributes (0,0,0). - Combinators (the space,
>,+,~) contribute nothing. They affect which elements match, not how heavy the selector is. - The value inside an attribute selector does not matter.
[type]and[type="text"]both add one to column B.
Counting step by step
To calculate a selector's specificity:
- Count the ID selectors. This is A.
- Count the classes, attribute selectors, and pseudo-classes. This is B.
- Count the type selectors and pseudo-elements. This is C.
- Ignore
*and combinators.
p { color: black; } /* (0,0,1) */
.note { color: green; } /* (0,1,0) */
p.note { color: blue; } /* (0,1,1) */
#main p.note { color: purple; } /* (1,1,1) */
ul li.active > a[href] { color: red; } /* (0,2,3) */Take the last rule. The type selectors are ul, li, and a, so C is 3. The class .active and the attribute [href] give B as 2. There are no IDs. The > adds nothing, so the result is (0,2,3).
If one element matches all five rules, the purple rule wins. Its triple (1,1,1) is the highest, because an ID outweighs everything in the other columns.
Comparing two triples
Compare the triples one column at a time, left to right:
- Compare column A. The larger number wins, and you stop.
- If A is equal, compare column B.
- If B is also equal, compare column C.
- If all three are equal, the specificity is a tie, and source order decides (see below).
Columns do not carry over
A common mistake is to read the triple as a single number, such as 0,1,0 as ten and 0,0,11 as eleven. Columns never overflow into each other. One class beats any number of type selectors:
.title { color: green; } /* (0,1,0) */
html body div main section article h1 { color: red; } /* (0,0,7) */An <h1 class="title"> inside that chain is green. The long selector has seven type selectors, but its B column is 0, and B is compared before C.
Likewise, one ID beats any number of classes. (Older write-ups sometimes say 256 classes equal one ID. Modern browsers do not work that way.)
Equal specificity: source order
When the triples are identical, the declaration that appears later in the source wins.
.btn { background: gray; }
.btn { background: teal; }Both selectors are (0,1,0), so the button is teal. "Later" covers stylesheets too. A rule in a stylesheet linked after another one comes later, even if the selectors sit in different files.
Inline styles and `!important`
A style attribute on the element beats every selector in a stylesheet, whatever its triple. You can think of it as a column to the left of A. You cannot write it as a selector, so you cannot match it from a stylesheet. The !important flag works at a different level of the cascade and is covered later. Do not try to model it as extra specificity.
Selectors that look odd
Pseudo-classes and pseudo-elements
a:hover is (0,1,1): one type, one pseudo-class. p::first-line is (0,0,2): one type, one pseudo-element. The one-colon and two-colon forms are counted by what they are, so :hover goes in B and ::before goes in C.
Functional pseudo-classes
Some pseudo-classes take a selector list as an argument. They are not counted as a plain pseudo-class. Instead, they take the specificity of their argument.
:is(.a, #b) p { color: blue; } /* (1,0,1) */
:where(.a, #b) p { color: blue; } /* (0,0,1) */
p:not(.hidden) { color: blue; } /* (0,1,1) */:is()and:not()use the most specific selector in their argument list. In the first rule,#bis the most specific argument. That is true even when the element matched.a. The selector:is()itself adds nothing, so the rule is (1,0,1).:where()always contributes zero, so the second rule is just (0,0,1).:not(.hidden)contributes the weight of.hidden, sop:not(.hidden)is (0,1,1). The:notis not counted on top of that.:has()follows the same rule as:is(): the most specific argument.
Selector lists
A comma-separated list is several selectors in one rule. Each is calculated on its own.
h1, .title, #page-title { margin: 0; }For an element that matches only h1, this rule has specificity (0,0,1). For an element that matches #page-title, it has (1,0,0). A matching element is judged by the selector that matched it.
A worked example
Take this markup and stylesheet:
<nav id="site-nav">
<ul class="menu">
<li class="item active"><a href="/home">Home</a></li>
</ul>
</nav>nav a { color: gray; } /* (0,0,2) */
.menu a { color: navy; } /* (0,1,1) */
.menu .item.active a { color: crimson; } /* (0,3,1) */
nav#site-nav a:not([href="#"]) { color: black; } /* (1,1,2) */All four selectors match the link. For the last one, nav and a are types (2), #site-nav is an ID (1), and :not([href="#"]) takes the weight of its attribute argument, which is 1 in column B. The result is (1,1,2), the highest triple, so the link is black. The crimson rule has three classes, but it has no ID.
If you removed #site-nav from the last selector, it would become (0,1,2). Crimson at (0,3,1) would then win, because B is compared before C.
Practical consequences
- Each ID you add makes a selector hard to override later. Only another ID, or a later rule at the same weight, can beat it.
- Long descendant chains raise specificity without much benefit, and they tie the CSS to the HTML structure.
- Combinators do not affect specificity, so
nav > aandnav ahave the same weight. - To beat a rule, match its triple and come later in the source, or exceed it with the smallest change you can find, such as one extra class.
Exercise
Calculate the specificity of #nav .menu li:hover a::after. Then say which wins on the same element and property: that selector, or body .menu .item .link:focus (that is, which triple is higher)?
Show answer
The first selector has one ID (#nav), two class-like parts (.menu and :hover), and three type-like parts (li, a, and ::after), so it is (1,2,3). The second has no ID, four class-like parts (.menu, .item, .link, :focus), and one type (body), so it is (0,4,1). Column A is compared first, so the first selector wins with (1,2,3).
Exercise
What is the specificity of p:not(#intro, .lead), and what does p:where(#intro, .lead) give instead?
Show answer
:not() takes the weight of its most specific argument, which is #intro, so p:not(#intro, .lead) is (1,0,1): one ID plus the type p. :where() always contributes zero, so p:where(#intro, .lead) is (0,0,1).