How to use images like checkboxes with CSS?

Sometimes, we want to use images like checkboxes with CSS.

In this article, we’ll look at how to use images like checkboxes with CSS.

How to use images like checkboxes with CSS?

To use images like checkboxes with CSS, we can add a label and an input and style them.

For instance, we write

<input type="checkbox" id="myCheckbox1" />
<label for="myCheckbox1"><img src="https://picsum.photos/200/200" /></label>

to add a checkbox with a label that has an img element in it.

Then we set the background image of the label with CSS by writing

label::before {
  background-image: url(../path/to/unchecked.png);
}

:checked + label::before {
  background-image: url(../path/to/checked.png);
}

We set the background image to replace the checkbox with the image with

label::before {
  background-image: url(../path/to/unchecked.png);
}

And the we set the image that shows when the checkbox is checked instead of the checkbox with

:checked + label::before {
  background-image: url(../path/to/checked.png);
}

Conclusion

To use images like checkboxes with CSS, we can add a label and an input and style them.