:where()

Term side of the Card.
Topic: Pseudo Classes
Unit title: :where()
Categories: Pseudo-class
Explanation side of the Card

Explanation: The pseudo-class that matches elements based on a selector list without adding specificity. It's useful for applying styles to multiple elements while maintaining low specificity, preventing conflicts with other styles.

Browser compatibility:
  • The chrome browser supports this feature starting from version 88.
  • The edge browser supports this feature starting from version 88.
  • The firefox browser supports this feature starting from version 78.
  • The safari browser supports this feature starting from version 14.
Availability: 

Example

Wrongcss
ul[class],
ol[class] {
  list-style: none;
  padding: 0;
}
Rightcss
:where(ul, ol)[class] {
  list-style: none;
  padding: 0;
}

Only the [class] part counts now, so a single class can override this reset without extra specificity.

Common mistakes

  • Wrapping component styles in :where() and then losing to a plain element rule.
  • Thinking :where() changes which elements match, while it only changes specificity.

Key points

  • Resets and library defaults are wrapped in :where() so any user selector overrides them.
  • Code does not use :where() where styles must win over others, since it adds no specificity.

Interview questions

What is the specificity of :where()?

Always zero. Nothing inside :where() counts, though selectors outside it still do.

Should I use :where() or @layer for low-priority styles?

Both work. :where() lowers one selector's specificity, while @layer lowers a whole group of rules at once.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy