Say Hello to Webkit Filters

Say Hello to Webkit Filters

Tutorial Details

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.

 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.

img {
   -webkit-filter: hue-rotate(50deg);
}

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.

img {
   -webkit-animation: adjustHue 1s alternate infinite;
}
    
@-webkit-keyframes adjustHue {
   0% { -webkit-filter: hue-rotate(30deg); }
   50% { -webkit-filter: hue-rotate(60deg); }
   100% { -webkit-filter: hue-rotate(90deg); }   
}

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.

img {
   -webkit-filter: grayscale(100%);
}

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.

img {
  -webkit-transition: -webkit-filter 1s;
}
img:hover {
    -webkit-filter: grayscale(100%); 
}

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.

img {
      -webkit-filter: sepia(100%);
}

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.

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

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.

img {
      -webkit-filter: brightness(15%);
}

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.

img {
      -webkit-filter: brightness(60%) sepia(100%);
}

contrast

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

img {
      -webkit-filter: contrast(200%);
}

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.

img {
      -webkit-filter: contrast(0%);
}

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

img {
      -webkit-filter: contrast(2000%);
}

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

img {
    -webkit-animation: bluePill 1s alternate infinite;
}
    
@-webkit-keyframes bluePill {
   0% { -webkit-filter: contrast(2000%); }
   100% { -webkit-filter: contrast(100%); }
}

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.

img {
      -webkit-filter: invert(100%);
}

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.

img {
      -webkit-filter: saturate(0%);
}

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!

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://www.nouveller.com/ Benjamin Reid

    $respect–;

    Post thumbnail, not cool.

    • http://rommelcastro.me Rommel

      i think so!!! use something better for thumb

      • Ethan Kramer

        agreed

    • http://baylorrae.com Baylor Rae’

      I think you meant.

      –$respect;

      https://ideone.com/Vjkc0


      I think this is awesome. I just hope people don’t go overboard and make image galleries obnoxious with blur(2px) + contrast(20) + invert(50%) and a rotation transition.

    • http://www.jeffrey-way.com Jeffrey Way
      Author

      Guys – I thought we could all enjoy the photo…considering how it’s Christmas time.

      • http://habanized.com/ habanized

        LOL Jeffrey. You’ve gone and done it again! (And people are still falling for it! Amazing.)

      • tigerlabs

        LMAO, Jeffrey, you’re up to your old tricks again.

        I laughed when I saw the photo, haha!

        Let’s see how the greatest artist who ever lived looks in sepia.

        ROFL, classic!

    • http://affanruslan.com Affan Ruslan

      +1

    • http://bensheedy.com Ben

      it’s ok. It looks like there an issue on Nettuts at the moment, so the thumbnail’s not showing. That’s one less Beiber image in the world.

      • http://bensheedy.com Ben

        Well.. it’s fixed now.

        1 more Beiber picture in the world.

      • http://www.jeffrey-way.com Jeffrey Way
        Author

        haha

    • jack

      ewwww justin bieber? what is this website

  • http://edwinhollen.me edwinhollen

    Fantastic! I cannot wait for these to be supported in a stable update. Absolutely phenomenal tutorial, Jeffery.

  • http://www.bramjetten.nl Bram Jetten

    Thumbnail really not cool man.

    • http://rikdevos.com/ Rik de Vos

      +1 Why not blur out his ugly face using one of these new webkit filters :-)

    • http://www.jeffrey-way.com Jeffrey Way
      Author

      We at Nettuts+ don’t take kindly to Bieber’s name being disgraced.

      • Blip

        The world is going gay :P…..
        But I love Canary… nightly excitements!

      • w1sh

        Cool homophobia bro!

  • Dimitris Bor

    That’s so cool!
    Thanks Jeff

  • Enrique Moreno

    +1 for the article

    -5 for “Let’s see how my favorite artist looks in sepia”

    • Ethan Kramer

      I agree

  • Paul

    Thumbnail is so legit. What version of Chrome/Safari supports filters?

    • http://thomasbower.net Thomas Bower

      Hi Paul. Currently, the filter property (with the -webkit- vendor prefix) can be used successfully in Google Chrome Canary 18.0.976.0 (I believe that is the version, but it’s already been updated to 18.0.980.0 so you should be good to go if you just update it to the latest version. Additionally, it works in the latest Webkit nightly.

  • Paul

    Thumbnail is so legit. What version of Chrome/Safari supports filters?

  • http://www.cirkut.net/ Josh Allen

    “But, strangely, if we cut off the percentage, and apply a value of 20 (which I don’t think we’re supposed to do), we get the exact same result. That’s a bit odd.”

    Imaging 100% being 1.0 (or as it is in a lot of other CSS values, such as opacity and rgba).

    100% = 1.0,
    200% = 2.0,
    1000% = 10.0,
    2000% = 20.0.

    So this filter actually isn’t odd at all by doing that, it makes perfect sense. I would assume you can do the same to the rest of these filters.

    • http://www.cirkut.net/ Josh Allen

      Anyways, great tutorial Jeffrey, I look forward to this becoming more developed and put into all the modern browsers, however I’m not looking forward to the websites that abuse these filters.

    • http://www.jeffrey-way.com Jeffrey Way
      Author

      Yeah that makes sense. I was just surprised that it accepted any non percentage value at all. :)

  • Tom Conway

    I guess Bieber fever has not infected Nettuts readers, haha.

    On topic, these filters could be really exciting. It would be nice not to have to produce 2 images (one in color and one in b&w) for rollover effects.

  • Jelmar Bosscher

    Been wandering around on this site for two days now, been amazed what it has brought me compared to what school brought me in 2 years.

    I am thinking of getting tuts+ premium, but I will be enjoying this site until then.
    And a bit of this Justin Bieber.

    Thank you

  • gdfgdf

    why did you choose bieber ? I can’t read this…

    • http://www.wordimpressed.com Devin Walker

      That’s what I’m sayin’… I’m not a belieber

    • http://www.jeffrey-way.com Jeffrey Way
      Author

      Because he’s the greatest artist of both his generation and ours.

    • kankaro

      Mr. Way is a big fan of Justin Beiber…. that’s how I think…

      • http://net.tutsplus.com Jeffrey Way
        Author

        Because I couldn’t possibly be joking…

      • Craig

        When you look a bit of a douchebag to be fair.

  • http://tagthatphoto.com Sean Thompson

    This is so cool!!!

  • Bob Johnson

    Oh sweet, we’re going back to Internet Explorer filters. Almost all of these existed for IE 10 years ago.

  • Ondrej

    Please, be so kind, change that picture of that uhm.

  • http://www.tucoaster.com Jesus

    Photoshop CS5 soon will become to Photoshop CSS lol

  • Wouter J

    Who is saying IE was and is bad? IE5.5 and higher already supported these things: http://msdn.microsoft.com/en-us/library/ms532847(v=vs.85).aspx

    Almost everybody is saying IE is bad. But the other browser are more or less bad. IE has features far before FF or Chrome it has (and yes, Chrome and FF has some features before IE…).
    IE became with the .innerHTML propertie in JS. FF is saying, ah IE has that method so it’s not good we make another method. 15 years later FF is thinking, oh no that innerHTML propertie is really good lets add it into our browser.

    But it’s a nice article, good job! (I secreatly hoped it was a video tutorail (!))

    • Ian

      Wow, I just checked this page in IE 6 and all of the filters are working. lol The only thing not working is the nettuts page (slightly off posisitons on the comments and no reply button). But it is confusing to me how things like this have been working in IE but all the regular things used day to day for web design are always a pain with IE.

      I don’t know enough to argue either way. I just know like everyone else, I’ve been having to learn hacks and stuff to get things to work right in IE and then when it comes to something like this that’s cool but really not important, they are ahead of the game with it. Why wasn’t IE turning out tutorials on this stuff if they had the capability so long ago?

      • Ian

        never mind, I thought the images on this tut were actually using the filters so I thought IE was working. Neither IE or firefox are working with the demos.

      • http://www.promonorge.no Stian Jacobsen

        The fact is; IE was, and to some degree, still is in the front line of exiting features on the web. The problem is that Microsoft likes to go their own way and don’t support the “standards”. But, IE is still bad. Both the fact that you need to “hack” your html/css and javascript to make it IE compatible and that it’s so unsafe for the end user. I like the fact the “normal” people have gotten their eyes open for other browsers than IE. I believe i read somewhere that Chrome has overtaken IE now?

      • Wouter J

        IE support another syntax for filters, see the documentation page I posted for the syntax IE used. The Webkit syntax is much better… :)

    • http://www.github.com/dotink Matthew J. Sahagian

      @Wouter J

      Actually, IE’s syntax was not drastically different. It appears much longer simply because IE’s filters were calling classed and namespaced methods and supported named parameters of a sort. The core syntax, however, is essentially the same: filter: function(arg1, arg2, …);

      Hypothetically we could create a filter called DXImageTransform_Microsoft_Blur() and things probably wouldn’t look drastically different.

  • readres

    if (document.getElementById(‘Webkit Filters’)) {
    document.getElementById(‘Webkit Filters ‘).innerHTML = ‘Hello Webkit Filters – this was inserted using sepia :d ‘;
    }

  • Joe

    Am I the only one that thinks that CSS is starting to do too much? It is going to be another issue just like styling mixed in HTML. Gotta keep ‘em separated…

    • http://www.github.com/dotink Matthew J. Sahagian

      @Joe — yes and no.

      I am actually quite OK with CSS doing this, my problem is just the lack of consistency. The main reason that “filter” seems to exist is for the ability to create custom filters using primitives. There is absolutely no reason to have “filter: blur(20%)” instead of “blur: 20%;” other than the fact that you can create “filter: custom();” — this, of course, will lead to absolute ridiculousness.

  • http://www.leandroberna.com/ LeandroBerna

    R U a “belieber”?! XD

  • Mecha

    Great. Just when I thought I might be able to get a grasp on the basics of css and html this comes along. I feel like this is the best and the worst time to become an aspiring developer. The best because there are so many tools at my disposal. The worst because there are so many tools at my disposal. I wish I had got into this when it was just working with the terminal and maybe my head would not ache from so much information :)):

  • kankaro

    Hopefully the other browsers supports this CSS property….

  • Alex Begg

    The text above the contrast code example, has “2000%” instead of “200%”

  • Saad Salman

    Awesome post! It’s going to my instapaper. On request though: Could you change the post thumb? Net tuts can not bear the “Musicianship” of bieber

  • Ian

    What browser does this work in? I installed safari on windows and it’s not working. After trying IE9 and firefox 9 I realized with it being webkit it shouldn’t work should it? I’m not really all that knowledgeable with webkit other than using it with radius and shadow for safari and then using -moz for firefox. So is this going to work with any browser other than safari on a mac right now?

    • http://twitter.com/stephensauceda Stephen

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

      • ian

        Thanks for the link Stephen.

  • http://www.flashminddesign.com Matt

    hahahaaha love the tutorial hate the sample picture used :) I am sure a lot have agreed.

  • http://www.ijasnijas.com/ IJas

    Seriously, for the first time when I came to know the article, I thought it’s something about beiber’s new album “Say Hello to Webkit Filters” or beiber’s about to talk web filters :D

    Let’s Fix Post Image,

    html body.single div#content_wrap div#content.clearfix div.page_wrap div#page.single_post div.page_inner div#post-23318.post div.tut_top div.post_image img
    {
    -webkit-filter: contrast(0%)!IMPORTANT;
    }

    :D

  • Patrick

    This is great! I’m sure there’s a lot that these new filters can accomplish but one thing that jumps out to me in particular is that once these get implemented into the new versions of browsers, we won’t have to create two or more separate images just to create a hover effect – we can simply create the same effect with filters!

    And about the Beiber picture, I think some people just need to take it easy. It is the holidays after all..

  • http://www.varusoft.com AdrianMaftei

    Looks interesting these filters, but until these will became a standard for all browsers will be hard to use.

  • Austin Condiff

    This article just made my day, no week, no year! I’ve been waiting for blur for a very long time!

  • Nelson

    Jeffrey, I didn’t know you had that taste.

    Cool effects though.

  • Abhijit

    Webkit is crazy! Unfortunately some others love to stay sane!

  • http://sirwan.me Sirwan

    Hi Jeff.

    This is cool, but does everyone on the web seriously need to upgrade to Canary just to see this effect?

  • w1sh

    “HAI JAFFRY WAY!1 WHY ARUNT YOU NOT PUTING LESS JASUTIN BAVER ON THIS!?! MY WIFE WHEEL SEE AND THEN NUT LOVE ME!!”

    NetTutsians never disappoint when it comes to you Bieber-trolling them. :)

    Thanks for the article. I’m genuinely excited about all this designer love browsers are showering on us. It will make everything so much easier and give us much more time to work on web apps that should be running the world by now.

    What was that one dark/metallic site with the dragon emblem logo that everyone was freaking out about because it had some fancy nav where you hovered and it went from monotone to some color with CSS sprites? Yeah. That thing. That one friggin’ effect that landed that site on the front of every design gallery will be accessible to anyone/everyone who can use a CSS generator (as one will surely pop up – Prefixr implementation I’m sure?).

    Naturally the newbs will run it into the ground, but for the people who can use it subtly, it will be great.

    I Beliebe in a thing called love! Juslisentotherhmofmhart!

  • Kicky

    Awesome. But…..the Beebs? That’s pretty funny right there….

  • Yahia M.

    I got trolled by NETtuts.

  • https://twitter.com/#!/iamssshaan Shaan

    Hey guys come on!!. focus on the tut man, that’s the important part and, it’s Jeff’s personal taste, and he can like whoever he wants, and we shouldn’t be going nuts on that. Just focus on the tuts guys, and share what you know, and wanna know…

  • http://mike.deleondesign.org/ Mike DeLeon

    Future grayscale and blur support! This made my night!

  • Frank

    So it is really interesting even if we have a photo of justin bieber as an example hahahahaha it shows a real powerfulll resource to use :) keep on studying Jeffrey !!!!!

  • http://www.github.com/dotink Matthew J. Sahagian

    *SIGH*….

    This is becoming, extremely disappointing. What a meaningless property. You might as well just do filter for everything….

    img {
    filter: rotate(-15deg);
    filter: border-radius(10px);
    filter: whatever-else(argument);
    }

    This is precisely what IE has used for a number of things which were not supported in some standard fashion previously…. and the only excuse for it is… so that browsers can implement completely custom functions that will not be standardized.

    Why do we use “transform” for rotate() and “filter” for blur()?

    On top of this, it also duplicates existing standardized functionality… filter has things like “drop-shadow” where you can set the offsets, and blur of a shadow on an element, sound familiar? It also has opacity()…

    Another MESS coming out of this ridiculous HTML5 hype nonsense. Who the hell is pushing this garbage?

    I’m not saying some of these effects aren’t useful… I’m saying they’re implementing them wrong, and/or perhaps correctly, but where the current methodology seems in contrast.

    Why not the following:

    img {
    hue-rotate: 50deg;
    }

    instead of

    img {
    filter: hue-rotate(50deg);
    }

    • AntoxaGray

      I hope it’s not final syntax, hate it too. Also I want to use more than one filter at once (how would they stack though?).

      • http://www.jeffrey-way.com Jeffrey Way
        Author

        Just add a space between the filters.

  • AntoxaGray

    Justin Bieber approves this article.

  • http://www.ruturaaj.com Ruturaaj

    This everything looks so cool… but at the end of the day, one term called “Browser Support” and “Browser Compatibility” may just push me away from using these filters. Can someone please tell me which Browsers are supporting or will support once officially released?

  • Sebastiaan

    Personally i’m hoping for a feature in Chrome that auto-detects Beiber pics and distorts / blurs them so much that i don’t have to look at it. That’s a feature that warrants a full version number change imho.

  • http://www.vision-ps.de Irfan

    Good work Jeff. i am waiting for your “Professional Screencasting” course. when it will be available ?

  • http://www.my-html-codes.com Matt

    I’m not sure I like the idea of putting power in the hands of the front end developer to create these effects with CSS. To have the ability to do this is one thing, to do it correctly is another. Also the fact that backwards compatibility will be a nightmare and you’ll have to create the same effect in photoshop/fireworks anyway I just don’t see the point. Just cause you can doesn’t mean you should. Should the w3c be concentrating on finishing off the current spec instead of pushing forward with new additions?

  • Paul

    left the site after seeing the thumbnail.

    • http://www.facebook.com/dvclmn Dave Coleman

      *and visiting the post and commenting.

  • Alhadis

    First of all, I vomit all over Justin Bieber and his tasteless fanbase.

    Second, while the premise of having Photoshop-like powers in CSS3 are bringing drool to my lips, the proposed syntax is less than appealing. Sahagian is right, what’s the point in using “filter:” as the property name when each of those effects are likely going to be applied separately?

    It’s also going to be a headache for managing numerous unrelated effects, particularly those that’re being scripted. Imagine changing one effect’s properties and having to copy+paste the other filter properties just to avoid overwriting them (assuming the proposed filter property works the same as it does in IE).

    I like Sahagian’s suggestion of using separate properties for the effects.

    hue: rotate(45deg); /* Increases hue +45 degrees, etc */
    hue: 0deg; /* Sets hue to red */
    blur: 1px 2px, 10px 1px; /* Array of blur effects; this one applying an effect similar to a motion blur */

    Trying to lump them all inside a single property is going to be messy, unintuitive, and a nightmare.

  • http://andersschmidt.com/ Anders Schmidt Hansen

    Great little article, despite the Bieberization. Aaaaaaaand if there are any LESS users out there, I’ve smacked a few of these filters together into one function: http://tinkerbin.com/1f9beRYo. Or grab it here:

    .filter(
    @blur: 1px,
    @grayscale: 1.0,
    @sepia: 100%,
    @opacity: 1
    ) {
    -webkit-filter:
    blur(@blur * 1px)
    grayscale(@grayscale)
    sepia(@sepia * 1%)
    opacity(@opacity)
    ;

    -moz-filter:
    blur(@blur * 1px)
    grayscale(@grayscale)
    sepia(@sepia * 1%)
    opacity(@opacity)
    ;

    filter:
    blur(@blur * 1px)
    grayscale(@grayscale)
    sepia(@sepia * 1%)
    opacity(@opacity)
    ;
    }

  • http://res.celseven.com/ Ryan

    Webkit and CSS3 should have a filter for Bieber’s face… god.

    Nice article, anyway :D

  • smfr

    Demo works with a WebKit nightly as well as a Chrome Canary. In fact, the filters work was done by Apple folks.

  • Max

    Beautiful Model, who is she?

    Oh! and nice tut, thanks Jeff… Whosoever said, you got no sense of humour needs to take an arrow to his knee… I was not really amused with CSS3 filters, I mean, practically, I do not see a corporate use.. still better love story than twilight…