Vanta Logo
SPONSOR
Automate SOC 2 & ISO 27001 compliance with Vanta. Get $1,000 off.
Published
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.

If you found this article helpful, you might enjoy my free newsletter. I share developer tips and insights to help you grow your skills and career.


More Articles You Might Enjoy

If you enjoyed this article, you might find these related pieces interesting as well. If you like what I have to say, please check out the sponsors who are supporting me. Much appreciated!

Webdev
3 min read

HTML Details Element: The Native Accordion You're Not Using

Discover how the HTML details element can replace your JavaScript accordions and why it might be better than your current solution

Dec 10, 2024
Read article
Webdev
5 min read

How To Restore Your Passion for Programming

Programming is a difficult skill to master and requires great perseverance to get good at. The grind can be too much at times — remember, if something is hard, it’s worth doing, as nothing good comes easy.

Nov 26, 2019
Read article
Webdev
13 min read

10 Essential Terminal Commands Every Developer Should Know

List of useful Unix terminal commands to boost your productivity. Here are some of my favorites.

Aug 21, 2024
Read article
Webdev
3 min read

Preloading Responsive Images

How to properly preload responsive images to improve initial page load

Nov 28, 2024
Read article
Webdev
5 min read

The Secret to Being a Top Developer Is Building Things

You can only become a great developer if you're willing to put effort into it

Dec 2, 2017
Read article
Webdev
7 min read

How to Land Your First Tech Job

A developer's guide to tech interviews - from someone who sits on both sides of the table

Oct 24, 2024
Read article
Webdev
7 min read

What Does an Entry-Level Programmer Need to Know Exactly?

Expectations for entry-level programmers

Nov 6, 2019
Read article
Webdev
4 min read

The What, Why, and How of Using a Skeleton Loading Screen

Skeleton loading screens enhance user experience and make your app feel faster

Nov 12, 2020
Read article
Webdev
4 min read

Programming Trends to Watch in 2020 and Beyond

Here are my bets on the programming trends

Jul 19, 2019
Read article

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. By using my affiliate links, you support my work and get a discount at the same!


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.