17

I have logo in my website, it is grayscaled on hover i want it to be colored smoothly. it is working but not smoothly. i am using CSS transition.

This is my code

<img alt="TT ltd logo" src="./img/tt-logo.png" class="tt-logo" />

   <style>
    img.tt-logo {
      filter: grayscale(1);
      transition: grayscale 0.5s;
    }

    img.tt-logo:hover {
      filter: grayscale(0);
    }
   </style>
Maria
  • 193
  • 1
  • 1
  • 9

3 Answers3

32

Try do it this way:

 img.tt-logo {
   -webkit-filter: grayscale(100%);
   -moz-filter: grayscale(100%);
   filter: grayscale(100%);
   transition: all 0.5s ease;
 }

 img.tt-logo:hover {
   -webkit-filter: grayscale(0%);
   -moz-filter: grayscale(0%);
   filter: grayscale(0%);
 }

and every image has its own alt, you can use it without using img.class:

 img[alt="TT ltd logo"] {
   -webkit-filter: grayscale(100%);
   -moz-filter: grayscale(100%);
   filter: grayscale(100%);
   transition: all 0.5s ease;
 }

 img[alt="TT ltd logo"]:hover {
   -webkit-filter: grayscale(0%);
   -moz-filter: grayscale(0%);
   filter: grayscale(0%);
 }

in this case class is extra

Igor Ivancha
  • 3,413
  • 4
  • 30
  • 39
10

Animating a filter takes a lot of computation and might hurt performance in some browsers.

You can get better performance by animating the opacity of a grayscale image to reveal a full-color image beneath it.

Here's an example.

Rafi
  • 816
  • 9
  • 21
4

With the current state of browsers, you could go for the example below, short and simple. Modern browsers nowadays support the grayscale CSS attribute and if you're only transitioning one attribute, best reference one attribute instead of all attributes.

img {
    grayscale: 1;
    transition: filter .23s ease-in-out;
}

img:hover {
    grayscale: 0;
}

Sources:

BroodjeBE
  • 95
  • 10