Sentry Logo Debug Microservices & Distributed Systems

Join my free newsletter

Level up your dev skills and career with curated tips, practical advice, and in-depth tech insights – all delivered straight to your inbox.

3 min read
Up to date

Trevor I. Lasn

Staff Software Engineer & Engineering Manager

CSS :has() - The Parent Selector We've Always Wanted

Transform your CSS with :has(), the game-changing selector that finally lets us style elements based on their children.

The :has() selector addresses a long-standing limitation in CSS - the inability to style elements based on their contents.

While CSS has always allowed styling children based on their parents, the reverse wasn’t possible until now. This change brings new possibilities for dynamic, content-aware styling.

The :has() selector functions as a conditional check for element contents. It enables style application based on whether an element contains specific children or siblings:

This straightforward syntax eliminates the need for JavaScript or complex CSS workarounds that were previously necessary.

The :has() selector becomes more powerful when combined with other selectors for checking states, positions, and combinations:

The :has() selector enables adaptive layouts that respond to content structure:

This approach eliminates manual class management or JavaScript intervention for layout adjustments.

The :has() selector fundamentally changes CSS architecture by enabling content-aware styling without JavaScript. Its ability to style parent elements based on their children opens up robust, maintainable approaches to common layout challenges.

Modern browsers support :has() well, making it production-ready for contemporary web development. For older browsers, implement fallback styles:

My tip: Start small - replace JavaScript-based style toggles with :has() selectors. Move gradually to content-aware layouts.


Become a better engineer

Here are engineering resources I've personally vetted and use. They focus on skills you'll actually need to build and scale real projects - the kind of experience that gets you hired or promoted.

Many companies have a fixed annual stipend per engineer (e.g. $2,000) for use towards learning resources. If your company offers this stipend, you can forward them your invoices directly for reimbursement.


This article was originally published on https://www.trevorlasn.com/blog/has-css-functional-pseudo-class. It was written by a human and polished using grammar tools for clarity.

Interested in a partnership? Shoot me an email at hi [at] trevorlasn.com with all relevant information.