DEV Community

Cover image for Say Goodbye to Complex Selectors with :where()
Kieren Quimsoquimos
Kieren Quimsoquimos

Posted on • Edited on

Say Goodbye to Complex Selectors with :where()

What is :where()?

Think of :where() as a powerful tool in your CSS toolbox that lets you group multiple selectors into a single, concise expression. It's particularly useful for applying styles to elements that match any of the specified selectors, without worrying about specificity conflicts.

Basic Syntax:

element:where(selector1, selector2, ...) {
  /* Styles to be applied */
}
Enter fullscreen mode Exit fullscreen mode

Example:
Let's say you want to style all <p> elements that have either the class highlight or the class important. You can use :where() like this:

p:where(.highlight, .important) {
  font-weight: bold;
  color: red;
}
Enter fullscreen mode Exit fullscreen mode

Why Use :where()?

  1. Improved Readability:
    • Makes your CSS more concise and easier to understand.
  2. Specificity Control:
    • Helps you override styles more easily, as :where() has zero specificity.
  3. Enhanced Maintainability:
    • By grouping selectors, you can make your CSS more modular and easier to maintain.

Real-world Example:
Imagine you have a website with a navigation bar. You want to style the active navigation link differently. You can use :where() to target both the :hover and :active states:

nav a:where(:hover, :active) {
  background-color: #f0f0f0;
  color: #333;
}
Enter fullscreen mode Exit fullscreen mode

Conclusion:
By understanding and effectively using :where(), you can write more efficient, maintainable, and elegant CSS code. It's a valuable tool for any web developer's arsenal.

Leveraging :where() for Complex Selectors

While :where() is great for simple selector groupings, it becomes even more powerful when used with complex selectors.

Example: Styling Specific Table Cells
Let's say you want to style specific table cells based on their content and position. You can use :where() to combine multiple selectors:

table td:where(
  :nth-child(2),
  :contains("Important")
) {
  background-color: yellow;
  font-weight: bold;
}
Enter fullscreen mode Exit fullscreen mode

This code will style the second child of each table cell, as well as any cell containing the word "Important".

Combining :where() with Other Pseudo-classes

You can also combine :where() with other pseudo-classes to create even more specific selectors:

a:where(:hover, :focus) {
  text-decoration: underline;
  color: blue;
}
Enter fullscreen mode Exit fullscreen mode

This code will style both the :hover and :focus states of anchor links.

Best Practices for Using :where()

  1. Use it judiciously: Don't overuse :where() as it can make your CSS more complex to read and maintain.
  2. Prioritize specificity: While :where() has zero specificity, it's still important to consider specificity when writing CSS.
  3. Test thoroughly: Always test your CSS in different browsers to ensure compatibility.

Conclusion:
The :where() pseudo-class is a valuable tool for modern CSS. By mastering its usage, you can write more efficient, maintainable, and elegant CSS code.

Top comments (0)