Style Label When Radio Button is Checked in Tailwind
Want to style your labels when your radio button is selected? Here’s how to do that in CSS and Tailwind.
Jul 31, 2024
This is a problem I solved before but somehow forgot how to, so I’m documenting this here:
Here are the steps to achieve this:
- First make sure the input field comes first before the label because the logic is to make use of the next sibling selector.
- Then hide the input field but before then, make sure the label is linked to the input using htmlFor.
- The individual labels and input should be grouped in its own parent div.
- Then use `input[type=”radio”]:checked + label` and style.
- If it is Tailwind, add a class of `peer` to the input field
- Then on the label, use `peer-checked:styling`
And that’s that.
Hopefully this helps someone someday.