I'm trying to use a custom image for checkboxes, as discussed in this question:
Pure CSS Checkbox Image replacement
My checkboxes appear in a grid, so don't need a label associated with them. However, I find that if the label isn't present, the technique discussed in that question doesn't work.
For example, here's the relevant css:
input[type=checkbox] {
opacity: 0;
}
input[type=checkbox] {
height: 17px;
width: 17px;
background: url('/resources/img/checkBox-unchecked.png') 0 0px no-repeat;
}
input[type=checkbox]:checked {
background: url('/resources/img/checkBox-checked.png') 0 0px no-repeat;
height: 17px;
width: 17px;
padding: 0 0 0 0px;
}
Declaring just a simple input
doesn't render the image:
<input type="checkbox">
However, if I modify the css, appending the + label
, and append a label to the markup, as discussed in the solution, it works:
input[type=checkbox] {
opacity: 0;
}
input[type=checkbox] + label {
height: 17px;
width: 17px;
background: url('/resources/img/checkBox-unchecked.png') 0 0px no-repeat;
}
input[type=checkbox]:checked + label {
background: url('/resources/img/checkBox-checked.png') 0 0px no-repeat;
height: 17px;
width: 17px;
padding: 0 0 0 0px;
}
<input type="checkbox"><label></label>
This renders as expected.
Why does the first solution not work?
How can I acheive this without having to add labels that aren't required?