Apr 1, '25 03:00

Creating your own custom CSS selectors using :is() and :where()

CSS selectors are key tools for styling web pages. Over time, new possibilities emerge to simplify and optimize code. Among these innovations are the pseudo-classes :is() and :where(), which allow for the creation of custom CSS selectors more efficiently. A...

Read post
Share
🔥 More posts
This content has been automatically translated from Ukrainian.

CSS selectors are key tools for styling web pages. Over time, new possibilities emerge to simplify and optimize code. Among these innovations are the pseudo-classes :is() and :where(), which allow for the creation of custom CSS selectors more efficiently.

Advantages of Using :is() and :where()

Sometimes creating CSS selectors becomes a challenging task, especially when it comes to complex structures. :is() and :where() provide the opportunity to simplify this process. They allow you to combine multiple selectors into one, minimizing code duplication and improving readability.

Application of the :is() Pseudo-Class

:is() comes in handy when you need to apply the same styles to different elements. For example, if you want to set the same style for different headings, using :is() will significantly simplify the task:

:is(h1, h2, h3) {
  color: #333;
  font-family: 'Arial', sans-serif;
}

This code means that all headings h1, h2, and h3 will receive the same styles. This not only saves time but also makes the CSS code cleaner.

Features of the :where() Pseudo-Class

:where() works similarly to :is(), but has one significant difference — specificity. Styles defined through :where() have a specificity of zero, making it easier to override them with other styles.

:where(nav, footer) {
  margin: 0;
  padding: 0;
}

In this example, all <nav> and <footer> elements will receive zero margins. If later you want to add more specific styles for these elements, it can be easily done without the risk of conflicts.

When to Use :is() and :where()

The choice between :is() and :where() depends on how specific the styles need to be. If specificity is not critical, :where() will be the optimal choice. However, if it is important to maintain a certain level of specificity for styles, it is better to choose :is().

Real-World Examples of Application

Imagine you are working on a large project where you need to style many elements based on their type. Instead of writing a huge list of CSS code, you can use :is() and :where() to reduce size and increase the efficiency of styles.

:is(article, aside, section) p {
  line-height: 1.6;
  margin-bottom: 1em;
}

:where(.btn-primary, .btn-secondary) {
  padding: 10px 20px;
  border-radius: 5px;
}

Such selectors provide uniform styles for similar elements, greatly simplifying their maintenance.

Using :is() and :where() in CSS opens new horizons for optimizing styles and enhancing code efficiency. These pseudo-classes help create simpler and more flexible solutions, improving the development and maintenance experience of web pages.

🔥 More posts

All posts