Example 1: :is() Pseudo-Class


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

✅ This will style all <h1>, <h2>, and <h3> headings at once.

Instead of writing:


h1, h2, h3 {
  color: darkblue;
}
  

You save time with :is().

Real-Time Use Case:


nav :is(a, button) {
  padding: 10px;
  cursor: pointer;
}
  

✅ This applies the same style to both <a> and <button> inside navigation.


Example 2: :has() Pseudo-Class

The :has() pseudo-class is known as the “parent selector” in CSS.


article:has(img) {
  border: 2px solid green;
}
  

✅ Any <article> that contains an <img> will have a green border.

Real-Time Use Case:


form:has(.error) {
  background-color: #ffe6e6;
}
  

✅ If the form contains an element with the class .error, it will show a red background.


Example 3: :where() Pseudo-Class

The :where() pseudo-class works like :is() but with zero specificity.


:where(h1, h2, h3) {
  margin: 0;
  padding: 0;
}
  

✅ This resets heading styles but won’t interfere with more specific styles applied later.

Real-Time Use Case:


:where(p, ul, ol) {
  margin: 0;
  padding: 0;
}
  

✅ This resets <p>, <ul>, and <ol> globally without conflicts.


Why These Pseudo-Classes Are Helpful

  1. :is() → Reduces code repetition.
  2. :has() → Allows parent-based styling (something CSS lacked before).
  3. :where() → Provides safe, low-specificity defaults.

They make CSS more powerful, flexible, and maintainable, especially in large-scale projects.


CSS Pseudo-Class Comparison: :is(), :has(), :where()

Selector Syntax Specificity Real-Time Example
:is()

:is(h1, h2, h3) {
  color: darkblue;
}
            
Takes the specificity of the most specific selector inside it. Example: :is(.btn, #id) → Specificity = ID (100)

nav :is(a, button) {
  padding: 10px;
  cursor: pointer;
}
            
✅ Style both <a> and <button> inside navigation.
:has()

article:has(img) {
  border: 2px solid green;
}
            
Matches the parent if it contains a given child. Specificity = that of the selector inside.

form:has(.error) {
  background-color: #ffe6e6;
}
            
✅ Highlight forms that contain validation errors.
:where()

:where(h1, h2, h3) {
  margin: 0;
  padding: 0;
}
            
Always has 0 specificity. Great for global resets and safe defaults.

:where(p, ul, ol) {
  margin: 0;
  padding: 0;
}
            
✅ Reset spacing for common elements without overriding stronger rules.

Conclusion

The introduction of :is(), :has(), and :where() in CSS brings modern solutions to common problems.

From grouping selectors to parent-based styling and safe resets, these pseudo-classes help developers write cleaner and smarter CSS.

🚀 Start using them in your projects to save time, reduce redundancy, and improve maintainability.