Grow your CSS skills. Land your dream job.

Receding Background Modal Boxes

Published by Chris Coyier

You all know Hakim El Hattab right? He creates some super crazy progressive demos over on his blog. His CodePen profile is full of amazing too.

One recent creation of his is Avgrund. It's a design pattern for dialog boxes in which the main page fades away and the modal box flies down from above (or up from below). The main page becomes smaller and blurry, making it seem further away ala depth of field in photography. The modal box sits on top, making it seem closer to you and clearly demand your attention. That's good, because the very purpose of modal boxes is to require a user to give you some input before they can do anything else.

avgrund

It feels pretty magical when you see and use it. Kinda makes you want to right-click and see if it's Flash. But it's not, and like many things on the web when you start digging in, the magic is just a nice combination of simple effects.

Let's look at them in order. Note: this isn't exactly how Avgrund works, it's just me reverse engineering it.

Step 1) Separate Page Markup and Modal Markup

All the content on the entire page should be contained within a wrapper div. The modal is outside of that wrapper.

<body>

  <div id="page-wrap">
    <!-- all page content -->
  </div>

  <div id="modal">
    <!-- modal box content -->
  </div>

</body>

How that markup gets there is up to you. If I was using this for real, I'd probably inject it dynamically when needed through a JavaScript thingy I create just for handling dialogs.

Step 2) State Based CSS

No need to get too fancy with JavaScript. If we think "state based", all we need is a class name on the body element and we can adjust all visual design as needed with that class. This is a larger concept that is useful in big ways and warrants further discussion (like how/where/why to trigger states), but let's just keep it simple here with a bit of jQuery:

// Something happens
$("button").on("click", function() {

  // State changes
  $("body").toggleClass("dialogIsOpen");

});

Step 3) Default State for Modal

The modal will be a fixed position box right in the middle of the screen. By default, it will be hidden (zero opacity) and unclickable (pointer-events). Let's just ignore browser support on that. If it's a big deal to you, you can hide it in any number of different other ways like positioning it off screen.

#modal {
  background: white;

  position: fixed;
  width: 50%;
  top: 50%;
  left: 50%;
  margin: -25% 0 0 -25%;

  /* Embiggen */
  transform: scale(1.5); /* prefix me */

  /* Hidden */
  opacity: 0;
  pointer-events: none;
}

Step 4) Active Modal State (The Magic!)

Now we have all we need to "recede" the page when the modal is open. Let's target the #page-wrap when the state is active and do the magic.

The magic is simply: transform scale the #page-wrap to make it smaller and filter the #page-wrap to make it blurry and less colorful.

.dialogIsOpen #page-wrap {

  /* Blur and de-color */
  -webkit-filter: blur(5px) grayscale(50%);

  /* Recede */
  -webkit-transform: scale(0.9);

}

WebKit only? Well... the filters are only in WebKit for the time being. Your call if you want to load up the vendor prefixes or not. If I was going to use this for real on a site, I'd spend a little time making sure this effect had a fallback, which shouldn't be too hard. Perhaps just an emphatic box-shadow would do.

Then: Make the dialog appear from above, enforcing the depth of field effect. Opacity makes it appear; transform scale makes it appear from above.

.dialogIsOpen #modal {
  
  /* Regular size and visible */
  transform: scale(1); /* prefix me */
  opacity: 1;

  /* Clickable */
  pointer-events: auto;

}

Step 5) Transitions

To make it feel natural and magical, toss in some transitions on both of the players involved.

#page-wrap, #modal {
  
  transition: all 0.4s ease; /* prefix me */

}

Of course Sass/Compass makes all this a bunch easier since it has @mixins for all this stuff. e.g.

@include transition(all 0.4s ease);
@include filter(blur(5px) grayscale(50%));
@include transform(scale(0.9));

Fair warning, this stuff is fairly memory/processing intensive. Sometimes little hacks like triggering 3D transforms helps in WebKit, at the risk of nasty looking text.

body {
  /* Use at your own discretion */
  -webkit-transform: translateZ(0);
}

Wrapup

A video, if you don't have access to a supported browser or whatever:

I put my reverse engineered demo from this article on CodePen, but you should really just go look at Hakim's demo, which also on CodePen.

Comments

  1. Permalink to comment#

    wicked cool stuff!

  2. Permalink to comment#

    I really like Hakim El Hattab’s work — discovered it on CodePen. I’ve redone my personal website using another of his projects, Meny.

  3. Permalink to comment#

    Blur & grayscale are cross-browser with simple svg filter for Firefox and proprietary filter for IE ( even IE6, and for the record with an older syntax you could get IE4 !! ).

    Missing transitions for IE but hey ! a little piece of Jquery should do the trick.

    Too bad for the performance thing, it would be nice to use !!

    Great tip, thanks for sharing !

  4. Nhim Chanborey
    Permalink to comment#

    Amazing!

  5. Nhim Chanborey
    Permalink to comment#

    Seems like the code doesn’t clear! Can you show me all the code you did in the video?

  6. Peter S.
    Permalink to comment#

    How does this perform on netbooks/tablets? There a noticeable lag/stutter of the animation on my 1.6 GHz Core 2 Duo laptop.

    • Permalink to comment#

      I tried it on a new i7 laptop, the animation is laggy on this one and also on a Nexus4 Android with 4 cores

  7. Permalink to comment#

    Very cool! Wheels are turning. Might use this in some way for my artist site!

  8. Permalink to comment#

    This is a really cool interaction. I love how it kind of pulls you in, and you can’t help but pay attention to the modal. Could be good for “you must be logged in to do that” kind of messages.

  9. Permalink to comment#

    Hakim rules! I’ve clicked a link on some WebGL blog, where he was giving pointers and ended up on his blog. Have been reading it all sunday. Hakim is the king of the Immersive Front-End experience.

  10. Permalink to comment#

    Love the effect. It’s a bit laggy on my aging computer (dual core processor ftw)

    Might play around with it in some future projects.

  11. Permalink to comment#

    Awesome work, thanks so much for sharing!

  12. Rob
    Permalink to comment#

    Quite the amazing effect. If only system performance could be transfered with the User Agent much like screen resolutions are (number of CPU cores), this type of effect could easily be degraded or enhanced based on that.

    I did notice, however, that while the effect performs with a few hickups on my iOS6 iPhone 4 (which could be fixed by removing the blur filter), my iPhone 5 has NO problems rendering this effect in a next-to-native level.

    • Rob
      Permalink to comment#

      Whoops, fail on my part. I am so used to looking at screen res information in Google Analytics that i forget it is not actually part of the User Agent, but rather part of GA’s javascript tracking/accounting code.

  13. Permalink to comment#

    Hakim has an amazing portfolio with experiments such as the one presented on this blog but also others such as Fokus or Meny (on of my favourites).
    Great inspiration.

  14. Rafal Krupiński
    Permalink to comment#

    I’d be careful with toggling body class. test

    • Ben
      Permalink to comment#

      Interesting jsperf test. But I don’t understand why IE doesn’t have any results at all for body class. What’s going on there?

    • Rafał Krupiński
      Permalink to comment#

      IE does have results for body class but the numbers are: 100/sec vs. milions/sec (other browsers).

  15. I love Hakim’s work! Thanks for sharing

  16. Hi Mr. coyier
    I am learning new techniques from your articles.
    Your articles are wonderful!
    Thank you very much.

  17. Modal interfaces, even aesthetically pleasing ones, should be discouraged.

  18. fishme
    Permalink to comment#

    Wow nice – thank you!

  19. Andrea
    Permalink to comment#

    Very nice work.

    Is it possible to have one button trigger 3 modal overlays?

    thank you!
    Andrea

  20. kamel Akhmed
    Permalink to comment#

    working only on Chrome only :(

  21. Permalink to comment#

    I’m trying to create a staff directory with images that one can click on and produe a modal
    dialogue box that is interactive (i.e contact info for person whose picture is clicked).
    This seems to offer me a hint on how to get there but I don’t want to risk building something that shouldn’t be built or rebuilding something that is easily accomplished by using an existing WordPress plugin. Any feedback is appreciated.

This comment thread is closed. If you have important information to share, you can always contact me.

*May or may not contain any actual "CSS" or "Tricks".