I have a list of checkboxes and labels. I'm using this CSS to apply a style to the label when the checbox is checked:
input[type=checkbox]:checked + .CheckLabels {background-color: green;}
When I order my HTML like this:
<input type="checkbox" name="C" id="checkC" value="C" checked>
<label class="CheckLabels" for="checkC">C</label>
<input type="checkbox" name="D" id="checkD" value="D" checked>
<label class="CheckLabels" for="checkD">D</label>
It works as intended: clicking the C label adds or removes a green style. However, when I put label before the input, clicking the label causes the following label to have the style applied/removed. Ie: Clicking the C label adds or removes the green from the D label (though the proper checkbox is still checked.)
<label class="CheckLabels" for="checkC">C</label>
<input type="checkbox" name="C" id="checkC" value="C" checked>
<label class="CheckLabels" for="checkD">D</label>
<input type="checkbox" name="D" id="checkD" value="D" checked>
Why is this happening? I'm not particularly attached to the order of the labels and checkboxes, since I'll be using display:none
. But it would be nice to know what is going on.