When I put the label after checkbox, this CSS selector works.
input[type=checkbox]:checked + label {
color: red;
}
<input type="checkbox"><label>Checkbox1</label>
But I'd like to make the checkbox to be checked when the label is clicked on as well.
Here is the CSS for the updated HTML.
label > input[type=checkbox]:checked {
color: red;
}
<label><input type="checkbox">Checkbox1</label>
What could I be doing wrong here?
Here is the simplified HTML and style in one file.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Selectors Test Ground</title>
<style>
label > input[type=checkbox]:checked {
color: red;
}
</style>
</head>
<body>
<form id="checkBoxForm">
<ul>
<li>
<label><input type="checkbox">Checkbox1</label>
</li>
<li>
<label><input type="checkbox">Checkbox2</label>
</li>
<li>
<label><input type="checkbox">Checkbox3</label>
</li>
</ul>
</form>
</body>