How to use CSS to change the image on Hover

🗓️ Updated: 15.08.2022
💬Comments: 2
👁️Views: 9606

How do you can change the image on Hover? This can be done with CSS.

A ready example:

See the Pen How to use CSS to change the image on hover by kirill (@kirivaha) on CodePen.

Explanation. How did we make the image change on hove? We add two images to the HTML code, but hide one of them with :nth-of-type(2). Then when you hover over a parent element, you hide the first one (display: none), but show the second one by setting display: block; property to it

Was the article helpful?
Yes - 32
Not - 10
Robert Lipinski

Hello, I like your solution, but how can I control the speed of hover effect?


You need another approach to make a smooth transition.

It is not suitable to use “display:none” for this.
You can try opacity:0 and visibility:hidden – and then replace the values with “1” and “visible” respectively.

CSS Головоломки в Telegram
Subscribe and don't miss:
Actual news
Interesting puzzles
Useful links
Our rating
🏆The best website hosting
Что читают?
🏆Популярные записи
How to make a burger menu – complete code and detailed explanation
Views: 54829
How to make a smooth zoom of the image on hover – effect on pure CSS
Views: 41401
Appearance of elements on scrolling
Views: 40640
How to center an image with CSS
Views: 25792
How to make a button click effect in CSS
Views: 18983
Check of knowledge
🤔How well do you know CSS?
Есть два блочных элемента, которые идут друг за другом в html. Какой будет оступ (margin) между ними, если задать им такие стили:

.top {
  height: 30px;
  background-color: blue;
  margin-bottom: 10px;
.bottom {
  height: 30px;
  background-color: red;
  margin-top: 20px;
Viva Magenta
🤔Color of 2023
The Pantone Research Institute has chosen the color of the year for 2023. They became a carmine-red shade with a purple undertone, which was called Viva Magenta.