28. Advanced Selectors (:is(), :where(), :has() Relational Parent Selector)
🔴 Advanced
1. Definition
Modern CSS introduces powerful relational pseudo-classes: :is() (groups selectors taking the highest specificity score), :where() (groups selectors taking zero specificity score), and :has() (the parent selector targeting parent elements based on child conditions).
2. Why It Is Used
:has() solves the decades-old CSS limitation of being unable to style a parent element based on its children, eliminating the need for JavaScript DOM class toggles.
3. How It Works
.card:has(img) checks if a .card container contains an <img> element inside its subtree. If true, the rule set applies to the .card parent.
4. Syntax
/* Parent Selector */
.card:has(img) {
padding-top: 0;
}
/* Specificity-free grouping */
:where(h1, h2, h3) {
margin-top: 0; /* Specificity score: 0,0,0,0 */
}
5. Relational Pseudo-class Reference Table
| Pseudo-class | Specificity Weight | Target Description |
|---|---|---|
:has(selector) | Weight of highest argument | Parent selector matching ancestor containers |
:is(s1, s2) | Weight of highest argument | Shortens repetitive selector list groupings |
:where(s1, s2) | Zero specificity (0,0,0,0) | Framework base rules that are easily overridden |
6. Beginner Example
/* Grouping headings without repeating article parent */
article :is(h1, h2, h3) {
color: #0f172a;
}
7. Intermediate Example
/* Style form label when field is invalid */
.form-field:has(input:invalid) {
border-color: #ef4444;
}
8. Advanced Relational Navigation State Example
/* Dims neighboring navigation items when any sibling link is hovered */
.nav-list:has(a:hover) a:not(:hover) {
opacity: 0.5;
}
9. Line-by-Line Code Explanation
.nav-list:has(a:hover) a:not(:hover): Targets.nav-listif it contains a hovered link, and dims all other links inside it that are NOT currently hovered.
10. Expected Terminal / Browser Output
A interactive navigation menu where hovering over any link gracefully dims all non-hovered sibling links.
11. Common Mistakes & Pitfalls
- Confusing
:is()and:where()specificity::is()inflates specificity to match its highest argument, whereas:where()always evaluates to0,0,0,0.
12. Important Rules & Limitations
:has()is fully supported across all modern evergreen browsers (Chrome 105+, Safari 15.4+, Firefox 121+).
13. Multilingual Explanation: English
:has() is the CSS parent selector. Use :is() to group selectors and :where() for zero-specificity base rules.
14. Multilingual Explanation: Hindi (Roman Script)
:has() CSS ka parent selector hai jo child ke basis par parent ko target karta hai. :where() ki specificity zero hoti hai.
15. Multilingual Explanation: Marathi (Roman Script)
:has() mule child element cha aadhar ghevun parent la target karta येते. :where() chi specificity zero aastati.
16. Practice Questions
- How does
:has()eliminate the need for JavaScript class toggling on parent containers?
17. Practical Coding Challenge
Write a CSS rule using .card-box:has(.badge-sale) to add a gold border to card containers that contain a sale badge.