Advertisement
  1. Web Design
  2. HTML/CSS
  3. CSS

Say Hello to Webkit Filters

Scroll to top
5 min read

Earlier this month, a new specification, Filter Effects 1.0, was released. It presents some exciting new Photoshop-like effects that we can use in the browser. Even better, Webkit has already landed support (in the nightlies)!


According to the Spec...

"A filter effect is a graphical operation that is applied to an element as it is drawn into the document. It is an image-based effect, in that it takes zero or more images as input, a number of parameters specific to the effect, and then produces an image as output."

Now, at least at this point, I wouldn't presume to be able to show you everything that's possible with these new filters. I'm still learning them myself! That said, I'll show you a handful of the new filters, how we can use them in our projects, and then, hopefully, we can all brainstorm and learn from each other within the comments. Let's get started.

Filters are typically associated with images (though they can also be applied to video). As such, for the handful of demos below, we'll be using the Nettuts+ logo as input.

1
2
 <img src="https://cdn.tutsplus.com/net/uploads/legacy/nt-logo.jpg" data-original-url="https://cdn.tutsplus.com/net/uploads/legacy/nt-logo.jpg" data-original-url="http://nettuts.s3.amazonaws.com/nt-logo.jpg" alt="Nettuts+ Logo">
Nettuts+ Logo

Remember: these effects aren't yet available in the public releases of Webkit browsers. For now, download Canary when testing these demos.


hue-rotate

Ever played around with the Hue/Saturation panel in Photoshop? Well now you can play around with it in the browser.

1
2
img {
3
   -webkit-filter: hue-rotate(50deg);
4
}

If specifying this value in degrees seems confusing, just imagine a color wheel. The number of degrees you specify determines where that wheel stops. This means, that 0deg won't do a thing, while 50deg will turn the dial, accordingly.

In this case, the Nettuts+ logo will take on a blu-ish hue.

Or, let's say that you want your image to continuously change colors. Likely, in a real-world project, the color transitions will be far more subtle, but for this demo, we'll be a bit obnoxious.

1
2
img {
3
   -webkit-animation: adjustHue 1s alternate infinite;
4
}
5
    
6
@-webkit-keyframes adjustHue {
7
   0% { -webkit-filter: hue-rotate(30deg); }
8
   50% { -webkit-filter: hue-rotate(60deg); }
9
   100% { -webkit-filter: hue-rotate(90deg); }   
10
}

Simple enough. View Demo


grayscale

We've used a variety of hacks in the past to transition an image from black and white to color in the browser. One technique calls for two images stacked on top of one another. Another option is to use canvas. Or... we can use the grayscale filter.

1
2
img {
3
   -webkit-filter: grayscale(100%);
4
}

When applying a percentage to the grayscale function, just think to yourself, "On a scale of 0 to 100%, how gray do I want this image to be?

When used in tandem with CSS3 transitions, we can apply a nice and clean hover effect.

1
2
img {
3
  -webkit-transition: -webkit-filter 1s;
4
}
5
img:hover {
6
    -webkit-filter: grayscale(100%); 
7
}

In the future, you'll want to provide prefixes for the other browsers, however, it's not necessary at this point. No need in applying Mozilla transitions to accomodate for a filter that's only implemented in Webkit (so far).

View Demo


sepia

Enjoy the sepia-flavored Instagram effect? Let's see what Nettuts+ looked like in the old west.

1
2
img {
3
      -webkit-filter: sepia(100%);
4
}

Typically, though, this effect is applied to photos. Let's see how the greatest artist who ever lived looks in sepia.

Excellent.


blur

By passing a radius, we can blur an image in the browser with ease.

1
2
img {
3
      -webkit-filter: blur(2px);
4
}

Or by upping the blur radius to 50px.


brightness

We use the brightness filter to specify...wait for it...how bright the input image should appear.

1
2
img {
3
      -webkit-filter: brightness(15%);
4
}

Think of 100% as home base. brightness(100%) keeps the image unchanged. As we reduce this percentage, however, the image will continue to darken.

Don't forget: you can combine all of these filters.

1
2
img {
3
      -webkit-filter: brightness(60%) sepia(100%);
4
}

contrast

We can now adjust the contrast of an image quite easily.

1
2
img {
3
      -webkit-filter: contrast(200%);
4
}

Once again, think of 100% as resting position. We can then reduce or increase this value to adjust the contrast of the image. According to the spec, applying a value of 0% should make the image 100% black, similar to what you might expect from -webkit-filter: brightness(0%);. However, I'm seeing more of a dark gray.

1
2
img {
3
      -webkit-filter: contrast(0%);
4
}

Now if we up the percentage considerably, to 2000%:

1
2
img {
3
      -webkit-filter: contrast(2000%);
4
}

Just for fun, let's create a throbbing Matrix version of the Nettuts+ logo. We'll combine CSS3 animations and filters.

1
2
img {
3
    -webkit-animation: bluePill 1s alternate infinite;
4
}
5
    
6
@-webkit-keyframes bluePill {
7
   0% { -webkit-filter: contrast(2000%); }
8
   100% { -webkit-filter: contrast(100%); }
9
}

View Demo


invert

Mac users: press Control + Option + Command + 8. Notice how it inverts your screen (of course you noticed). I use this trick late at night when I'm reading on the computer, and my eyes are sore.

By applying a percentage of 100 to the new invert filter, we can achieve the exact same effect.

1
2
img {
3
      -webkit-filter: invert(100%);
4
}

Note that 0% will leave the image unchanged.

Now, you could technically apply this to, say, the body of your website, and it would work. However, you'll notice considerable slow down, and lose the ability to scroll the page. AKA - Don't do it, except for fun.


saturate

In addition to setting grayscale(100%), we could also achieve a similar effect by desaturating the image entirely.

In this case, 100% is the unchanged state, at which point you can either decrease or increase this value. As such, reducing this value to 0% should remove all color from the image.

1
2
img {
3
      -webkit-filter: saturate(0%);
4
}

Or, by upping the value to 700%:


That's All For Now

Stay tuned to this article over the course of the next week. As these techniques are still super new, we all need time to figure out how to use them. I'll update this article as I learn more!

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.