Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS transition image from black white to color

I've seen many websites that have a black and white image that transitions to a colored version with a :hover selector. What I'm trying to do is the same but with no user interaction. For example, the image would start black and white and fade to its colored version after 10 seconds.

Is this possible with CSS?

like image 596
Eduardo Martins Avatar asked Jul 13 '16 03:07

Eduardo Martins


People also ask

How do I change an image from black to white in CSS?

You can use filter: -webkit-filter: grayscale(1) invert(1); filter: grayscale(1) invert(1); Or just use invert(1) instead of grayscale(1) invert(1) if you have black and white image.

Can we change the color of image using CSS?

Given an image and the task is to change the image color using CSS. Use filter function to change the png image color. Filter property is mainly used to set the visual effect to the image. There are many property value exist to the filter function.

Can you transition a background image in CSS?

For the transition background image, we use the transition property in CSS and add a transition to the background image in the style tag. transition: background-image 3s; In the script section, we create an image object and add the source of the image that needs to be transitioned.


1 Answers

Like this?

@keyframes toColor {
    0%    { -webkit-filter: grayscale(100%); filter: grayscale(100%); }
    25%   { -webkit-filter: grayscale(75%); filter: grayscale(75%); }
    50%   { -webkit-filter: grayscale(50%); filter: grayscale(50%); }
    75%   { -webkit-filter: grayscale(25%); filter: grayscale(25%); }
    100%  { -webkit-filter: grayscale(0%); filter: grayscale(0%); }
}


img.desaturate { 
   animation: toColor 10s;
}
<img src="http://i.imgur.com/gMPdDHk.jpg" alt="Lena Söderberg" style="width: 300px;" class="desaturate">
like image 191
Randy Stiven Valentín Avatar answered Sep 28 '22 06:09

Randy Stiven Valentín