Day 50 - The Mightiest Selectors - :has() and :not() in Action
Emiliano Pisu
?? Sensei & Co-Host @ DevDojo IT ?? Speaker ?? Accessibility WCAG Expert ? Turning Designs into interactive things ??♂? Your friendly neighborhood UI/UX Engineer
?? An Awesome CSS Link a Day – Day 50 ??
The Mightiest Selectors - :has() and :not() in Action
The CSS :has() pseudo-class is a true game-changer! ?? It allows you to style elements based on their children or descendants, something that was previously impossible with pure CSS. With :has(), you can create powerful conditional styles, improve accessibility, and even replace some JavaScript use cases. Combining it with the well known :not() selector it's possible to do some black magic with our CSS.
Introduction
Have you ever wanted to style an element only if it contains a specific child? Or maybe change the appearance of a parent based on its internal content? Well, with :has(), you can now do it all directly in CSS!
For example, you can apply a unique style to a card only if it contains an image, or modify a container’s layout based on the presence of a specific button. In short, :has() brings conditional logic into CSS.
?? Theory :has() and :not() Beyond the Basics
The :has() selector is one of the most powerful tools in modern CSS, but it’s often misunderstood or compared to :not(). Here are some key insights:
?? Today’s Links
To celebrate the power of :has() and :not(), here are two must-read resources:
?? The :not() Selector in Use – by Silvestar Bistrovi?
?? Decoding CSS Selectors :has(:not()) vs :not(:has()) – by Polypane
These articles will help you understand how :has() and :not() work together and how to leverage them for more dynamic, powerful styles.
?? Giveaways
? Why This Rocks
??? Join the Conversation
Let’s discuss and explore new ways to leverage :has(), :not() and related combos!
?? Sensei & Co-Host @ DevDojo IT ?? Speaker ?? Accessibility WCAG Expert ? Turning Designs into interactive things ??♂? Your friendly neighborhood UI/UX Engineer
2 天前Errata Corrige: Hi Folks! I’m sorry for the typo in the title: I wrote :now() instead of :not()