I am trying to make a checkbox
custom in which the background color of my is changed label
depending on whether it is selected or not, and in this way hide the checkbox
itself. The code I have is this:
.milabel {
background-color:#c9c9c9;
border-radius:5px;
padding:20px;
cursor:pointer;
}
input:checked{
}
<input id="micb" type="checkbox"><br><br>
<label class="milabel" for="micb"> Hola mundo! Soy clickeable </label>
I know that with input:checked
it you can modify the checkbox
itself to give it a certain style but I don't know.
To change the color of
label
Update:
To hide the
checkbox
on selectlabel
you can use a bit ofjQuery
:The background of the checkbox itself cannot be changed.
However, you can hide the
checkbox
and use thelabel
one associated with it to simulate the checkbox. As it islabel
associated with the checkbox, we can give it styles according to the events that it hascheckbox
at any given time.In this case, for the checkbox state
false
the label will be red and when the checkbox state istrue
, the label will be green.In SO in English you have this solution without labels, spans, etc., just with
pseudo-elementos
: