Languages
[Edit]
EN

CSS - change image on hover

0 points
Created by:
Marley-Marks
442

In this article, we would like to show you how to change image on hover using CSS.

Quick solution:

.logo {
  background: url("https://example.com/image1.png");
}

.logo:hover{
  background: url("https://example.com/image2.png");
}

 

Practical example

In this example, we use :hover pseudo-class to overwrite the background image from .logo class with a new one on hover event.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>

    div {
      width: 230px;
      height: 230px;
    }

    .logo {
      background: url("https://dirask.com/static/bucket/1631898942509-VMYrnXyYZv--image.png") no-repeat;
    }

    .logo:hover{
      background: url("https://dirask.com/static/bucket/1574890428058-BZOQxN2D3p--image.png") no-repeat;
    }

  </style>
</head>
<body>
  <span>Hover the logo to change the image</span>
  <div class="logo"></div>
</body>
</html>

Note:

In this solution we use background instead of the background-image property so we can add no-repeat that disables image repeating.

References

  1. :hover - CSS | MDN
  2. background - CSS | MDN
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join