Simple Lightbox-like effects with CSS and Javascript explained.
Programming a lightbox-like effect with CSS and Javascript.
One way of really adding a professional touch to your website is with lightboxes. This is an effect where, when you have a popup, the rest of the website behind the popup darkens and blurs. A simplified version of the effect looks like this:
This effect is quite popular with Web 2.0 sites, and there are many libraries that allow you to use lightbox like effects. It doesn’t just look good to the visitors though- many people have found that the use of lightboxes will help with signups for their blog (if they use a signup form in the popup), or can even help with sales.
If you don’t want to use someone else’s library though (maybe because you’re worried about their size), you can implement your own lightbox surprising easily, with just a little CSS and Javascript.
Start by defining the CSS for the background. It looks like this:
#screenoverlay
{
background-color: #CCCCFF; /* The color to display*/
/* The transparency of the background */
opacity: .75;
filter: alpha(opacity=75);
position: absolute;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
/* Set the overlay so it appears above the page content */
z-index: 1000;
}
This CSS sets up the div we will use, so that it takes up the entire page (With the width/height 100% lines), and so that it appears above the rest of the page content (with the z-index line). You can change the color quite easily (Just alter the background-color line), and the transparency (or how “see-through” the overlay is), with the opacity and filter lines.
To use this CSS, we add the following just after the opening body tag.
<div id="screenoverlay" style="visibility:hidden;"></div>
This will setup the overlay, and make sure that by default it is hidden. If you wish to see it while you are developing, you would just use this:
<div id="screenoverlay"></div>
The next thing to do is setup the CSS for the popup itself. You can use various techniques for this- in this example I’ll just create a simple 200px X 200px, centered div.
The CSS for this is:
#topbox
{
position: absolute;
/* Default positioning, that will be overridden by the javascript */
left: 0px;
top: 0px;
padding: 5px;
width: 200px;
height: 200px;
background-color: #FFFFFF;
/*Must use a z-index greater than the screen overlay*/
z-index: 1001;
}
This sets up the div so that it is 200×200 (the width and height lines), has a white background, and is positioned in the top left corner. (The javascript will override this, so that the div is actually centered on the screen.)
Note the z-index: It’s important that this number is higher than the number for the screenoverlay, otherwise the popup will appear underneath instead of above.
Now we add the div to the HTML, right underneath the screenoverlay…
<div id="topbox" style="visibility:hidden;">
<h3>Hey there!</h3>
<a href='#' onclick="closeTop();return false;">Close</a>
</div>
This code adds the div to the page, and sets it to be hidden by default. The link code will call the javascript function ‘closeTop’ when it is clicked. (NB: Clicking the link wont actually go anywhere because of the ‘return false;’).
Underneath this, we add the normal page content.
You can bring up the popup any way you want, including mouse-overs, when a link is clicked, or using a delay. In this example, I’ll just bring up the popup when a link is clicked…
<a href='#' onclick="showTop();return false;">
Click here to display the lightbox...</a>
This calls the javascript function ’showTop’, when the link is clicked.
Now we get to the javascript functions. We only need 2, one to bring the popup up (showTop), and one to close it (closeTop).
At their simplest, these functions are just this:
<script type="text/javascript" language="javascript">
//<!--
function showTop()
{
//Show the background overlay and topbox...
document.getElementById('screenoverlay').style.visibility='visible';
document.getElementById('topbox').style.visibility='visible';
}
function closeTop()
{
//Hide the overlay and tobox...
document.getElementById('screenoverlay').style.visibility='hidden';
document.getElementById('topbox').style.visibility='hidden';
}
//-->
</script>
As you can see, the code just overrides the visibility of the CSS.
If you want to do anything more fancy, you can add extra code to showTop. If you wanted to actually change the popups content, you might set it by overriding innerHTML (eg: document.getElementById('topbox').innerHTML = ‘Hello World’;). In this example, we want to center the popup, so we will change showTop to…
function showTop()
{
//Position the topBox. In this example I am just centering it on the screen
boxWidth = 200;
boxHeight = 200;
screenWidth=document.all?document.body.clientWidth:window.innerWidth;
screenHeight=document.all?document.body.clientHeight:window.innerHeight;
xPos = (screenWidth - boxWidth) * 0.5;
yPos = (screenHeight - boxHeight) * 0.5;
document.getElementById('topbox').style.left=xPos;
document.getElementById('topbox').style.top=yPos;
//Show the background overlay and topbox...
document.getElementById('screenoverlay').style.visibility = 'visible';
document.getElementById('topbox').style.visibility = 'visible';
}
That’s it! Quite simple I think you’ll agree! Obviously you can add much more fancy effects, but hopefully now you understand the basics. If you want to see the example in action , click here.
Tags: css, Javascript, Lightbox

December 1st, 2008 at 9:43 pm
Thanks!
February 10th, 2009 at 6:55 pm
I love it.. but when I try centering it, it just comes up blank. I copied exactly, but not having luck. It’s weird, when I try your example it works beautifully. But even when I vanilla out a copy of the code, I can’t get it… I’ll keep poking at it.
February 10th, 2009 at 7:03 pm
GOT IT!!! It was my header:
caused failure… I don’t know enough to know why, but I thought I share the results. THANKS AGAIN!!!!
February 10th, 2009 at 7:04 pm
!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”
html xmlns=”http://www.w3.org/1999/xhtml”
March 21st, 2009 at 12:30 am
Great, but I noticed that your code does not work in IE 6.0 if the web page has a doctype. Do you know why?
It would be great if you could make your example work with a doctype.
Regards,
Saeid
March 21st, 2009 at 5:35 am
I found out that the problem is due to The !DOCTYPE “Switch” in IE 6.0. So your example works as long as IE6.0 is in quirks mode, but as soon as we add a doctype to your example, IE6.0 starts rendering the page in standard mode and the overlay effect disappears.
After more digging into this, I found out the problem is with CSS rule “height: 100%;”. IE 6.0 doesn’t seem to like that in standard mode, so instead I used a few lines of JavaScript to detect browser version and then set the height of overlay equal to document’s height rather than 100% if the browser is IE6.0.
Also when there is a scrollbar in the page, your overlay only covers the viewport area and if someone scrolls down it ends. This is because you are using position: absolute instead of position: fixed which solves this problem. Since IE 6.0 doesn’t understand position: fixed, you need to change your overlay’s positioning CSS to this:
position: fixed;
_position: absolute; /* for IE 6.0 */
You can see/download my revised version of your example that is also W3C valid at:
http://saeid.fastmail.fm/files/saeid/webdev/DOMScripts/lightbox/
I have removed the code related to centering of topbox to make the example simpler.
August 24th, 2009 at 7:37 am
ujepubej…
Coi Phim Sex …
September 25th, 2009 at 12:19 pm
xaqyseq…
ferrari jersey monmouth new …
June 24th, 2010 at 12:27 pm
Medicamentspot.com International Legal RX Medications. Special Internet Prices (up to 40% off average US price). NO PRIOR PRESCRIPTION REQUIRED!…
Combivir@buy.online” rel=”nofollow”>.…