EN
CSS - grayscale and re-color image on hover
0 points
In this article, we would like to show you how to grayscale and re-color image on hover effect using CSS.
Quick solution:
xxxxxxxxxx
1
img {
2
filter: grayscale(100%);
3
}
4
5
img:hover {
6
filter: grayscale(0%);
7
}
The :hover
CSS pseudo-class is triggered when the user hovers over an img
element with the cursor, changing the img
style to the one specified within curly brackets.
In this example, we set the grayscale filter on the img
element to 100%
at the beginning and disable it on hover effect using CSS pseudo-class.
xxxxxxxxxx
1
2
<html>
3
<head>
4
<style>
5
6
img {
7
transition: 1s;
8
filter: grayscale(100%); /* <----- required */
9
}
10
11
img:hover {
12
filter: grayscale(0%); /* <----- required */
13
}
14
15
</style>
16
</head>
17
<body>
18
<img src="https://dirask.com/static/bucket/1624644642609-mbQLqBEYJX--example-img-730w.jpg" />
19
</body>
20
</html>