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”>.…
September 5th, 2010 at 6:04 pm
Buy:Retin-A.Prednisolone.Lumigan.Human Growth Hormone.Arimidex.Mega Hoodia.Nexium.Prevacid.Accutane.Petcam (Metacam) Oral Suspension.Valtrex.Zovirax.Synthroid.100% Pure Okinawan Coral Calcium.Zyban.Actos….
September 6th, 2010 at 2:23 pm
Buy:Viagra Super Force.Cialis Soft Tabs.Zithromax.Viagra.Tramadol.Soma.Maxaman.Cialis Super Active+.Cialis.Viagra Soft Tabs.VPXL.Propecia.Levitra.Viagra Professional.Viagra Super Active+.Cialis Professional.Super Active ED Pack….
November 11th, 2010 at 2:31 am
…
BUY FASHION. TOP BRANDS: GUCCI, DOLCE&GABBANA, BURBERRY, DIESEL, ICEBERG, ROBERTO CAVALLI, EMPORIO ARMANI, VERSACE…
December 20th, 2010 at 10:24 am
Abilify@official.site” rel=”nofollow”>.…
Buywithout prescription…
December 20th, 2010 at 5:33 pm
abilify side effects in children…
Buydrugs without prescription…
December 22nd, 2010 at 6:45 pm
dosage of amoxicillin for respiratory disorder…
Buygeneric drugs…
December 25th, 2010 at 4:04 pm
rxlist@abilify.now” rel=”nofollow”>..…
Buydrugs without prescription…
December 28th, 2010 at 9:23 pm
Advair@official.site” rel=”nofollow”>.…
Buynow it…
December 30th, 2010 at 1:59 am
Amoxicillin@official.site” rel=”nofollow”>……
Buyno prescription…
December 30th, 2010 at 3:34 pm
Ampicillin@official.site” rel=”nofollow”>.…
Buynow it…
December 31st, 2010 at 7:20 am
..…
Buygeneric drugs…
December 31st, 2010 at 2:09 pm
Azor@official.site” rel=”nofollow”>……
Buygeneric pills…
January 1st, 2011 at 1:18 pm
Augmentin@official.site” rel=”nofollow”>.…
Buywithout prescription…
January 1st, 2011 at 9:53 pm
Clonidine@official.site” rel=”nofollow”>……
Buynow…
January 2nd, 2011 at 10:00 am
celexa@canada.now” rel=”nofollow”>.…
Buynow…
January 4th, 2011 at 6:01 pm
generic@viagra.sildenafil.paypal.no.prescription” rel=”nofollow”>……
Buygeneric meds…
January 5th, 2011 at 8:04 pm
abilify@and.sleep.issues” rel=”nofollow”>.…
Buyno prescription…
January 17th, 2011 at 10:14 am
Abilify@official.site” rel=”nofollow”>……
Buynow…
February 3rd, 2011 at 12:12 pm
lisinopril aspirin interaction…
Buywithout prescription…
February 4th, 2011 at 7:03 pm
what is the shelf life of zyrtec…
Buynow it…
February 5th, 2011 at 8:47 am
Zocor…
Buygeneric meds…
February 7th, 2011 at 4:58 am
dog zyrtec…
Buygeneric drugs…
February 7th, 2011 at 5:02 pm
Vytorin…
Buynow…
March 3rd, 2011 at 4:33 am
Zyvox…
Buydrugs without prescription…
March 3rd, 2011 at 9:34 am
Zofran…
Buygeneric pills…
March 4th, 2011 at 12:52 pm
Zyprexa…
Buydrugs without prescription…
March 5th, 2011 at 4:32 am
Rogaine…
Buygeneric meds…
March 7th, 2011 at 3:55 am
Orlistat…
Buyit now…
March 7th, 2011 at 5:45 pm
Synthroid…
Buygeneric pills…
March 8th, 2011 at 1:06 pm
Prozac…
Buyno prescription…
March 9th, 2011 at 7:09 am
howard county purim carnival 2010…
Buyno prescription…
March 9th, 2011 at 7:22 pm
Risperdal…
Buygeneric pills…
March 10th, 2011 at 6:50 pm
Rogaine…
Buygeneric meds…
March 11th, 2011 at 6:01 am
low dose haldol and paxil…
Buygeneric drugs…
March 29th, 2011 at 4:50 am
Zoloft…
Buynow it…
March 29th, 2011 at 1:42 pm
Tri@Cyclen.now” rel=”nofollow”>……
Buyno prescription…
March 29th, 2011 at 9:38 pm
Soma@Soma.Soma” rel=”nofollow”>.…
Buyno prescription…
March 30th, 2011 at 3:06 pm
Purim@Purim.Purim” rel=”nofollow”>……
Buygeneric drugs…
March 31st, 2011 at 12:23 am
Singulair@Singulair.Singulair” rel=”nofollow”>……
Buygeneric pills…
March 31st, 2011 at 1:19 am
Prozac@Prozac.Prozac” rel=”nofollow”>.…
Buygeneric pills…
March 31st, 2011 at 4:10 pm
Cipro@Cipro.Cipro” rel=”nofollow”>..…
Buygeneric meds…
March 31st, 2011 at 4:11 pm
Singulair@Singulair.Singulair” rel=”nofollow”>.…
Buygeneric drugs…
April 2nd, 2011 at 12:38 am
cms@coverage.of.pulmicort.respules.j7627” rel=”nofollow”>.…
Buygeneric pills…
April 2nd, 2011 at 4:35 pm
allegra@print.colorado.springs” rel=”nofollow”>……
Buygeneric drugs…
April 3rd, 2011 at 1:20 pm
buy@acai.berry.supplement” rel=”nofollow”>.…
Buyit now…
April 3rd, 2011 at 3:39 pm
accutane@and.ototoxicity” rel=”nofollow”>……
Buyno prescription…
April 4th, 2011 at 5:38 pm
alternatives@to.premarin” rel=”nofollow”>……
Buygeneric pills…
April 5th, 2011 at 4:51 am
altace@generic.name” rel=”nofollow”>.…
Buynow it…
April 5th, 2011 at 8:42 am
.…
Buygeneric drugs…
April 5th, 2011 at 4:59 pm
coral@calcium.safety” rel=”nofollow”>.…
Buyit now…
April 6th, 2011 at 8:35 am
abilify@does.it.work” rel=”nofollow”>.…
Buynow it…
April 6th, 2011 at 2:57 pm
calories@and.nutrition.in.boost.nutritional.energy.drink” rel=”nofollow”>……
Buywithout prescription…
June 20th, 2011 at 1:29 pm
….
buy.cialis.viagra@levitra.online.pharmacy” rel=”nofollow”>.…
July 15th, 2011 at 9:33 pm
buy@generic.VIAGRA” rel=”nofollow”>……
Need cheap generic VIAGRA?…
July 21st, 2011 at 6:35 pm
buy@generic.LEVITRA” rel=”nofollow”>……
Need cheap generic LEVITRA?…
July 29th, 2011 at 5:39 am
Buy@Discount.Coral.Calcium” rel=”nofollow”>.…
Buyit now…
July 29th, 2011 at 10:14 am
Buy@Abana.Online” rel=”nofollow”>.…
Buynow it…
July 31st, 2011 at 8:47 pm
Order@Cheap.Coral.Calcium” rel=”nofollow”>..…
Buyit now…
August 2nd, 2011 at 7:51 am
Order@Coral.Calcium.Online” rel=”nofollow”>..…
Buyno prescription…
August 2nd, 2011 at 2:35 pm
Order@Abana.Online” rel=”nofollow”>..…
Buygeneric pills…
August 3rd, 2011 at 1:53 am
….
.@cialis.online” rel=”nofollow”>.…
August 3rd, 2011 at 11:36 am
Purchase@Generic.Abilify” rel=”nofollow”>..…
Buygeneric drugs…
August 4th, 2011 at 2:58 am
Order@Discount.Acai” rel=”nofollow”>..…
Buywithout prescription…
August 4th, 2011 at 5:49 am
Purchase@Cheap.Acai” rel=”nofollow”>……
Buyit now…
August 5th, 2011 at 9:17 am
Order@Cheap.Energy.Boost” rel=”nofollow”>.…
Buynow it…
August 5th, 2011 at 9:23 pm
….
.@cozaar.online” rel=”nofollow”>.…
August 5th, 2011 at 10:39 pm
Order@Discount.Accupril” rel=”nofollow”>.…
Buygeneric drugs…
August 6th, 2011 at 8:54 am
Order@Accutane.Online” rel=”nofollow”>.…
Buynow it…
August 6th, 2011 at 3:54 pm
….
.@buy.cozaar” rel=”nofollow”>.…
August 6th, 2011 at 6:20 pm
Buy@Accutane.Without.Prescription” rel=”nofollow”>.…
Buygeneric drugs…
August 8th, 2011 at 2:36 am
Buy@Aciphex.Without.Prescription” rel=”nofollow”>.…
Buygeneric meds…
August 10th, 2011 at 4:22 pm
Purchase@Generic.Actonel” rel=”nofollow”>.…
Buygeneric meds…
August 11th, 2011 at 7:48 am
Cheap@Actoplus.Met.Online” rel=”nofollow”>..…
Buygeneric meds qmo…
August 13th, 2011 at 9:22 pm
Purchase@Abilify.Without.Prescription” rel=”nofollow”>..…
Buygeneric meds abx…
August 14th, 2011 at 12:39 am
Purchase@Generic.Abilify.5mg.10mg.15mg.20mg.30mg” rel=”nofollow”>..…
Buywithout prescription yen…
August 14th, 2011 at 8:58 pm
Order@Generic.Acai” rel=”nofollow”>..…
Buyno prescription wkp…
August 15th, 2011 at 8:10 am
Cheap@Generic.Acai.500mg” rel=”nofollow”>..…
Buyno prescription tao…
August 17th, 2011 at 8:12 am
Purchase@Discount.Accutane” rel=”nofollow”>.…
Buygeneric drugs mby…
August 17th, 2011 at 11:42 am
Buy@Generic.Accutane” rel=”nofollow”>……
Buygeneric meds svb…
August 17th, 2011 at 5:14 pm
Purchase@Generic.Accutane” rel=”nofollow”>.…
Buygeneric drugs axm…
August 18th, 2011 at 2:14 am
….
best.places.to@buy.jeans” rel=”nofollow”>.…
August 19th, 2011 at 5:01 am
Cheap@Aciphex.Without.Prescription” rel=”nofollow”>……
Buyit now sge…
August 21st, 2011 at 7:55 am
Purchase@Discount.Acomplia” rel=”nofollow”>.…
Buynow tqa…
August 22nd, 2011 at 10:47 am
Order@Actonel.Without.Prescription” rel=”nofollow”>..…
Buyno prescription jwi…
August 22nd, 2011 at 4:07 pm
Buy@Discount.Advair” rel=”nofollow”>..…
Buydrugs without prescription faj…
August 25th, 2011 at 6:23 pm
Order@Discount.Albenza” rel=”nofollow”>.…
Buynow it lmg…
August 28th, 2011 at 3:31 am
arcoxia@120mg.buy” rel=”nofollow”>……
Buygeneric drugs…
August 28th, 2011 at 8:47 am
abilify@and.sleep.issues” rel=”nofollow”>..…
Buydrugs without prescription…
August 29th, 2011 at 1:47 am
aloe@vera.juice.for.sale” rel=”nofollow”>..…
Buyit now…
August 29th, 2011 at 12:27 pm
side@effects.of.aricept” rel=”nofollow”>..…
Buyit now…
September 25th, 2011 at 4:58 pm
….
.…
October 17th, 2011 at 2:14 am
medieval armour…
Buy_generic drugs…
October 20th, 2011 at 4:51 am
urinary tract infection canine…
Buy_generic pills…
October 20th, 2011 at 6:11 pm
behavior symptoms of prescription drug addiction…
Buy_generic meds…
October 21st, 2011 at 2:31 am
honey for diabetes…
Buy_generic meds…
October 21st, 2011 at 8:02 am
warfarin sod…
Buy_drugs without prescription…
October 21st, 2011 at 6:02 pm
verapamil side effects…
Buy_drugs without prescription…
October 25th, 2011 at 8:43 am
death rate of hiv…
Buy_it now…
October 27th, 2011 at 12:43 am
trigger point injections with lidocaine…
Buy_without prescription…
October 27th, 2011 at 2:23 am
steroids and weight gain…
Buy_generic pills…
October 28th, 2011 at 8:23 am
adhd cant take stimulants adult…
Buy_it now…
October 29th, 2011 at 8:39 am
dehli meats during pregnancy…
Buy_now it…
October 29th, 2011 at 11:36 pm
nicotrol inhaler buy free sample oregon…
Buy_now…
October 30th, 2011 at 3:21 pm
imodium during pregnancy…
Buy_generic meds…
October 30th, 2011 at 8:20 pm
diabetes popcorn…
Buy_generic pills…
October 31st, 2011 at 1:00 pm
allergy blood testing…
Buy_it now…
October 31st, 2011 at 4:20 pm
atlanta zyprexa lawyers…
Buy_generic drugs…
October 31st, 2011 at 7:40 pm
weight loss pa…
Buy_no prescription…
November 1st, 2011 at 10:20 pm
clinical pharmacists prescribe medication…
Buy_it now…
November 4th, 2011 at 9:07 pm
resume clinical sas programmer…
Buy_generic meds…
November 5th, 2011 at 9:24 pm
breast cancer awareness wristband…
Buy_generic meds…
November 6th, 2011 at 8:44 pm
healing lithium water in utah…
Buy_no prescription…
November 7th, 2011 at 8:24 am
social skills training negative symptoms schizophrenia…
Buy_generic meds…
November 9th, 2011 at 10:58 pm
drug testing passing short notice…
Buy_generic meds…
November 10th, 2011 at 3:58 am
overweight employee abuse…
Buy_generic drugs…
November 10th, 2011 at 5:18 pm
liquid tylenol and dogs…
Buy_without prescription…
November 11th, 2011 at 2:58 pm
10 mg provera…
Buy_generic pills…
November 11th, 2011 at 7:58 pm
palpitations side effect homeo medications…
Buy_generic pills…
November 13th, 2011 at 1:58 am
john degan anna degan il…
Buy_drugs without prescription…
November 14th, 2011 at 7:58 am
food allergy and adenoids…
Buy_without prescription…
November 15th, 2011 at 10:36 pm
allergy testing and injections…
Buy_without prescription…
November 16th, 2011 at 1:56 am
esophagus 2009 jelsoft enterprises ltd…
Buy_now it…
July 1st, 2012 at 12:43 am
…
Purchase Unique Pills Now!…
July 23rd, 2012 at 8:37 am
…
Check Good Meds Now!…
July 23rd, 2012 at 12:14 pm
…
Purchase Quality Pharmacy Today!…
July 23rd, 2012 at 1:31 pm
…
Purchase Cheap Meds Now!…
July 23rd, 2012 at 3:18 pm
…
Buy Unique Generic.Drugs Today!…
April 13th, 2013 at 12:51 pm
Trackback…
[...] What host are you the use of? Can I am getting your associate link for your host? I desire my website loaded up as fast as yours lol[...]…