Basic Image Lightbox In Pure JavaScript

Category: Javascript , Modal & Popup | October 30, 2015
Views Total:2,619 views
Official Page:Go to website
Last Update:October 30, 2015


Basic Image Lightbox In Pure JavaScript


A very small JavaScript library for presenting your images in a fullscreen popup window like a modal. Click anywhere on the black area to close the lightbox.

How to use it:

Add images with captions specified using data-src attribute to your webpage.

<img class="thumb lightbox-photo" data-caption="First Photo Caption" src="1.jpg" />
<img class="thumb lightbox-photo" data-caption="Second Photo Caption" src="2.jpg" />

The core CSS styles for the lightbox.

.lightbox-wrapper {
  position: absolute;
  height: 100%;
  width: 100%;
  z-index: 1004; /*This is appended before the actual lightbox is faded in so that elements with
                    high z-index values (e.g. navbar) don't cover lightbox during fadein*/
.lightbox-photo:hover { cursor: pointer; }
.expanded {
  position: fixed;
  height: 95%;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 1002;

Style the fullscreen background overlay.

.overlay {
  position: fixed;
  top: 0;
  bottom: 0;
  background-color: black;
  height: 100%;
  width: 100%;
  opacity: 0.85;
  z-index: 1001;

Style the caption bar.

.caption {
  position: fixed;
  background: black;
  color: white;
  padding: 8px;
  border-radius: 5px;
  font-size: 14px;
  bottom: 3%;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1003;

Include the lightbox.js JavaScript file on the webpage when needed. That’s it.

<script src="lightbox.js"></script>

You Might Be Interested In:

One thought on “Basic Image Lightbox In Pure JavaScript

  1. George

    Hello, nice script you have there. i just wonder how can i add in your code a close button(x)? Thanks


Leave a Reply