A card with an image may need different spacing from a text-only card. Adding a class for every content combination can become tedious, especially when a publishing system controls the markup. CSS :has() offers a way to style a component according to a relationship expressed in a selector.
The MDN reference for :has() describes it as a relational pseudo-class. The element before :has() is the one being matched; the argument describes a condition relative to that element. Although it is often called a parent selector, its possibilities also include relationships involving siblings.
Read a small example aloud
Consider .card:has(img). Read it as “a card that contains an image”. A rule using that selector applies to the card, not directly to the image. By contrast, .card img selects images inside cards.
For a simple styling experiment, add a border to cards matching the first selector. Create three cards in a local test page: one with an image, one with text alone and one with an image nested inside another element. Check which cards receive the border.
Then change the condition to .card:has(> img). The direct-child relationship is narrower. The nested-image card gives you a useful test of whether the selector expresses the structure you intended.
Test the component, not only the happy path
Write down which content variations your real component allows. Can the image be absent? Can a link wrap it? Does a loading placeholder use a different element? A selector that works on a hand-written example may stop matching when the publishing system adds a wrapper.
Keep the selection as specific to the component as practical. Broad conditions applied high in a large document can make changes harder to reason about and can have performance implications. Use the browser’s tools to inspect the matched rule and test the browsers your audience actually uses.
Also separate presentation from behaviour. A highlighted card does not become keyboard-operable merely because a selector found a link inside it. Form validation, accessible names and interaction still need the appropriate HTML and application behaviour.
The best use of :has() is often modest: a component adapts to a clearly defined variation in its markup. When the selector can be explained in one plain sentence and checked with a few contrasting examples, the styling becomes easier to maintain.

