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:
.nav-link:focus,
.nav-link:hover,
.nav-link[aria-current="page"] {}Each selector repeats .nav-link. When selectors become longer, the duplication grows, as shown with :not:
.button:not(.disabled):focus,
.button:not(.disabled):hover {}In Sass, the ampersand ( &) can reduce repetition:
.nav-link {
&:focus,
&:hover,
&[aria-current="page"] {}
}However, the compiled CSS still contains duplicated selectors. All major browsers now support the :is pseudo‑class, which accepts a comma‑separated list of selectors, allowing the same result with far less code.
Rewriting the first example with :is:
.nav-link:is(:focus, :hover, [aria-current="page"]) {}The selectors are merged into a single rule that is functionally equivalent to the original.
Similarly, the second example can be refactored:
.button:not(.disabled):is(:focus, :hover) {}Using :is eliminates the need to repeat any class or pseudo‑class.
In Sass, :is works nicely with the ampersand:
.nav-link {
/* base styling */
&:is(:focus, :hover, [aria-current="page"]) {
/* active styling */
}
} :iscan also be used on its own to target direct descendants of multiple parent selectors:
:is(.parent1, .parent2, .parent3) > * {}Without :is, the same rule would require repetitive code:
.parent1 > *,
.parent2 > *,
.parent3 > * {}When using :is, two important aspects to consider are specificity and forgiving parsing.
Specificity
The specificity of :is is determined by the most specific selector in its list; all selectors share that specificity. In the first example, all selectors have equal specificity.
.nav-link:is(:focus, :hover, [aria-current="page"]) {}If an ID selector appears in the list, the overall specificity rises, causing lower‑specificity selectors to be overridden:
div:is(#id, .class) {
background: red;
}
div:is(.class, .another-class) {
background: blue;
}In this case, the div will 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:
.element:is(:focus, :unrecognized-selector) {}The unrecognized selector is ignored, but :focus still receives the styles.
.element :focus,
.element :unrecognized-selector {}All major browsers now support :is. The current support matrix is shown below:
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
