Simplify CSS Selectors with :is() and :where() for Cleaner Code

This article explains how the CSS :is() pseudo‑class can replace long selector lists, compares it with :where(), shows compatibility across browsers, and provides multiple code examples demonstrating concise, readable selector patterns for styling elements like headings and buttons.

JavaScript
JavaScript
JavaScript
Simplify CSS Selectors with :is() and :where() for Cleaner Code

When writing CSS, developers often use long selector lists to apply the same rules to multiple elements, such as styling <b> tags inside headings. The traditional approach looks like this:

h1 > b, h2 > b, h3 > b, h4 > b, h5 > b, h6 > b {
  color: hotpink;
}

Using the :is() pseudo‑class, the code can be reduced and its readability improved:

:is(h1, h2, h3, h4, h5, h6) > b {
  color: hotpink;
}

Browser Compatibility

The :is() function originated from :match(). Some browsers implement a private :any() that provides part of :is() 's functionality. Before using :is(), check its compatibility:

CSS :is() compatibility chart
CSS :is() compatibility chart

:is() and :where()

The :is() pseudo‑class accepts a selector list as its argument and matches any element that any of those selectors would match. The :where() pseudo‑class works the same way but has a different selector specificity. Below are several examples illustrating their usage:

/* at the beginning */
:where(h1, h2, h3, h4, h5, h6) > b {
  color: hotpink;
}

/* in the middle */
article :is(header, footer) > p {
  color: gray;
}

/* at the end */
.dark-theme :where(button, a) {
  color: rebeccapurple;
}

/* multiple */
:is(.dark-theme, .dim-theme) :where(button, a) {
  color: rebeccapurple;
}

/* stacked */
:is(h1, h2):where(.hero, .subtitle) {
  text-transform: uppercase;
}

/* nested */
.hero:is(h1, h2, :is(.header, .boldest)) {
  font-weight: 900;
}

Finally, the following diagram helps visualize how :is() works:

Diagram of :is() selector behavior
Diagram of :is() selector behavior

References:

https://css-tricks.com/css-is-and-where-are-coming-to-browsers/

https://web.dev/css-is-and-where/

https://developer.mozilla.org/en-US/docs/Web/CSS/:is

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

frontend developmentCSSisWHERE
JavaScript
Written by

JavaScript

Provides JavaScript enthusiasts with tutorials and experience sharing on web front‑end technologies, including JavaScript, Node.js, Deno, Vue.js, React, Angular, HTML5, CSS3, and more.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.