How the :is Pseudo‑Class Simplifies CSS and Cuts Redundant Code
This article explains how the modern CSS :is pseudo‑class can replace repetitive selector lists, reduce duplicated code, improve maintainability, and handle selector priority and forgiving parsing, with examples in plain CSS and Sass, plus browser support details.
In early web development, layouts relied on tables and hacks, but modern CSS now enables developers to write cross‑browser code more easily, create responsive designs, and produce smaller style sheets by eliminating redundancy.
Using :is to Reduce Repetitive Code
CSS often uses selector lists to apply the same styles to multiple selectors. For example:
<code>.nav-link:focus,
.nav-link:hover,
.nav-link[aria-current="page"] {}
</code>Each selector repeats
.nav-link. When selectors become longer, the duplication grows, as shown with
:not:
<code>.button:not(.disabled):focus,
.button:not(.disabled):hover {}
</code>In Sass, the ampersand (
&) can reduce repetition:
<code>.nav-link {
&:focus,
&:hover,
&[aria-current="page"] {}
}
</code>However, the compiled CSS still contains duplicated selectors. All major browsers now support the
:ispseudo‑class, which accepts a comma‑separated list of selectors, allowing the same result with far less code.
Rewriting the first example with
:is:
<code>.nav-link:is(:focus, :hover, [aria-current="page"]) {}
</code>The selectors are merged into a single rule that is functionally equivalent to the original.
Similarly, the second example can be refactored:
<code>.button:not(.disabled):is(:focus, :hover) {}
</code>Using
:iseliminates the need to repeat any class or pseudo‑class.
In Sass,
:isworks nicely with the ampersand:
<code>.nav-link {
/* base styling */
&:is(:focus, :hover, [aria-current="page"]) {
/* active styling */
}
}
</code> :iscan also be used on its own to target direct descendants of multiple parent selectors:
<code>:is(.parent1, .parent2, .parent3) > * {}
</code>Without
:is, the same rule would require repetitive code:
<code>.parent1 > *,
.parent2 > *,
.parent3 > * {}
</code>When using
:is, two important aspects to consider are specificity and forgiving parsing.
Specificity
The specificity of
:isis determined by the most specific selector in its list; all selectors share that specificity. In the first example, all selectors have equal specificity.
<code>.nav-link:is(:focus, :hover, [aria-current="page"]) {}
</code>If an ID selector appears in the list, the overall specificity rises, causing lower‑specificity selectors to be overridden:
<code>div:is(#id, .class) {
background: red;
}
div:is(.class, .another-class) {
background: blue;
}
</code>In this case, the
divwill be rendered red because the ID selector raises the rule’s priority.
Forgiving parsing
:isuses forgiving selector parsing, so if one item in the list is invalid, the rest still apply. For example:
<code>.element:is(:focus, :unrecognized-selector) {}
</code>The unrecognized selector is ignored, but
:focusstill receives the styles.
<code>.element :focus,
.element :unrecognized-selector {}
</code>All major browsers now support
:is. The current support matrix is shown below:
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.