日本語

How to Adjust Specificity with CSS :where() and :is()

​​CSS​Playwright,specificity

If you want the base styles from a reset CSS or a UI library to be overridable without !important, wrap the selectors in :where(). The specificity of :where() is 0 no matter what you put inside it. The similar :is() takes the specificity of the strongest selector in its list. Both change which rule wins, so knowing the difference also helps when you investigate why an override does not take effect.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and headless Chromium 153. I opened a test page and read the colors with getComputedStyle. For specificity, I recorded the values returned by CSS.getMatchedStylesForNode in the Chrome DevTools Protocol (the mechanism DevTools uses to talk to the browser). I did not test Firefox or Safari.

Specificity is compared as three numbers: IDs, classes, and elements

Specificity counts the IDs, classes (including attribute selectors and pseudo-classes), and elements (including pseudo-elements) in a selector, and writes the result as three numbers such as 1-0-0. The numbers are compared from left to right, and the rule with the larger value wins. If they are equal, the rule written later wins. For the full counting rules, see MDN's specificity guide.

In this article, I write the values returned by the DevTools Protocol in the form a-b-c, where a is IDs, b is classes, and c is elements.

Wrapping in :where() lets a single class in your own code override it

Suppose the base styles of a library are written like this. Each one makes a button gray.

css
/* Library side (loaded first) */
.btn-a.primary { background: #9aa5ab; }
:where(.btn-b.primary) { background: #9aa5ab; }
.btn-c:where(.primary) { background: #9aa5ab; }

On your side, in CSS loaded later, you try to make the button light blue with a single class, save.

css
/* Your side (loaded later) */
.save { background: #00c8e8; }
html
<button class="btn-a primary save">保存する</button>
<button class="btn-b primary save">保存する</button>
<button class="btn-c primary save">保存する</button>

When I opened this in the test environment, the specificities and resulting colors were as follows.

Library selector Specificity Button color
.btn-a.primary 0-2-0 Stays gray
:where(.btn-b.primary) 0-0-0 Light blue
.btn-c:where(.primary) 0-1-0 Light blue

The specificity of .save on your side is 0-1-0. .btn-a.primary has two classes, so its specificity is 0-2-0. That is stronger than the later .save, so the button stayed gray. When I changed the override on your side to two classes, as in .save2.primary, and wrote it later, the two rules were both 0-2-0 and the later one won, so the button turned light blue.

Three buttons. The button with .btn-a.primary (0-2-0) is gray. The buttons with :where(.btn-b.primary) (0-0-0) and .btn-c:where(.primary) (0-1-0) are light blue. The labels in the image are in Japanese.
After the gray rules from the library, the .save rule on the user side sets light blue

The second version, with the whole selector wrapped in :where(), had a specificity of 0-0-0, so .save won. Because 0-0-0 is weaker than any other specificity, it can be overridden regardless of load order. On the test page, a button { background: #00c8e8; } rule (0-0-1) that was loaded before the library beat the gray from :where(.btn-d.primary) loaded later, and the button turned light blue.

As in the third version, you can also wrap only part of a selector in :where(). In .btn-c:where(.primary), only the .btn-c outside the :where() is counted, so the specificity is 0-1-0. That matches .save, so the later .save wins. If you want to keep the strength of a certain class, move that part outside :where().

:is() takes the specificity of its strongest selector

:is() is used to group the same declaration across several selectors, as in :is(header, footer) a. However, its specificity is not the one of the selector that actually matched. It is the value of the strongest selector in its list (see MDN's :is() page).

css
:is(#main, .lead) { color: #d85878; }
.box .lead { color: #067a98; }
html
<div class="box"><p class="lead">:is(#main, .lead) に当たる段落</p></div>

This paragraph has no id="main", and it matches through .lead inside :is(). Even so, the DevTools Protocol reported a specificity of 1-0-0 for :is(#main, .lead). That is stronger than the later .box .lead (0-2-0), so the paragraph turned pink.

If you put one ID inside :is(), every element matched by the other selectors in the list is also styled with the strength of that ID. If you do not want to raise the specificity, write the group in :where() to make it 0.

An invalid selector inside does not discard the whole rule

According to MDN's :where() page, in an ordinary comma-separated selector list, one invalid selector makes the entire list invalid. Inside :is() and :where(), only the invalid selector is ignored and the rest are used. MDN calls these contents a "forgiving selector list".

css
.f1, .f2:unknown-pseudo { color: #d85878; }
:is(.f3, .f4:unknown-pseudo) { color: #d85878; }
:where(.f5, .f6:unknown-pseudo) { color: #d85878; }

:unknown-pseudo is a pseudo-class that does not exist. In the test environment, the paragraph for .f1 did not change color, while the paragraphs for .f3 and .f5 turned pink. When I inspected the CSSOM (the stylesheet as seen from JavaScript), the first rule was not kept in the stylesheet, and the second and third rules were kept with their selectors reduced to :is(.f3) and :where(.f5).

MDN gives an example where :where(:valid, :unsupported) still applies :valid in browsers that do not support :unsupported. When you combine a newer pseudo-class with other selectors, wrapping them in :is() or :where() prevents the whole declaration from being dropped in browsers that do not support it.

Browser support and what cannot go inside

According to the MDN browser compatibility data (@mdn/browser-compat-data 8.1.8), both :is() and :where() are available in Chrome 88, Edge 88, Firefox 78, and Safari 14 (and iOS 14). The behavior of ignoring invalid selectors inside them is available in Firefox from version 82.

According to the MDN pages for :is() and :where(), pseudo-elements (such as ::before) cannot be written inside either of them. Do not write some-element:is(::before, ::after). Instead, write the pseudo-elements outside :is(), as in some-element::before, some-element::after.

​Posted by 86