Recently, I came across some interesting HTML code:
<label>
<input type="checkbox" value="cb_val" name="cb_name">
my checkbox text
</label>
After applying CSS, I successfully added a background-color to the <label>
tag.
label { background-color:#333; color:#FFF; }
However, now I am facing a challenge. I want to change the background color of the label when the checkbox is checked using only CSS without relying on JavaScript. Some solutions I found use the adjacent sibling selector and assume the label appears after the checkbox, which isn't always the case.
If anyone has any ideas on how to achieve this effect with just CSS, please share your insights!
UPDATE:
Unfortunately, it seems like achieving this effect purely through CSS is not possible. I may have to resort to using JavaScript or explore alternative HTML structures to get the desired result. One suggestion was placing the checkbox absolute on top of the label for a similar visual effect. Another option is to apply the background color to both the label and the checkbox simultaneously.
Thank you for all the assistance provided!