How to Change Image on hover with CSS ? Last Updated : 06 Mar, 2024 Comments Improve Suggest changes Like Article Like Report 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 SwapUsing Content Property1. Using Background Image Swap: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.geeksforgeeks.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.geeksforgeeks.org/wp-content/uploads/20190506164011/logo3.png"); } </style> </head> <body> <h2>GeeksForGeeks</h2> <h2> changing image on hover by Using Background Image Swap </h2> <div class="sudo"></div> </body> </html> Output: Using Background Image Swap 2. Using Content Property:The Content Property utilizes CSS's ::before and ::after pseudo-elements to insert content before or after an element, often used for generating dynamic content or altering the appearance of elements. Example: In this example we demonstrates changing an image on hover using the Content Property. The ::before pseudo-element is utilized to display different images before and after hover, providing a smooth transition effect. HTML <!DOCTYPE html> <html lang="en"> <head> <title>Change Image on Hover</title> <style> .image-container { width: 200px; height: 200px; position: relative; overflow: hidden; } .image-container::before { content: url( 'https://media.geeksforgeeks.org/wp-content/cdn-uploads/20190417124305/250.png'); position: absolute; top: 0; left: 0; width: 100%; height: 100%; transition: opacity 0.3s ease-in-out; } .image-container:hover::before { content: url( 'https://media.geeksforgeeks.org/wp-content/uploads/20190506164011/logo3.png'); } </style> </head> <body> <h2>GeeksForGeeks</h2> <h2> changing image on hover by Using Content Property </h2> <div class="image-container"></div> </body> </html> Output: Using Content Property Comment More infoAdvertise with us Next Article How to Change Image on hover with CSS ? M manaschhabra2 Follow Improve Article Tags : Web Technologies CSS CSS-Questions Similar Reads How to Change Image Opacity on Hover using CSS ? Changing the opacity of an image on hover can add a more elegant and interactive effect to your website. This simple yet effective technique can be implemented using CSS. When a user hovers over an image, it gradually becomes more transparent, pointing out the interaction and creating an attractive 2 min read How to Change Image on Hover using Tailwind CSS? One common effect is changing an image when the user hovers over it. We use Tailwind CSS, a utility-first CSS framework, to accomplish this without any additional JavaScript logic for the hover effect. By utilizing Tailwind's built-in classes we can create smooth transitions between two images where 2 min read How to Flip an Image on hover using CSS? Flipping an image with a mirror effect on hover using CSS enhances visual interaction by applying transformations along the X or Y axis. This dynamic hover animation creates an engaging experience by flipping the image horizontally or vertically, resulting in an eye-catching, interactive effect.Appr 2 min read How to Change Tabs on Hover with CSS and JavaScript ? In this article, we are going to learn how can we change tabs on hover, with CSS and JavaScript. Changing tabs on hover enhances the user experience by offering interactive hover navigation. Users can quickly preview different sections without clicking which reduces the time it takes to explore diff 4 min read How to Zoom an Image on Mouse Hover using CSS? CSS plays a vital role in styling modern websites, with over 90% of sites using it for visual effects and responsive layouts. One popular effect is image zoom on hover, which enhances user experience and adds visual appeal. In this article, youâll learn three simple ways to create image zoom effects 2 min read How to Write a:hover in Inline CSS? The :hover pseudo-selector in CSS enables you to modify the style of elements when a user hovers their mouse over them. This selector is widely used to improve user experience by adding visual feedback on elements like buttons, links, images, or any interactive items on a webpage. For the :hover eff 2 min read How To Change The Cursor On Hovering In CSS? Changing the cursor style when a user hovers over a particular element is a common user experience enhancement in web development. The default cursor is usually a pointer (arrow) but CSS allows us to change the cursor to various shapes such as a hand (for links), text selection, or even custom icons 2 min read How to Change Image Color in CSS? Here are the methods to Change Image Color in CSS:1. Applying CSS FiltersCSS filters allow you to apply visual effects directly to images, such as changing colors, adjusting brightness, or applying grayscale.HTML<html> <head> <style> .grayscale { filter: grayscale(100%); } .sepia { 3 min read How to Change the Border Color on Hover in CSS ? Border color in CSS defines the color of an elementâs border. You can change the border color on hover using the :hover pseudo-class, allowing you to modify the borderâs appearance when a user hovers over the element. Using CSS hover Pseudo ClassThe hover pseudo-class in CSS is a powerful tool that 1 min read How to Change the Color of Link on Hover using CSS ? Changing the color of a link on hover can provide visual feedback to users, indicating that the link is interactive. It improves the user experience by helping users understand that the link is clickable. These are the following approaches: Table of Content Using CSS pseudo-classUsing CSS VariablesU 2 min read Like