Seo Blogs Submitter Seo Blogs Submitter
Seo Blogs Submitter
Seo Blogs Submitter Seo Blogs Submitter
  • News
    • Press Release
  • Business
    • Crypto
    • Economy
    • Finance
    • Market
    • Money
  • Tech
    • Apps
    • Gadget
    • Gaming
    • Mobile
    • Science
  • Blogs
  • Digital Marketing
    • Artificial Intelligence
    • Blockchain Technology
    • Cryptocurrency
    • Keyword Research
    • SEO Services
    • Software
    • Web Design & Development
  • Lifestyle
    • Celebrity
    • Culture
    • Education
    • Fashion
    • Food
    • Health
    • Nature
    • Religion
    • Science
    • Travel
  • Real Estate
  • Classifieds
  • Top Local Professionals
Seo Blogs Submitter / News / Web Design & Development / Combining CSS :has() And HTML  For Greater Conditional Styling — Smashing Magazine
Prev Post
Next Post
May 29
Web Design & Development

Combining CSS :has() And HTML  For Greater Conditional Styling — Smashing Magazine

  • Seo Blogs Submitter
  • . May 29, 2024
  • 182 Views



While the CSS :has() pseudo-class is widely celebrated for its ability to select a parent element up the chain conditionally based on its contents, there is more conditional logic it is capable of handling when we move it up the chain, so to speak. Amit Sheen demonstrates using :has() to apply styles conditionally when a certain in a element. With it, we can make a choice from a series of s. Combined with :has(), we are capable of manipulating styles based on the selected .


This is your standard element according to the value of the selected , we select the element if it has a specific [value] that is :checked.

See the Pen [demo 02 – Using the :has selector on a dropdown menu](https://codepen.io/smashingmag/pen/eYoRopZ) by Amit Sheen.

See the Pen demo 02 – Using the :has selector on a dropdown menu by Amit Sheen.

Just how practical is this? One way I’m using it is to style mandatory element as the parent of an , then we can also use it to style the parent of the child of the document :root :has() a particular that is :checked:

:root:has(select [value="foo"]:checked) {
  // Styles applied if 

This is useful for setting a custom property value dynamically or applying a set of styles for the whole page. Let’s make a little style picker that illustrates the idea of setting styles on an entire page.

See the Pen [demo 03 – Using the :has selector on a dropdown menu](https://codepen.io/smashingmag/pen/yLrXroO) by Amit Sheen.

See the Pen demo 03 – Using the :has selector on a dropdown menu by Amit Sheen.

Or perhaps a theme picker:

See the Pen [demo 04 – Using the :has selector on a dropdown menu](https://codepen.io/smashingmag/pen/OJGgjaJ) by Amit Sheen.

See the Pen demo 04 – Using the :has selector on a dropdown menu by Amit Sheen.

How that last example works is that I added a class to each elements on the page.

And, of course, we don’t have to go all the way up to the :root element. If we’re working with a specific component, we can scope :has() to that component like in the following demo of a star rating component.

See the Pen [demo 05 – Using the :has selector on a dropdown menu](https://codepen.io/smashingmag/pen/rNbwvqz) by Amit Sheen.

See the Pen demo 05 – Using the :has selector on a dropdown menu by Amit Sheen.

Watch a short video tutorial I made on using CSS to create 3D animated stars.

Conclusion

We’d be doing :has() a great disservice if we only saw it as a “parent selector” rather than the great conditional operator it is for applying styles all the way up the chain. Seen this way, it’s more of a modern upgrade to the Checkbox Hack in that it sends styles up like we were never able to do before.

There are endless examples of using :has() to create style variations of a component according to its contents. We’ve even seen it used to accomplish the once-complicated linked card pattern. But now you have an example for using it to create dropdown menus that conditionally apply styles (or don’t) to a page or component based the currently selected option — depending on how far up the chain we scope it.

I’ve used this technique a few different ways — e.g., as form validation, a style picker, and star ratings — but I’m sure there are plenty of other ways you can imagine how to use it in your own work. And if you are using :has() on a