How to use Background Image Swap In CSS
In CSS, background image swap involves using the :hover pseudo-class on an element and changing its background-image property to a new image URL upon hover, providing a seamless transition effect and enhancing user interaction with the webpage.
Example: Here, we are swapping the background image of an element on hover using CSS. The initial image is set with background-image, and :hover pseudo-class changes it smoothly.
HTML
<!DOCTYPE html> < html lang = "en" > < head > < title > How to Change Image on Hover in CSS </ title > < style > .sudo { width: 230px; height: 195px; margin: 50px; background-image: url( "https://media.w3wiki.org/wp-content/cdn-uploads/20190417124305/250.png"); background-size: cover; transition: background-image 0.3s ease-in-out; } .sudo:hover { background-image: url( "https://media.w3wiki.org/wp-content/uploads/20190506164011/logo3.png"); } </ style > </ head > < body > < h2 >w3wiki</ h2 > < h2 > changing image on hover by Using Background Image Swap </ h2 > < div class = "sudo" ></ div > </ body > </ html > |
Output:
How to Change Image on hover with CSS ?
To change an image on hover with CSS, use the :hover pseudo-class on the image element and alter its properties, such as background-image or content, to display a different image when hovered over.
Here are some common approaches to changing the image on hover:
Table of Content
- Using Background Image Swap
- Using Content Property
Contact Us