Convert an image to grayscale using only CSS
Webkit supports filters (been quite some time actually) and it is possible to convert images to greyscale using only CSS. There are other options that use javascript. However, this simple approach works for me.
.desaturate{
transition-duration: 1000ms;
}
.desaturate.active{
filter: grayscale(100%);
-webkit-filter: grayscale(100%); -moz-filter: grayscale(100%);
-ms-filter: grayscale(100%); -o-filter: grayscale(100%);
filter: url(desaturate.svg#greyscale);
filter: gray;
-webkit-filter: grayscale(1);
}
I can then bind the mouseover event on an image to add the desaturate class to make it grayscale.
$('.img').bind('mouseenter', function() {
$(this).addClass('desaturate');
});
You can refer to this list for cross browser support
Written by Rizwan Iqbal
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Css
Authors
Filed Under
Did you know?
JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.
Security insights from Cside
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#