How :where Can Simplify Global CSS Resets and Reduce Specificity
This article explains how the modern CSS pseudo‑class :where works like :is but with zero specificity, making it ideal for global style resets, reducing selector weight, and simplifying responsive layouts while keeping code concise and maintainable across browsers.
In early web development, layout and positioning relied on tables and hacky techniques. Today, CSS has evolved, allowing developers to write cross‑browser code easily, create responsive layouts, and reduce stylesheet size.
Using :where to Set Global Default Styles
Like
:is, the
:wherepseudo‑class accepts a selector list as its argument.
<code>.nav-link:where(:focus, :hover, [aria-current="page"]) {}</code>The difference is that selectors inside
:iskeep the highest specificity among them, while selectors inside
:whereare assigned the lowest possible specificity (0). Specificity values are:
Inline style: 1000
ID selector: 100
Class, attribute selectors, etc.: 10
Element, pseudo‑element selectors: 1
Universal selector, adjacent selectors, etc.: 0
Thus,
:israises each selector’s specificity, whereas
:wherelowers it. The following two snippets have the same effective specificity:
<code>// 10 + 0
.nav-link:where(:focus, :hover, [aria-current="page"]) {}</code> <code>// 10
.nav-link {}</code>No matter how complex the selector list,
:wherealways has a specificity of 0, as shown here:
<code>where(#id:not(.very.high.specificity).more.classes) {}</code>Because of its low specificity, styles declared with
:whereare easy to override, making it perfect for global style resets. For example:
<code>:where(ul, ol) {
list-style: none;
}
:where(img) {
max-width: 100%;
height: auto;
}
/* etc */</code>These low‑specificity selectors ensure that the defined styles are unlikely to clash with other rules, so developers don’t need to increase specificity when overriding them.
In practice, CSS resets are placed at the top of a stylesheet, and techniques like BEM avoid using element selectors for styling. Using
:wherehelps guarantee that custom styles won’t encounter specificity conflicts.
Adam Argyle notes that the low specificity of
:whereis valuable in public libraries, allowing users to easily override library styles with their own custom rules.
KooFE Frontend Team
Follow the latest frontend updates
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.