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 book lover

Growing Rails Applications in Practice

Check out our e-book. Learn to structure large Ruby on Rails codebases with the tools you already know and love.

  • Introduce design conventions for controllers and user-facing models
  • Create a system for growth
  • Build applications to last
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)