Is there a simple way to display a color bitmap in grayscale with just HTML/CSS
?
It doesn't need to be IE-compatible (and I imagine it won't be) -- if it works in FF3 and/or Sf3, that's good enough for me.
I know I can do it with both SVG
and Canvas, but that seems like a lot of work right now.
Is there a truly lazy person's way to do this?
Simplest way to achieve grayscale with CSS exclusively is via the
filter
property.The property is still not fully supported and still requires the
-webkit-filter
property for support across all browsers.For people who are asking about the ignored IE10+ support in other answers, checkout this piece of CSS:
Applied on this markup:
For more demos, checkout IE testdrive's CSS3 Graphics section and this old IE blog http://blogs.msdn.com/b/ie/archive/2011/10/14/svg-filter-effects-in-ie10.aspx
Here's a mixin for LESS that will let you choose any opacity. Fill in the variables yourself for plain CSS at different percentages.
Neat hint here, it uses the saturate type for the matrix so you don't need to do anything fancy to change the percentage.
Then use it:
You don't need use so many prefixes for full use, because if you choose prefix for old firefox, you don't need use prefix for new firefox.
So for full use, enough use this code:
support for native CSS filters in webkit has been added from the current version 19.0.1084.46
so -webkit-filter: grayscale(1) will work and which is easier than SVG approach for webkit...
In Internet Explorer use the filter property.
In webkit and Firefox there is currently no way to desatuarte an image solely with CSS. so you will need to use either canvas or SVG for a client side solution.
But I think using SVG is more elegant. check out my blog post for the SVG solution that works for both Firefox and webkit: http://webdev.brillout.com/2010/10/desaturate-image-without-javascript.html
And strictly speaking since SVG is HTML the solution is pure html+css :-)