I have a checkbox and an image on an HTML page:
I want the image to disappear when the checkbox is checked, and return back when it's unchecked. It can be easily done with the opacity property but this will only hide the image – and I need it to disappear (so that other elements can take the image's place for example).
I tried to combine opacity with display properties:
img {
display: block;
opacity: 1;
transition: opacity 0.5s ease-in-out;
}
#checkbox:checked ~ div img {
display: none;
opacity: 0;
}
<body>
<input type="checkbox" id="checkbox">
<div>
<img src="https://via.placeholder.com/150" alt="">
</div>
</body>
but it wouldn't work (no transition).
How can I solve this problem?