=> Why Targeting Parent Was Always Hard
Before this, CSS could only go downward:
.parent .child {
color: red;
}
But you couldn’t do:
👉 style parent based on child
=> The Game-Changer: :has()
Now you can:
.card:has(img) {
border: 2px solid green;
}
👉 If .card contains an image → style it
=> Real Power Example
Form validation:
.form-group:has(input:invalid) {
border: 1px solid red;
}
👉 No JavaScript needed
=> Interactive UI Without JS
.button:has(:hover) {
background: black;
}
Or:
.menu:has(.active) {
display: block;
}
=> Why This Is Big
You can now:
- reduce JavaScript
- write cleaner CSS
- build smarter UI logic
=> What Developers Often Miss
This is not just a selector.
👉 It’s conditional logic in CSS
=> Performance Note
:has() can be expensive if overused.
Avoid:
*:has(.child)
👉 Too broad = slow
=> Browser Support
Now supported in modern browsers.
But always test before production.
=> Real UI Tip
Use :has() for:
- form states
- conditional styling
- parent-based layouts
=> What Do You Think?
Will :has() reduce the need for JavaScript in UI logic?
Top comments (0)