"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Achieve Grayscale with Color Restoration on Mouse-Over Using CSS?

How to Achieve Grayscale with Color Restoration on Mouse-Over Using CSS?

Published on 2024-11-21
Browse:998

How to Achieve Grayscale with Color Restoration on Mouse-Over Using CSS?

Greyscaling with Color Restoration on Mouse-over Using CSS

Achieving a grayscale appearance with color restoration on mouse-over is possible through various methods, providing cross-browser compatibility in both IE and Firefox.

Method 1: Pure CSS (Using a Single Colored Image)

This technique utilizes the filter property with vendor prefixes to grayscale the image in all supported browsers:

img.grayscale {
  filter: url("data:image/svg xml;utf8, ..."); /* Firefox 3.5  */
  filter: gray; /* IE6-9 */
  -webkit-filter: grayscale(100%); /* Chrome 19 , Safari 6  */
}

img.grayscale:hover {
  filter: none;
  -webkit-filter: grayscale(0%);
}

Method 2: Pure CSS (Using Two Images)

Another approach involves using two images: a grayscale version and a colored version. The grayscale image is initially displayed, and the hover state transitions to the colored image:

img {
  transition: all .6s ease;
}

img:hover {
  opacity: 0;
}

.grayscale {
  opacity: 1;
}
How to Achieve Grayscale with Color Restoration on Mouse-Over Using CSS?How to Achieve Grayscale with Color Restoration on Mouse-Over Using CSS?

Method 3: SVG with CSS Filters

For IE10 and modern browsers, inline SVG can be used to apply filters, allowing the grayscale effect to be controlled dynamically:

svg image {
  transition: all .6s ease;
}

svg image:hover {
  opacity: 0;
}
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3