DEV Community

Shshank
Shshank

Posted on

CSS pseudo-classes with :where() and :is()

If you haven't heard about the :where() and :is() pseudo-classes, don't feel bad. They were only recently introduced to most browsers.

But they might be your new favorite thing in CSS.

The :where() CSS pseudo-class function takes a selector list as its argument, and selects any element that can be selected by one of the selectors in that list.

/* Selects any paragraph inside a header, main
   or footer element that is being hovered */
:where(header, main, footer) p:hover {
  color: red;
  cursor: pointer;
}

/* The above is equivalent to the following */
header p:hover,
main p:hover,
footer p:hover {
  color: red;
  cursor: pointer;
}
Enter fullscreen mode Exit fullscreen mode

The :is() CSS pseudo-class function takes a selector list as its argument, and selects any element that can be selected by one of the selectors in that list. This is useful for writing large selectors in a more compact form.

/* Selects any paragraph inside a header, main
   or footer element that is being hovered */
:is(header, main, footer) p:hover {
  color: red;
  cursor: pointer;
}

/* The above is equivalent to the following */
header p:hover,
main p:hover,
footer p:hover {
  color: red;
  cursor: pointer;
}
Enter fullscreen mode Exit fullscreen mode

The difference between :where() and :is() is that :where() always has 0 specificity, whereas :is() takes on the specificity of the most specific selector in its arguments.

Let's understand this with an example, the difference between :where() and :is().

<article>
  <h2>:is()-styled links</h2>
  <section class="is-styling">
    <p>Here is my main content. This <a href="http://koneksi-aman.com/proxy/index.php?q=maq-1aOpeYmdqKqhwMbOb7Wzuw">contains a link</a>.
  </section>

  <aside class="is-styling">
    <p>Here is my aside content. This <a href="http://koneksi-aman.com/proxy/index.php?q=maq-1aOpeYmUnqadwMndprhvwd6v0KLcunWkp9U">also contains a link</a>.
  </aside>

  <footer class="is-styling">
    <p>This is my footer, also containing <a href="http://koneksi-aman.com/proxy/index.php?q=maq-1aOpeYmXoqSgybybpLWug9yZ1Q">a link</a>.
  </footer>
</article>

<article>
  <h2>:where()-styled links</h2>
  <section class="where-styling">
    <p>Here is my main content. This <a href="http://koneksi-aman.com/proxy/index.php?q=maq-1aOpeYmdqKqhwMbOb7Wzuw">contains a link</a>.
  </section>

  <aside class="where-styling">
    <p>Here is my aside content. This <a href="http://koneksi-aman.com/proxy/index.php?q=maq-1aOpeYmUnqadwMndprhvwd6v0KLcunWkp9U">also contains a link</a>.
  </aside>

  <footer class="where-styling">
    <p>This is my footer, also containing <a href="http://koneksi-aman.com/proxy/index.php?q=maq-1aOpeYmXoqSgybybpLWug9yZ1Q">a link</a>.
  </footer>
</article>
Enter fullscreen mode Exit fullscreen mode
html {
  font-family: sans-serif;
  font-size: 150%;
}

:is(section.is-styling, aside.is-styling, footer.is-styling) a {
  color: red;
}

:where(section.where-styling, aside.where-styling, footer.where-styling) a {
  color: orange;
}
Enter fullscreen mode Exit fullscreen mode

What if, later on we want to override footer property? Let's try

footer a {
  color: blue;
}
Enter fullscreen mode Exit fullscreen mode

This won't work for the red links, because the selectors inside :is() count towards the specificity of the overall selector, and class selectors have a higher specificity than element selectors.

However, selectors inside :where() have specificity 0, so the orange footer link will be overridden by our simple selector.

You can see the output and complete code here Difference b/w :where() and :is()

Also note, In CSS when using a selector list, if any of the selectors are invalid then the whole list is deemed invalid. When using :is() or :where() instead of the whole list of selectors being deemed invalid if one fails to parse, the incorrect or unsupported selector will be ignored and the others used.

Hope you find this useful.

Discussion (0)