Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Css stops working when I add a class of a checkbox's label [duplicate]

Tags:

html

css

I'm really sorry, I know this is a very basic question.

The code works fine, except if I try to apply the attribute to another object, in this case a label. class "c". I don't understand why such a basic thing is not working.

HTML:

 <button type="button" id="boton" disabled>hi</button>
 <input type="checkbox" id="checky"/>
 <label class="c" id="checkyl" for="checky">etiquette</label>

CSS:

#boton:disabled:hover + .c {
  font-weight: bold;
}
like image 442
M.M. Avatar asked Oct 29 '25 23:10

M.M.


2 Answers

You are probably looking for the general sibling selector ~

#boton:disabled:hover~.c {
  font-weight: bold;
}
<button type="button" id="boton" disabled>hi</button>
<input type="checkbox" id="checky" />
<label class="c" id="checkyl" for="checky">etiquette</label>

The adjacent sibling selector + won't work in this case because .c does not come directly after #boton

like image 194
ksav Avatar answered Nov 01 '25 12:11

ksav


.c1 + .c2 {
  /* styles here  */
}

the above selector selects element with class c2 which is just after the element with class .c1

.c1 ~ .c2 {
  /* styles here  */
}

This selector selects element with class c2 which is sibling if the element with class .c1

In your case you need to use sibling(~) selector instead of adjacent-sibling(+) selector

#boton:disabled:hover~.c {
  font-weight: bold;
}
like image 42
Mohana Naga Venkat Sayempu Avatar answered Nov 01 '25 13:11

Mohana Naga Venkat Sayempu



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!