What I am trying to achieve, is when the image hovered it will change to another. But I guess that the anchor tag is preventing that. How to do so without using any event handler for the image tag. Thanks.
Consider the following code:
#imgId:hover {
background-image: url("https://via.placeholder.com/100/ff0000");
}
<a href=""><img id="imgId" width="40" height="40" src="https://via.placeholder.com/100"></a>
4
Answers
Unfortunately you can’t do that with
img
tag, but you can try to replace it withdiv
tag instead with the use ofbackground-image
, sample here:(The additional
background-image
in the non-hover state is just to pre-load the image to prevent flickering, as requested by the requester)If you’d rather keep the image element you can use an absolutely-positioned pseudo-element as an overlay. Note that I moved the ID to the anchor for this because images don’t have pseudo-elements.
I just try doing it with
img
tag.You can play with padding to hide the original image: