That is one way to do it. Another way is to have one image as the background and the other image as an img in the container with the background. Then use hover to change the img opacity and reveal the bg.
Thanks for replies, really like @pdxSherpa solution but the Jquery stuff may push my brain to boiling point so I’m going to take a closer look at @SamA74 idea, will update here with how I progress
I just cant get image to change on mouse hover. I followed this tutorial:http://www.corelangs.com/css/box/hover.html “CSS hover image swap - onmouseover Event” but I just cant get it to work
Heres the css Ive bodged up in some way:.top-image-text{
background-image:url(…/test/images/wedding-photography-service-200.jpg)
width:200px;
height:200px;
}
The element ‘.top-image-text’ is actually your html image and not a containing element as such which means that when you hover your image you are changing the background of the image. Of course you will see no effect because the html image is in front of its own background so you will see nothing.
Although I guess this was not you were intending (as it is not mentioned in that tutorial) but you can change the background of the image on hover if you set its height and width to zero but create space for the background with padding.
I amy be worrying for no reason but is my css fix to get different rollover images a bit bloated, I have a bad feeling there should be a more efficient way of achieving the same result.
Heres the relevant css so far:
img.top-image-text{
float:left;
clear:left;
margin:0 10px 20px 0;
}
The problem I’m seeing in that method (I think that’s what it is) is you are not loading the background image until the hover begins. That is causing a delay, I’m seeing a blank white bg while the image loads on hover.
I think its best to have the background image there all along, obscured by the foreground image until the hover, then hide the foreground by whichever means you like, opacity, transform, cropping etc…
Yes that would be best as long as the foreground image doesn’t have transparent parts (Otherwise you will see the background image).
However you could still do this but just offset the background-position so that the background is offscreen (background-position:-999em -999em) initially and then just move it back on hover.