I'm Dudley Storey, the author of Pro CSS3 Animation. This is my blog, where I talk about web design and development with HTML, CSS and SVG. To receive more information, including news, updates, and tips, you should follow me on Twitter or add me on Google+.

my books

Pro CSS3 Animation, Apress, 2013

Using SVG with CSS3 and HTML5, O'Reilly, 2017

my other blogs

Massive Head Canon: Intelligent discussion of movies, books, games, and technology.

my projects

The New Defaults — A Sass color keyword system for designers. Replaces CSS defaults with improved hues and more memorable, relevant color names.

CSSslidy — an auto-generated #RWD image slider. 3.8K of JS, no JQuery.

Animating CSS3 Image Filters

Transitioning greyscale photos to color, blurred to sharp, all in CSS, including browser fallbacks.

In recent articles I’ve shown how to create cross-browser image filter effects with CSS and SVG: converting color photographs to black and white and sepia-tone, as well as blurring them. The next obvious step is to animate these effects.

Right now, these effects can be fully transitioned only in the most recent builds of Chrome; other browsers will show a “flick” between two filtered states, with no animation. As a rule, I don’t show web development techniques until they are completely duplicable in at least two different browsers, but in this case, the effects are so neat (and expected to be fully supported in all browser versions so soon) that I’m making an exception.

You can see how your browser performs with animated filter effects in the example at the top of this article (a variation on my earlier “Animated Card Fan” effect, with individual transitions on each card when it is hovered over).

Greyscale to Color Image Transition

An obvious use of an animated filter is to transition an image from greyscale to color on mouseover, an ideal effect for a portfolio or image gallery.

The code is surprisingly simple: first, we set up the image:

  1. <img src=roma.jpg alt=Roma id=roma class=colorshift>

Then turn it into greyscale, using the technique I discussed in the earlier article:

  1. img#roma {
  2. -webkit-filter: grayscale(1);
  3. -webkit-filter: grayscale(100%); -moz-filter: grayscale(100%);
  4. -ms-filter: grayscale(100%); -o-filter: grayscale(100%);
  5. filter: url(desaturate.svg#greyscale);
  6. filter: gray; filter: grayscale(100%); 
  7. }

To transition the effect, we must set the greyscale filter to the opposite value for CSS3, and to none for other browsers:

  1. img#roma:hover {
  2. -webkit-filter: grayscale(0);
  3. -webkit-filter: grayscale(0); -moz-filter: grayscale(0);
  4. -ms-filter: grayscale(0); -o-filter: grayscale(0);
  5. filter: none; filter: grayscale(0); 
  6. }

To transition this effect smoothly, we must add CSS3. Since I’m doing this to several images, I’ll make it a class:

  1. img.colorshift {
  2. -webkit-transition: 1.2s all ease-in; -o-transition: 1.2s all ease-in;
  3. -moz-transition: 1.2s all ease-in; transition: 1.2s all ease-in;
  4. }

In Chrome, you’ll find that the black and white image transitions into color, in other current browsers, the change happens instantaneously.

Sharpening A Blurry Image With CSS3 Filters

Transitioning a blur takes very much the same approach: I’ve covered the “off” setting in the previous article, so I won’t repeat it here.

Sepia Tone To Color Image

Colorizing a sepiatone image takes much the same approach as greyscale-to-color. The markup:

  1. <img src=bike.jpg alt=”Bike on a Roman street” id=bike class=colorshift>

The setup CSS:

  1. img#bike {
  2. -webkit-filter: sepia(100%); -moz-filter: sepia(100%);
  3. -ms-filter: sepia(100%); -o-filter: sepia(100%);
  4. filter: url(sepia.svg#sepia); -webkit-filter: sepia(1);
  5. background-color: #5E2612; filter: alpha(opacity = 50);
  6. filter: sepia(100%); zoom:1;
  7. }

The hover state CSS:

  1. img#bike:hover {
  2. -webkit-filter: sepia(0); -moz-filter: sepia(0);
  3. -ms-filter: sepia(0); -o-filter: sepia(0); -webkit-filter: sepia(1);
  4. filter: alpha(opacity = 100); filter: sepia(0); zoom:1; filter: none;
  5. }

This site helps millions of visitors while remaining ad-free. For less than the price of a cup of coffee, you can help pay for bandwidth and server costs while encouraging further articles.