Super Simple Lightbox With CSS and jQuery
Rather than using a bloated plugin that has features you'll never use, this tutorial shows you how to create a super simple lightbox for handling images. It's perfect for image galleries, portfolios, and more!
The Lightbox Demo We’re Building
Step 1: The Markup
Open up your favorite text editor (I use Coda) and let's start with our HTML markup.
1 |
<!DOCTYPE html>
|
2 |
<html>
|
3 |
<head>
|
4 |
<title>Simple Lightbox</title> |
5 |
</head>
|
6 |
<body>
|
7 |
|
8 |
<div id="wrapper"> |
9 |
<h1>Super Simple Lightbox</h1> |
10 |
<p>Our super simple lightbox demo. Here are the image links:
|
11 |
<ul>
|
12 |
<li>
|
13 |
<a href="img.jpg" class="lightbox_trigger"> |
14 |
Picture 1 |
15 |
</a>
|
16 |
</li>
|
17 |
<li>
|
18 |
<a href="img2.jpg" class="lightbox_trigger"> |
19 |
Picture 2 |
20 |
</a>
|
21 |
</li>
|
22 |
<li>
|
23 |
etc. |
24 |
</li>
|
25 |
</ul>
|
26 |
</p>
|
27 |
</div> <!-- #/wrapper --> |
28 |
</body>
|
29 |
</html>
|



Note: We used the class lightbox_trigger
on every link that has an image we want to show in our lightbox. This becomes useful when we want to target those elements in the script we will write.
Step 2: The CSS
Let's apply some basic CSS styling to our page. For this basic example, we will place all our CSS in <style></style>
tags within the <head>
section.
1 |
body { |
2 |
margin:0; |
3 |
padding:0; |
4 |
background:#efefef; |
5 |
text-align:center; /* used to center div in IE */ |
6 |
}
|
7 |
#wrapper { |
8 |
width:600px; |
9 |
margin:0 auto; |
10 |
border-top:none; |
11 |
text-align:left; |
12 |
}
|



Now we have a simple but elegant page in which we can test out our lightbox window.
Step 3: Notes on CSS in Internet Explorer
We used margin: 0 auto;
to center our #wrapper div horizontally on the page. This works great for all browsers except...drum roll...you guessed it, Internet Explorer. To fix this issue, we used text-align:center;
on the body tag which centers child divs (in our case, the #wrapper div) horizontally on the page.
However, using text-align:center;
on the body tag will center align all text in child divs as well. To fix this, we reset text-align:left;
in the #wrapper div to restore all further child content to left alignment. Check out communitymx.com if you're interested in learning more about this particular IE issue.
Step 4: Lightbox HTML
This is what our lightbox HTML markup will look like. However, do not insert it into the HTML markup. We will do that dynamically with jQuery. Understanding the lightbox HTML markup allows us to style it with CSS accordingly.
1 |
<div id="lightbox"> |
2 |
<p>Click to close</p> |
3 |
<div id="content"> |
4 |
<img src="#" /> |
5 |
</div>
|
6 |
</div>
|
Note: There is no value for the image src
because that will be dependent upon which link is clicked by the user. Hence, we will use javascript to insert the value dynamically later on.
Step 5: Lightbox CSS Black Overlay
Let's go ahead and insert the CSS for our lightbox. By doing this now, our page elements will display properly later on when we dynamically insert our HTML.
First we have our #lightbox div which acts as the black overlay. We want this to fill the user's entire viewport, no matter the screen resolution.
1 |
#lightbox { |
2 |
position:fixed; /* keeps the lightbox window in the current viewport */ |
3 |
top:0; |
4 |
left:0; |
5 |
width:100%; |
6 |
height:100%; |
7 |
background:url(overlay.png) repeat; |
8 |
text-align:center; |
9 |
}
|



The CSS is pretty straightforward. Here are a few highlights:
-
position:fixed
makes our overlay appear correctly in the current viewport, no matter the user's position on screen (top or bottom of the page). This is where using the right DOCTYPE comes in handy. If IE runs in quirks mode due to the wrong DOCTYPE (or none at all), then our overlay won't appear correctly. -
width:100%; height:100%;
This makes our #lightbox div, which acts as the black overlay, cover the entire viewport no matter the end user's screen resolution. -
background:url(overlay.png) repeat;
We could have used RGBa color to make our background color slightly transparent. This would have required something likebackground: rgba(0,0,0,.7);
. However, since this property is fairly new, background color transparency won't work in IE (unless you apply certain hacks). So instead of using CSS3 and IE hacks, we simply create a 1x1 pixel PNG in photoshop with a black background and set the transparency at about 75 percent.
Step 6: Lightbox CSS User Instructions
Just to make sure our users aren't confused, we will add some text in the lightbox overlay stating they can click anywhere to have the lightbox window disappear.
1 |
#lightbox p { |
2 |
text-align:right; |
3 |
color:#fff; |
4 |
margin-right:20px; |
5 |
font-size:12px; |
6 |
}
|
Step 7: Lightbox CSS Image
Now we want to style the image that will appear in the lightbox. I am simply going to give it some CSS3 effects. These obviously won't appear in IE and older browsers, but it still degrades gracefully. You could add other effects here if you wanted, like a white border around the image.
1 |
#lightbox img { |
2 |
box-shadow:0 0 25px #111; |
3 |
max-width:940px; |
4 |
}
|
Note:Adding a max-width
will shrink any high-resolution images we might be linking to. This helps to ensure all images fit into the viewport.
This is what our page would now look like if we inserted our lightbox HTML into the document (we're going to do that with javascript):



Step 8: Determining the Logic
If the user clicks a link to image, we want to display the image in a lightbox. To do this, we first want determine the logic behind our functionality:
- User clicks link with class
lightbox_trigger
- Prevent the browser from following the link by default
- See if the
lightbox
div is already inside the document- If it exists:
- Find and the existing
img
tag inside thecontent
div. - Replace the image's
src
value with thehref
value of whatever link was clicked.
- Find and the existing
- If it doesn't exist:
- insert the lightbox markup into the page with the image's
src
value set to thehref
value of whatever link was clicked
- insert the lightbox markup into the page with the image's
- If it exists:
- Allow any click on the lightbox (when displayed) to make it disappear
Step 9: JavaScript Including jQuery
Now that we have our HTML markup, CSS styling, and JavaScript logic, it's time to delve into the code and get our functionality working.
First we include the jQuery library. I like to add it right before the closing body
tag using jQuery's hosted version.
1 |
<script src="https://code.jquery.com/jquery-1.6.2.min.js"></script> |
Step 10: JavaScript Custom Script
Let's wrap our code in script
tags and jQuery's document.ready
We will include our custom script right after including the jQuery library. We setup our jQuery function using the dollar sign "$" in the function parameter, that way we don't have to write "jQuery" every time we want to use jQuery functionality. Instead we can simply use "$".
1 |
<script> |
2 |
jQuery(document).ready(function($) { |
3 |
|
4 |
// Our script will go here
|
5 |
|
6 |
});
|
7 |
</script> |
Step 11: JavaScript lightbox_trigger
Function
As explained earlier in Step 1, for each linked image that will use the lightbox, we assigned it a class of lightbox_trigger
. In our script, we create a function that will fire every time a link with the class lightbox_trigger
is clicked.
1 |
$('.lightbox_trigger').click(function(e) { |
2 |
|
3 |
// Code that makes the lightbox appear
|
4 |
|
5 |
});
|
Step 12: JavaScript Inside the Function
First, we want to prevent the default action. This prevents the browser from opening the linked image in a new page. (To see why we use preventDefault()
click here
1 |
e.preventDefault(); |
Next, we get the href
of the image we will display in the lightbox from the link that was clicked:
1 |
var image_href = $(this).attr("href"); |
This is where our predetermined logic really comes into play. Because we are inserting our lightbox HTML dynamically, we want to first see if it already exists in the document. So, we will run an if/else statement.
If
If the lightbox
div exists, we will replace the img
tag inside the content
div. We will also make sure the src
value of the image tag is set to match the href
of whatever link was clicked.
1 |
if ($('#lightbox').length > 0) { // #lightbox exists |
2 |
|
3 |
//insert img tag with clicked link's href as src value
|
4 |
$('#content').html('<img src="' + image_href + '" />'); |
5 |
|
6 |
//show lightbox window - you can use a transition here if you want, i.e. .show('fast')
|
7 |
$('#lightbox').show(); |
8 |
}
|
Else
If our lightbox
div cannot be found in the document, we will create it and insert it. This will only run the first time a lightbox link is clicked.
1 |
else { //#lightbox does not exist |
2 |
|
3 |
//create HTML markup for lightbox window
|
4 |
var lightbox = |
5 |
'<div id="lightbox">' + |
6 |
'<p>Click to close</p>' + |
7 |
'<div id="content">' + //insert clicked link's href into img src |
8 |
'<img src="' + image_href +'" />' + |
9 |
'</div>' + |
10 |
'</div>'; |
11 |
|
12 |
//insert lightbox HTML into page
|
13 |
$('body').append(lightbox); |
14 |
}
|
Step 13: JavaScript Hiding the Lightbox Window
Lastly we want to hide the lightbox window whenever the user clicks on it. Because the div lightbox
is inserted after the DOM is already constructed (the page has already loaded) we have to tell jQuery that we are placing a new div in our page and to watch for clicks on this new elements. We do this by using .on()
rather than .click()
:
1 |
$('body').on('click', '#lightbox', function() { |
2 |
$('#lightbox').hide(); |
3 |
});
|
Note: in previous versions of this tutorial we used .live()
but this was removed from jQuery in version 1.9. We therefore have migrated it to use .on()
.
Step 14: JavaScript Final Code
Now that we've examined our lightbox script, here is what the whole thing looks like:
1 |
<script> |
2 |
jQuery(document).ready(function($) { |
3 |
|
4 |
$('.lightbox_trigger').click(function(e) { |
5 |
|
6 |
//prevent default action (hyperlink)
|
7 |
e.preventDefault(); |
8 |
|
9 |
//Get clicked link href
|
10 |
var image_href = $(this).attr("href"); |
11 |
|
12 |
/*
|
13 |
If the lightbox window HTML already exists in document,
|
14 |
change the img src to to match the href of whatever link was clicked
|
15 |
|
16 |
If the lightbox window HTML doesn't exists, create it and insert it.
|
17 |
(This will only happen the first time around)
|
18 |
*/
|
19 |
|
20 |
if ($('#lightbox').length > 0) { // #lightbox exists |
21 |
|
22 |
//place href as img src value
|
23 |
$('#content').html('<img src="' + image_href + '" />'); |
24 |
|
25 |
//show lightbox window - you could use .show('fast') for a transition
|
26 |
$('#lightbox').show(); |
27 |
}
|
28 |
|
29 |
else { //#lightbox does not exist - create and insert (runs 1st time only) |
30 |
|
31 |
//create HTML markup for lightbox window
|
32 |
var lightbox = |
33 |
'<div id="lightbox">' + |
34 |
'<p>Click to close</p>' + |
35 |
'<div id="content">' + //insert clicked link's href into img src |
36 |
'<img src="' + image_href +'" />' + |
37 |
'</div>' + |
38 |
'</div>'; |
39 |
|
40 |
//insert lightbox HTML into page
|
41 |
$('body').append(lightbox); |
42 |
}
|
43 |
|
44 |
});
|
45 |
|
46 |
//Click anywhere on the page to get rid of lightbox window
|
47 |
$('body').on('click', '#lightbox', function() { //must use on, as the lightbox element is inserted into the DOM |
48 |
$('#lightbox').hide(); |
49 |
});
|
50 |
|
51 |
});
|
52 |
</script> |
Final Lightbox Demo
Let’s remind ourselves of the simple lightbox we’ve built:
That’s it! Now you have a lightweight lightbox solution for displaying images. I hope you enjoyed this tutorial and learned something new! Thanks for reading.
Premium Options
This tutorial has taught you the do-it-yourself approach, but if you prefer a ready-made solution, Envato Market has lots of lightbox plugins available. Here's a selection of the most popular:
1. Lightbox Evolution
Lightbox Evolution is a tool for displaying images, html content, maps, and videos in a “lightbox” style that floats overtop of web page. Using Lightbox Evolution, website authors can showcase a wide assortment of media in all major browsers without navigating users away from the linking page.



2. PopupPress - Popups with Slider & Lightbox for WP
PopupPress is a Wordpress plugin that lets you easily create elegant overlapping windows. This plugin is intended to insert any type of content in a Popup of the fastest and easiest way on any page of WordPress. It also has a simple system statistics for the number of views of the popups.


