Blurry Background Effect

* 12/22/2008  —  67 Comments *

by: Chris Coyier

When setting a background image on a page element with CSS, you can control its position with “background-position”, but an often-forgotten trick is that you can control its position behavior with “background-attachment”.

Utilizing two images, we can pull off a pretty simple and fun CSS trick I’m calling the “blurry background effect”. Here are the results:

View DemoDownload Files

 

What you can’t see in that image, is that when you move the browser window wider and narrower, that white main content area stays centered while the colored pencils stay fixed. But anywhere the white main content area covers the pencils, they appear faded and blurry, like looking through frosted glass.

The Trick

The trick is that we are using two different background images. The full, sharp bright color version is set on the body element, while the faded blurry image is set on the main content div wrap. In order to keep them positioned exactly the same in that fixed position, is to set the background-attachment property on the div to “fixed”. Here is the relevent CSS:

body	 {
  background: url(images/bg-solid.jpg) no-repeat;
}

#page-wrap {
  background: url(images/bg-blurry.jpg) no-repeat fixed;
  width: 500px; margin: 40px auto;
}

Sometimes the simplest tricks have the the neatest results!

I was first shown this trick by Roy who pointed me to this website.

Responses

  1. Wow, what an awesome technique! I love the fact that it’s so simple and yet produces such a “complex” and interesting effect. Granted it’s simple, but I’d never think of it by myself, not in a million years!

  2. v-render says:

    wo hoo .. that was really awesome ..
    and i am luck to find here first !

  3. v-render says:

    o.. just missed first place ! :P

  4. I love simple techniques like this used to great effect. Thanks for posting this, Chris!

  5. Jason says:

    Just a thought, but could you also make your bg-blurry.jpg a semi-transparent white gif? That way it would work on any image.

  6. xbakesx says:

    The Compex Spiral is the first example I ever saw of something like this. It’s a pretty slick way of making such a cool effect.

  7. Evan Riley says:

    Wow sweet technique

  8. Elemental says:

    Hi There,

    thanks for the article, its a nifty, awesome CSS effect ::) Much appreciated.

  9. Is it me or does this technique break when you have to scroll down? Too bad, cuz it’s pretty sweet.

    • Chris Coyier says:

      Ah yes, good catch, I didn’t have enough content in there for scrolling. What you could do is just add the fixed position to the body background as well, which will keep them together.

  10. Danny Murphy says:

    This is great. Thanks again Chris for your wonderful tips!
    Merry Christmas

  11. Chris Morris says:

    Thanks for the article, interesting stuff. Cheers!

  12. Eric Meyer says:

    Looks like a riff on Complex Spiral Distorted (published 23 Feb 2002), which fixes both backgrounds in order to handle content scrolling. I wonder if that’s where the Bomb Squad folks got the idea.

  13. erickaweb says:

    Great Christmas present, thanks!

  14. I love this effect. There’s a lot of potential here for easter eggs and hidden surprises on your site. Must experiment some more.

  15. How will this effect IE6? IE6 normally isn’t to happy about fixed elements, although its not something I do have alot of experience with, fixed position that is.

  16. Page Gardens says:

    A great effect, but I wish I could figure out how to achieve this with code (not the CSS directional blur) so separate images wouldn’t be required.

  17. Logan Leger says:

    You can do something similar to this with straight CSS:

    filter: alpha(opacity=75);
    -moz-opacity: .75;
    opacity: .75;

    It doesn’t “blur” the background, but it will make it transparent. It works in most major browsers, too. It’s not quite the same effect, but it allows for a non-image design. You can see a live example here: 175amonthman.com. Good tip; thanks!

  18. Yustian says:

    Thanks. This is very usefull tricks.

  19. Player says:

    wow! thanks for that simple and great trick! =)

  20. POPOEVER says:

    Looks have some problem with Opera 10 alpha, scroll down to the bottom and take a look :P

  21. trace says:

    very cool and timely
    thanks man
    /trace

  22. So simple but so effective, thanks for this mate :)

  23. Very nice! Thank you

  24. cpg says:

    This example is stupid because it doesn’t work on ie6 and POPOEVER is right because it doesn’t work when you try to scroll down with Opera or Firefox.
    Try again dude…

    • yamaniac says:

      Dude! This site is called “CSS-TRICKS”… Chris here is showing us an interesting trick that we never knew about. now if you’re still using a browser thats Ie.6? . I suggest you upgrade. And anyways we all here at css tricks have completely freed ourselves from IE’s crappy browser. And if you think this neat trick is stupid? I suggest you make one yourself and show it to us.

  25. kiran says:

    Good technique…Thanks

  26. eren from Turkiye says:

    i thought that is a bit diffucult but when i saw its trick i realize that it is nothing not to understand := )

  27. louis says:

    When IE6 is still the most used browser I can’t see how this effect is useful to anyone unless they want to exclude IE6 users. This does NOT work in IE6.

  28. Bernhard H. says:

    While ago I did something similar but to simulate blurry background for dragdrop “windows” animated by JS. Check this out: Demo

    • edis says:

      Wow, looks really cool, how did you do that, i mean the drag and drop?

      • Bernhard H. says:

        You have to save the offset between box and coursor onmousedown and then onmousemove calculate the position of the box with that offset applyed to the mouse position. To get the blurry effect you use basicly the same method as described here but with an css trick called CSS sprites calculated by JS as seen in the function dotheMoves. (Take a look at the source)

        On mouseup you have to unregister your moving function and done.
        (You can not only save the position offset but also border color etc. if you want to change that while moving)

  29. Kazar says:

    very nice !
    I follow your website by feed .

  30. It’s a nice effect but like Eric and xbakesx said it has been done before, a long time ago. I was intrigued by that Complex Spiral example, when I saw it back when I was pretty new to CSS.

  31. Luiz says:

    Nice effect.

    However your demo page doesn’t work on Google Chrome.

    The other link works great in Chrome.

    The background attachment fixed in the body tag should fix it.

  32. This looks great, the IE fix is just an htc that tells it how to handle png files. I will try it out and see how it works with my site as well.

  33. Paul says:

    Nice! will have to try it out

  34. Dave says:

    Dude… you get absolutely no love for that. What a lousy trick! Multiple backgorund images, plus, try making that demo page really short and scrolling. Full of holes.

  35. Webdesign says:

    Thanks for sharing the whole thingy.