Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Target the label of a checked input

If I have a radio input that is wrapped within a label, how can I target the label when the input is checked?

<div>
  <p>Payment Plan:</p>
  <label><input name="os0" type="radio" value="monthly">TEST</label>
</div>

I tried:

input:checked + label { color: red }

and

input:checked + label 

But none worked, what I am doing wrong? I also tried the > selector.

The reason I have label wrapping the input, is because I NEED the label to be clickable

like image 902
user3368706 Avatar asked Dec 01 '25 20:12

user3368706


1 Answers

There's no parent or backward selector in CSS (yet?). Thus, we can't select the wrapper label by the wrapped input.

There are two options:

1) Wrapping the content by an inline wrapper like <span> element, as follows:

<label>
  <input name="os0" type="radio" value="monthly">
  <span>TEST</span>
</label>

Then select the <span> by using adjacent sibling selector +:

input:checked + span {
   color: red
}

WORKING DEMO

2) Using for attribute for the label to target the input by its id attribute as follows:

<input name="os0" type="radio" id="myinput" value="monthly">
<label for="myinput">TEST</label>

And Then select the label by:

input:checked + label {
    color: red
}

WORKING DEMO.

like image 65
Hashem Qolami Avatar answered Dec 03 '25 09:12

Hashem Qolami



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!