The :has() Selector: Bringing Real Logic to CSS

Published: (December 12, 2025 at 12:05 PM EST)
1 min read
Source: Dev.to

Source: Dev.to

Cover image for The :has() Selector: Bringing Real Logic to CSS

“If” logic in CSS — yes, it’s real.

We’ve all said:

“CSS can’t do logic.”

Turns out… that’s not true.

With :has(), CSS can behave like an if condition — letting you style an element based on what it contains or what’s happening inside it.

/* If a card has an image, add a border */
.card:has(img) {
  border: 2px solid #4f46e5;
}

In simple terms: if a .card contains an img, then apply the style.

Why this matters

  • Less JavaScript to manage UI state
  • More reactive, intelligent layouts
  • Cleaner, simpler component logic
Back to Blog

Related posts

Read more »

Fizz Buzz in CSS

Article URL: https://susam.net/fizz-buzz-in-css.html Comments URL: https://news.ycombinator.com/item?id=46166708 Points: 22 Comments: 5...

Cake Menu with Ring using Checkboxes

!Forem Logohttps://media2.dev.to/dynamic/image/width=65,height=,fit=scale-down,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%...