How to create a rollover image using CSS
Here is how to implement a rollover image using CSS. Before getting started, we need to have two images ready, one in its initial/static state as well as its rollover state.
Place both the static and rollover image in one file and make sure that the rollover image is placed on top of the static one. To achieve the rollover effect, we’ll write a code to display the static image and crop the hover image, so that only one image state is displayed at a time.
For this tutorial, we’ll use the following as a CSS rollover image.
Creating an HTML Anchor Element for our Image
Instead of adding the image file in a tag, we’ll display it as a background image of an (anchor) tag. Here’s the HTML that we need to add:
<a class=”rolloverimage” href=”https://helplogger.blogspot.com/2016/10/#URL“>Rollover Image</a>
Note: if you want to make the image clickable, replace #URL with the url of the webpage where you want the link to point to.
Using CSS to Set a Background Image
To create the mouseover image effect, we’ll use the :hover CSS pseudo-class. Then, we’ll use the background-position property and set the values to 0 0 to move the background image to the upper left corner which will create the rollover effect.
background: url(“https://helplogger.blogspot.com/2016/10/https://1.bp.blogspot.com/-NtrjKUd-0fs/V_ewyJEdB_I/AAAAAAAAL28/D3OfCvvRv4oADehdckX0d919xFeggWZOwCLcB/s180/rollover-image-light-bulb-on-off.png‘) bottom;
background-position: 0 0;
Note: Replace the text in blue with the url of your image file. Please pay attention on the width and height values marked in red, these should be different depending on your file, where the height value is for only one image and not the entire image file!
Hover your mouse cursor over the light bulb to see the rollover image effect in action:
Adding Rollover Image to Blogger
Or, if you want to add it inside one of your posts, when you create a New Post, switch to the HTML tab and paste the codes inside the content box.
And this is how you make images swap on mouseover using CSS. Enjoy!