Combining CSS\u00a0:has()\u00a0And HTML\u00a0<select>\u00a0For Greater Conditional Styling<\/h1>\nAmit Sheen<\/address>\n 2024-05-02T10:00:00+00:00
\n 2025-03-19T12:04:52+00:00
\n <\/header>\n
Even though the CSS :has()<\/code> pseudo-class is relatively new, we already know a lot about it, thanks to many, many articles and tutorials demonstrating its powerful ability to conditionally select elements based on their contents. We\u2019ve all seen the card component and header examples, but the conditional nature of :has()<\/code> actually makes it adept at working with form controls, which are pretty conditional in nature as well.<\/p>\nLet\u2019s look specifically at the <\/code> element. With it, we can make a choice from a series of <\/code>s. Combined with :has()<\/code>, we are capable of manipulating styles based on the selected <\/code>.<\/p>\n\n Option 1\n Option 2\n Option 3\n Option 4\n Option 5\n\n<\/code><\/pre>\nThis is your standard <\/code> usage, producing a dropdown menu that contains options for user selection. And while it\u2019s not mandatory, I\u2019ve added the selected<\/code> attribute to the first <\/code> to set it as the initial selected option.<\/p>\nApplying styles based on a user\u2019s selection is not a new thing. We\u2019ve had the Checkbox Hack<\/a> in our pockets for years, using the :checked<\/code> CSS pseudo-class to style the element based on the selected option. In this next example, I\u2019m changing the element\u2019s color<\/code> and the background-color<\/code> properties based on the selected <\/code>.<\/p>\n\nSee the Pen [demo 01 – Using the :has selector on a dropdown menu](https:\/\/codepen.io\/smashingmag\/pen\/oNOwded) by Amit Sheen<\/a>.<\/p>See the Pen demo 01 – Using the :has selector on a dropdown menu<\/a> by Amit Sheen<\/a>.<\/figcaption><\/figure>\n\n
\n 2025-03-19T12:04:52+00:00
\n <\/header>\n
:has()<\/code> pseudo-class is relatively new, we already know a lot about it, thanks to many, many articles and tutorials demonstrating its powerful ability to conditionally select elements based on their contents. We\u2019ve all seen the card component and header examples, but the conditional nature of :has()<\/code> actually makes it adept at working with form controls, which are pretty conditional in nature as well.<\/p>\nLet\u2019s look specifically at the <\/code> element. With it, we can make a choice from a series of <\/code>s. Combined with :has()<\/code>, we are capable of manipulating styles based on the selected <\/code>.<\/p>\n\n Option 1\n Option 2\n Option 3\n Option 4\n Option 5\n\n<\/code><\/pre>\nThis is your standard <\/code> usage, producing a dropdown menu that contains options for user selection. And while it\u2019s not mandatory, I\u2019ve added the selected<\/code> attribute to the first <\/code> to set it as the initial selected option.<\/p>\nApplying styles based on a user\u2019s selection is not a new thing. We\u2019ve had the Checkbox Hack<\/a> in our pockets for years, using the :checked<\/code> CSS pseudo-class to style the element based on the selected option. In this next example, I\u2019m changing the element\u2019s color<\/code> and the background-color<\/code> properties based on the selected <\/code>.<\/p>\n\nSee the Pen [demo 01 – Using the :has selector on a dropdown menu](https:\/\/codepen.io\/smashingmag\/pen\/oNOwded) by Amit Sheen<\/a>.<\/p>See the Pen demo 01 – Using the :has selector on a dropdown menu<\/a> by Amit Sheen<\/a>.<\/figcaption><\/figure>\n\n