how to select unwrapped text inside a label that is next to an input?
<label class="myLabel">
<input type"checkbox" class="myCheckbox" />
Apples
</label>
<label class="myLabel">
<input type"checkbox" class="myCheckbox" />
Bananas
</label>
<label class="myLabel">
<input type"checkbox" class="myCheckbox" />
Oranges
</label>
<label class="myLabel">
<input type"checkbox" class="myCheckbox" />
Peaches
</label>
<label class="myLabel">
<input type"checkbox" class="myCheckbox" />
Strawberries
</label>
i would like to change the background of the unwrapped text when input type=checkbox
is checked.
i tried >*
but it only works for wrapped elements
2
Answers
You could put a pseudo element behind the text:
For people coming here in search of an answer that doesn’t follow the strict criteria of only being able to only use CSS, but one that is widely supported I’d like to mention the possibility of using the "for" keyword on the label.
With this approach you will however have to add that key to the label element as well as place the select checkbox input outside of the label element.
So the HTML will have to be slightly altered but this actually has wide browser support instead of the has() selector which has very limited support.