Read more

CSS: :where() pseudo selector

Julian
August 16, 2022Software engineer at makandra GmbH

tl;dr

The :where() pseudo selector - zero specificity - matches against a comma-separated list of selectors.

Example

Compound selectors like ...

.datepicker .prev, .datepicker .next, .datepicker .switch
  padding-bottom: 1rem

ul li, ol li
  list-style-type: none
Illustration money motivation

Opscomplete powered by makandra brand

Save money by migrating from AWS to our fully managed hosting in Germany.

  • Trusted by over 100 customers
  • Ready to use with Ruby, Node.js, PHP
  • Proactive management by operations experts
Read more Show archive.org snapshot

can be simplified by using the :where() pseudo selector ...

.datepicker :where(.prev, .next, .switch)
  padding-bottom: 1rem

:where(ul, ol) li
  list-style-type: none

Hint

The specificity of :where() is always zero!

If you need or want specificity consider to use the pseudo selector :is().

Forgiving

Each selector within the comma-separated list within the :where() is ignored when invalid.

:where(ul, ol, #5invalid) li
  /* will still select ul li and ol li */

Pseudo Selector vs. CSS Preprocessing

Simplifying selectors with the pseudo selector is similar like CSS preprocssing is handling nested rules, but be aware of the specificity.

/* SASS */
.nav-list, ol,
  li

/* After preprocessing */
.nav-list li, ol li
/*
  .nav-list li => specificity (0,0,1,1)
  ol li => specificity (0,0,0,2)
*/

:where(.nav-list, ol) li
/*
  .nav-list li => specificity (0,0,0,1)
  ol li => specificity (0,0,0,1)
*/

Browser Support

Image

Julian
August 16, 2022Software engineer at makandra GmbH
Posted by Julian to makandra dev (2022-08-16 17:43)