Tuesday, December 4, 2007

Create a Modal Dialog Using CSS and Javascript

The code behind this effect is surprisingly simple. There are three pieces involved:

* A
containing the content you want to appear when the modal area is activated.
* Two CSS rules which keep the layer hidden until needed and then “fullscreen” when activated.
* Javascript which hides and shows the
.

The overlay


At the bottom of your HTML, create a
with id = “overlay”. Any content placed inside this area will initially be hidden by the browser and then shown modally when activated. Any content beneath it will be “unclickable” by the user, which forces them to interact with whatever message you give them.

Inside #overlay I usually place another
which I center horizontally and apply a few styles to create a dialog box appearance. It’s this second
that actually contains the content I’m showing the user.



Content you want the user to see goes here.





The CSS

There’s only one CSS rule to take care of the fullscreen/hiding for #overlay.

#overlay {
visibility: hidden;
position: absolute;
left: 0px;
top: 0px;
width:100%;
height:100%;
text-align:center;
z-index: 1000;
}

You can style the inner
however you like. As I said above, I usually center it horizontally to give it more of a dialog box look and feel.

#overlay div {
width:300px;
margin: 100px auto;
background-color: #fff;
border:1px solid #000;
padding:15px;
text-align:center;
}

The Javascript

The javascript that controls everything is insanely simple. Just add the following function to wherever you’re storing your javascript. (That could be in the of your document or in an external .js file.)

function overlay() {
el = document.getElementById("overlay");
el.style.visibility = (el.style.visibility == "visible") ? "hidden" : "visible";
}

The javascript grabs our overlay element and then toggles its visibility property. If it’s hidden, it makes it visible and vice versa. You could create a function to explicitly show or hide the layer, but I prefer this automatic toggle method since it requires less code.

With the function in place, we then call it whenever we want to show the overlay and then again to hide it. So, somewhere on our page we could add

Click here to show the overlay

When the user clicks on the link our javascript will show the overlay.

Within the overlay’s HTML, we need to add a link to hide it. The code is exactly the same:

Click here to [close]

Finishing Touches

Combining all the pieces together we can create a simple example. (View the source to see everything put together.)

However, when the user clicks the link to show the overlay, they may become confused since it looks like they can still click on any element in the page. To help them understand what’s going on, we can apply a background image to the overlay
. In Photoshop I create a simple checkerboard pattern .gif with transparency. This creates a shaded effect so the user can still see the underlying web page but know not to click on it. To add the background to the layer, add the following to our CSS.

#overlay {
visibility: hidden;
position: absolute;
left: 0px;
top: 0px;
width:100%;
height:100%;
text-align:center;
z-index: 1000;
background-image:url(overlay.gif);
}

And finally, (as always) we need to add a small tweak so that this works in Internet Explorer. Fortunately, it’s quick fix. Add this to your CSS.

body {
height:100%;
margin:0;
padding:0;
}

In order for an element to have a percentage height applied to it (which #overlay does), IE requires the parent element to have a height. So, just set body’s height to 100% and zero the margins and we’re all set! You can see the finished product here.

No comments: