How to Create a Simple Lightbox

Jean Jul 10, 2026

Creating a simple lightbox can enhance the visual appeal of your website or application, allowing users to view images in a larger, more engaging format. This guide will walk you through the process of making a basic lightbox using HTML, CSS, and JavaScript. No prior experience with lightboxes is required, and we'll keep the code simple and easy to understand.

DIY Photo Light Box for $3 • The Lucky Sprout
DIY Photo Light Box for $3 • The Lucky Sprout

Before we dive into the code, let's briefly discuss what a lightbox is and why you might need one. A lightbox is a overlay that displays images, videos, or other content on top of the current page. It's often used to showcase high-quality images, provide additional information, or create a more interactive user experience.

the diy light box is open and ready to be used as a snack bowl
the diy light box is open and ready to be used as a snack bowl

Setting Up the Basic Structure

We'll start by creating the basic HTML structure for our lightbox. This will include the main content area, the lightbox overlay, and the lightbox itself.

Simple DIY Light Box Photography Ideas
Simple DIY Light Box Photography Ideas

First, let's create a simple HTML page with a button that will trigger our lightbox:

```html

```

HTML Elements

the light box is set up for photography
the light box is set up for photography

The main elements we're using here are:

  • #lightbox-btn: The button that triggers the lightbox.
  • #lightbox-overlay: The overlay that darkens the background when the lightbox is open.
  • #lightbox: The actual lightbox that contains the image.
  • #close-btn: The button that closes the lightbox.
  • #lightbox-img: The image element that will display the image in the lightbox.

Initial CSS Styling

a light box sitting on top of a table with the words make a custom light box
a light box sitting on top of a table with the words make a custom light box

Next, let's add some basic CSS to style our lightbox and make it look presentable:

```css .hidden { display: none; } #lightbox-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.8); z-index: 1; } #lightbox { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); background-color: #fff; padding: 20px; border-radius: 5px; z-index: 2; } #close-btn { font-size: 20px; cursor: pointer; } #lightbox-img { max-width: 100%; height: auto; } ```

This CSS will hide the lightbox by default, style the overlay, position the lightbox in the center of the screen, and style the image inside the lightbox.

Adding Functionality with JavaScript

an open cardboard box sitting on top of a floor next to other pieces of paper
an open cardboard box sitting on top of a floor next to other pieces of paper

Now that we have the basic structure and styling in place, let's add the JavaScript functionality to make our lightbox interactive.

Opening the Lightbox

DIY Homemade Photography Light Box Step by Step Tutorial
DIY Homemade Photography Light Box Step by Step Tutorial
24 Brilliant DIY Light Box Ideas for Photography
24 Brilliant DIY Light Box Ideas for Photography
Easy Light Box
Easy Light Box
How to Build a Lightbox for Product Photos | Pixc Blog
How to Build a Lightbox for Product Photos | Pixc Blog
DIY Tutorial Light Box for Bloggers - Tips from a Typical Mom
DIY Tutorial Light Box for Bloggers - Tips from a Typical Mom
Creating a DIY lightbox
Creating a DIY lightbox
Quick & Simple DIY Light Table for All-Ages Learning Fun
Quick & Simple DIY Light Table for All-Ages Learning Fun
Foldable Homemade Light Box
Foldable Homemade Light Box
DIY dollar tree product light box
DIY dollar tree product light box
DIY light box
DIY light box
DIY Photography Light Box from a Cardboard Box, Walmart LED Desk Lamps
DIY Photography Light Box from a Cardboard Box, Walmart LED Desk Lamps
25 DIY Light Box Ideas For Photography
25 DIY Light Box Ideas For Photography
How to Make an LED Light Box
How to Make an LED Light Box
DIY Lightbox Tutorial
DIY Lightbox Tutorial
DIY  cheap photo light box made out of cardboard
DIY cheap photo light box made out of cardboard
How to Make a DIY Light Box for Just a Few Dollars
How to Make a DIY Light Box for Just a Few Dollars
Tips on Taking Great Photos
Tips on Taking Great Photos
How to Create a DIY Portable Light Box for Tracing, Art, and Hand Lettering
How to Create a DIY Portable Light Box for Tracing, Art, and Hand Lettering

First, we'll add an event listener to our button that opens the lightbox when clicked:

```javascript document.getElementById('lightbox-btn').addEventListener('click', function() { document.getElementById('lightbox-overlay').classList.remove('hidden'); document.getElementById('lightbox-img').src = 'path/to/your/image.jpg'; }); ```

Replace `'path/to/your/image.jpg'` with the actual path to the image you want to display in the lightbox.

Closing the Lightbox

Next, we'll add an event listener to the close button that closes the lightbox when clicked:

```javascript document.getElementById('close-btn').addEventListener('click', function() { document.getElementById('lightbox-overlay').classList.add('hidden'); }); ```

We're also using the `classList.add()` and `classList.remove()` methods to toggle the `hidden` class on the overlay, which shows and hides the lightbox.

Escaping the Lightbox

Finally, let's add the ability to close the lightbox by pressing the Escape key:

```javascript document.addEventListener('keydown', function(event) { if (event.key === 'Escape' && !document.getElementById('lightbox-overlay').classList.contains('hidden')) { document.getElementById('lightbox-overlay').classList.add('hidden'); } }); ```

This code listens for the `keydown` event, checks if the Escape key was pressed, and if the lightbox is currently open, it closes the lightbox.

And there you have it! A simple, functional lightbox that you can use to display images on your website or application. This lightbox can be further customized and enhanced with additional features, such as navigation buttons, captions, and more. Happy coding!