
Update: I've extended my blog post into a full GitHub project, with JavaScript polyfill that switches out <img> tags with the SVG goodness that IE 10 and 11 need: https://github.com/kununu-labs/gray
To apply a grayscale effect (color desaturation) to images across Firefox 10+, Firefox on Android, Internet Explorer 6 - 9, Chrome 19+ and Safari 6+ on desktop and iOS, use the following snippet below.
To disable the effect again, apply:
Check out a demo, which includes fading back to color on hover, Internet Explorer 10 support with inline SVG, and partial grayscale.
Note: If you're experiencing issues with blurry images in Firefox, you can try playing with the filterRes attribute, but this will tie your image resolution to the svg code.
Thanks, the svg into the css code helpme to work fine in FF!
ReplyDeletedoesn't work in chrome :(
ReplyDeleteDo you see the grayscale image in the fiddle? http://jsfiddle.net/KDtAX/8/ What operating system and version of Chrome do you have? It's worked on every Chrome 20 on Mac and Windows that I've tested.
DeleteThank you.. This works in FF and other browser!.. :-)
ReplyDeleteGlad to help!
DeleteGracias, era lo que estaba buscando.
ReplyDeleteNo hay problema!
DeleteThis is terrific and such a 'simple' solution! Thanks to you for your effort.
ReplyDeleteBeautiful :)
ReplyDeletethank you very much! nice!!
ReplyDeletetenkyu!!!
ReplyDeletework on Firefox :D
thanks
thanks
thanks
I tried it on my tumblr blog but somehow it doesn't work with the photosets anyone know why?
ReplyDeleteDo you have an example URL?
Deletehere : http:// my-biases.tumblr.com
DeleteThank you =)
Seems like photosets in tumblr use iframes, which means that it's a bit more effort to style the images with CSS. Try something like this: https://groups.google.com/forum/?fromgroups#!topic/tumblr-themes/SR99h0yLqZg%5B1-25%5D
Deleteis there a code for images on your website that you don't want to be made monochrome?
ReplyDeleteNot entirely sure what you mean. If you don't want the effect, don't apply it. If you wanted to selectively apply it to some images and not others, you could use a class for the effect.
Delete"If you wanted to selectively apply it to some images and not others.." <- that's exactly what i want. what is a class ;_;
DeleteA class is a type of CSS selector: http://www.cssbasics.com/chapter_3_css_class.html
Deleteoh ok well what i want is to make it not work on 2 images. is there possibly a class for that? and thank you for putting up with my stupidity x.x~
DeleteFrom the jsFiddle example at http://jsfiddle.net/jXV3r/2/ , there is a class called "color" that would work.
Deleteit doesn't D:
DeleteDo you have an example URL?
DeleteExactly the solution I've been looking for. Appreciate for posting this tut. Thanks.
ReplyDeleteAwesome stuff! Simple and Clean! Thanks for the solution!
ReplyDeleteI have a problem when I want my web page print. The gray pictures are not printed
ReplyDeleteThe Firefox rule (filter: url("data:image...) seems to break printing in Firefox and Chrome. If you need printing you may want to try a JavaScript solution.
DeleteAny idea how to make the smooth fading transition work in Firefox (15+). It looks o nice in Chrome and Safari
ReplyDeleteIf you don't mind a bit of extra styling and adding the image as a background to your css, then you could try a ::before or ::after pseudoelement with opacity for the hover effect. The demo link above has something similar in the IE10 SVG section, although you'll need to make the pseudoelements yourself.
DeleteThanks Very Useful Code :)
ReplyDeleteWorks Very Well :)
Thanks a lot! was looking for this solution for a long time. I used to create image sprites to get this effect, but this is far more flexible!
ReplyDeleteI luv ya!
ReplyDeleteThanks so much mate
This is soooo greeaat!!
ReplyDeletethanks a lot!
Hi! Thats what im lookong for! But..
ReplyDeleteIm a total noob if it comes to coding. Im trying to make a site in Dw, and i wold like to add this color on hover to the ClearBox gallery thumbnails in css. But no idea where exactly should i paste the code..
There are some effects fot thumbnails lika a alpha:opacity but I dont know how to add the efect You created to this rule.
my code:
http://freetexthost.com/h04bz2scwi
You would want to add the img.grayscale rules to your .item a img (within the curly braces, directly under opacity: 1). The img.grayscale.disabled rules should be added to your .item a:hover img, .item a:active img block at the bottom as well.
DeleteTake a look at the link below to get an idea of the basics of how CSS is written.
http://www.cssbasics.com/css-syntax/
where I have to put the code?
ReplyDeleteThanks for sharing! It work well! Nice solution!
ReplyDelete#win dude! great snippet
ReplyDeleteHey Karl, I've got a question. Can I apply different levels of greyscale (say, 50%)? It's simple with webkit but I don't know how with the others.
ReplyDeleteNow with 50% Grayscale (at the bottom)
DeleteLove it, thank you from belgium !
ReplyDeleteHey thanks a lot I've used it here: www.igconstruction.pl
ReplyDeletePretty good indeed!
ReplyDeleteDoes this work on iOS Safari though? i've tried it on iphone 4, iOS 5.1.1 but it ain't working.
Thank you anyway! :)
It works on iOS 6+.
Deletelittle update, i checked caniuse.com and found this, http://caniuse.com/#search=filter, >iOS6 has partial support for filters, still i cant find info on how to implement this
Deleteups didn't see your reply, yea full support only for ios6+, still caniuse tells there is partial support svg effects for html, how can those be implemented? thank you for your answer!
DeleteMy demo above uses SVG for the IE10 version, although this may fall under SVG Filters instead. Does the IE10 part of the demo work for you? I don't have an iOS device at the moment to test on.
DeleteI've checked your demo on jsfiddle on my iphone and it doesn't work atm, all images are with colors.
DeleteHm, yeah then I'm not sure that a CSS grayscale is possible or desirable under iOS < 6. The CanIUse notes of the SVG effects for HTML indicates that it will be replaced by CSS filters.
DeleteIf you need support for older versions of iOS then JavaScript or a server-side grayscale look like your best options.
mmm i see i guess i'll try with js then, thanks for help cheers!
DeleteHI
ReplyDeleteI just tested this code (which works in general good for me) with Windows 8 and IE10. In my case the gray scale suddenly does not apply to the images. On all other browsers incl . IE9 it works great.
Any idea with regards to IE10?
Thanks
Kai
IE10 doesn't support CSS filters yet. If you take a look at the second example in the demo, you'll see an SVG method you can use for IE10, but I'm not sure if I would recommend it. It's a lot of code for a simple effect.
DeleteIf you want IE10 support, you're probably best off going with a JavaScript or server-side solution instead.
Thanks for Your Tip, The demo works like magic. But My problem is that I am using Wordpress and Its hard for me to achieve this. Any Help that will point me in the right direction, will be deeply appreciated. Thank You.
ReplyDeleteI need to learn to thank people from the first time i'm borrowing the code, not like the fifth :-) Thank you brother!
ReplyDeleteThanks man, elegant solution
ReplyDeleteYou are the master of magic. You saved my day. Thank you sooooo much!!!
ReplyDeleteThanks! Kudos from Brazil =)
ReplyDeleteGreat! It works in Chrome, FF and IE.
ReplyDeleteThanks. Gracias.
Can someone confirm why is image moving in Firefox 17.0.1 (Windows 8) on hover and its a little blurry.
ReplyDeleteThose are side effects of the svg filter being applied to the image. I updated my post - the issue with the image moving is fixed in the latest demo above.
DeleteI tried your demo with 17.0.1 (Win 7) but the problem is still here.
DeleteA strange issue is that I experience the problem only in some browser resolutions. E.g. in fullscreen the problem appears, when I resize the page in some resolutions the images are displayed correctly.
Can you make a video of the effect so that I can be sure that I'm seeing the same thing that you're describing?
DeleteI think I have reproduced and fixed the issue with the blurriness now. Take a look at my comment below.
DeleteIt worked thanks!
ReplyDeletehello
ReplyDeleteI have the issue too, the image becomes blurry in firefox for gray and in color, any fix for that?
Class MUST me applied to the IMAGE, don't apply to anchor or container tag
ReplyDeletesame here,
ReplyDeleteimage is blurry for me too , can you please provide fix for that?
See my comment below
Deleteworks just fine for me
ReplyDeletehaha I just grabbed this css code, and hours later, I came back to the open tab and realized I was looking at a giant school bus doing a wheely
ReplyDeleteHi, its awesome its working fitefox but for me its not working on chrome!..
ReplyDeleteWhich version of Chrome does it not work for you on? It's working in the latest dev versions (v25) for Windows and Mac for me.
DeleteWhat issue are you having? Can you post a screenshot and what you've tried so far?
ReplyDeleteIt's not working for me on Opera or Safari!
ReplyDeleteSafari only works in version 6+. Current versions of Opera do not work, but since Opera is switching to WebKit, this shouldn't be a problem for much longer.
DeleteAwesome dude! working on mac osx 10.8.2 Firefox 19.0.
ReplyDeleteThank you very much!
Is there a way to do this universally with sepia, exposure or blur?
ReplyDeleteGreat job :)
ReplyDeleteThanks!
ReplyDeleteEmbeding svg in css works for me in FF!
Image becomes blurry for me when grayscaled in Firefox. And as a guy mentioned earlier, if i resize my browser a bit smaller then suddenly the image becomes clear. Very strange
ReplyDeleteAnyone knows of a solution?
Finally was able to reproduce the bug today and found the solution. It appears the filterRes attribute needs to be set to approximately twice the resolution of the image.
DeleteMade a fiddle here: http://jsfiddle.net/KDtAX/787/
A couple caveats: I'm not sure this is a recommended solution for everyone because it adds complexity and image dimension dependence to the CSS. Also, I haven't had a chance to test this yet on Firefox on Mac.
I tested this to be working on Firefox Mac. So the only issue I can see is that it makes your SVG code dependent on the size of your images.
DeleteIf you run that CSS using Yui compressor (twitter-recess seems to use it aswell) you get a wrong filter value for the firefox filter, it seems that it removes the white-spaces IE "<svg xmlns=" gets minified to "<svgxmlns=", any idea how to fix that?
ReplyDeleteThis looks like a bug in YUI Compressor.
DeleteThe YUI Team has decided to deprecate YUI Compressor in favour of the new yuglify project.
Your best options would be to either look for a new css minifier for the project or switch to a version of YUI Compressor that doesn't have this problem (this issue would affect other code with data uris too, not just my code).
DeleteIf you don't want to switch your minifier, you can create a separate svg file and then use the path to this file as the url instead. Be aware that this creates a slight performance hit. Solution here.
Thanks,
ReplyDeleteit's works fine for me
Any idea how to get this working for a specific image in Wordpress? Thanks.
ReplyDeleteHi, this is great. It doesn't seem to work on my Samsung Galaxy 3 browser. Any idea why?
ReplyDeleteDoes not work on Firefox 19.0.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis is awesome. i used this code many times. thanks :)
ReplyDelete#awesome tutorial!
ReplyDeleteWorks great, thank you, but is there a way to stop image quality degradation?
ReplyDeleteDo you mean the blurriness in Firefox?
Delete