EN
CSS - change image on hover
0 points
In this article, we would like to show you how to change image on hover using CSS.
Quick solution:
xxxxxxxxxx
1
.logo {
2
background: url("https://example.com/image1.png");
3
}
4
5
.logo:hover{
6
background: url("https://example.com/image2.png");
7
}
In this example, we use :hover
pseudo-class to overwrite the background image from .logo
class with a new one on hover event.
xxxxxxxxxx
1
2
<html>
3
<head>
4
<style>
5
6
div {
7
width: 230px;
8
height: 230px;
9
}
10
11
.logo {
12
background: url("https://dirask.com/static/bucket/1631898942509-VMYrnXyYZv--image.png") no-repeat;
13
}
14
15
.logo:hover{
16
background: url("https://dirask.com/static/bucket/1574890428058-BZOQxN2D3p--image.png") no-repeat;
17
}
18
19
</style>
20
</head>
21
<body>
22
<span>Hover the logo to change the image</span>
23
<div class="logo"></div>
24
</body>
25
</html>
Note:
In this solution we use
background
instead of thebackground-image
property so we can addno-repeat
that disables image repeating.