VITSOLS
2025-10-02
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
- :is() → Reduces code repetition.
- :has() → Allows parent-based styling (something CSS lacked before).
- :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() |
|
Takes the specificity of the most specific selector inside it.
Example: :is(.btn, #id) → Specificity = ID (100)
|
✅ Style both <a> and <button> inside navigation.
|
:has() |
|
Matches the parent if it contains a given child. Specificity = that of the selector inside. |
✅ Highlight forms that contain validation errors.
|
:where() |
|
Always has 0 specificity. Great for global resets and safe defaults. |
✅ Reset spacing for common elements without overriding stronger rules.
|