Icon indicating an imageLightbox

by Lokesh Dhakar

The original lightbox script.
Eight years later — still going strong!

Examples

Two individual images

Girl looking out people on beach Two men in bicycle jerseys sitting outside at table having coffee

Four image set

Golden Gate Bridge with San Francisco in distance Forest with mountains behind Bicyclist looking out over hill at ocean Small lighthouse at bottom with ocean behind

Getting started

  1. Download the latest version
    (Also available via Bower: bower install lightbox2 --save)

  2. Open up the zip file and take a peek at the barebones, working example that is included in the /examples folder.

  3. Ready to set Lightbox up on your page? Start by including the Lightbox Javascript and CSS. You can grab both these files from the /dist folder.

    <script src="path/to/lightbox.js"></script>
    <link href="path/to/lightbox.css" rel="stylesheet">
  4. Make sure jQuery, which is required by Lightbox, is also loaded.

    • If you already use jQuery on your page, make sure it is loaded before lightbox.js.
    • If you are not currently using jQuery, I've created a packaged file that includes both Lightbox and jQuery. Include dist/js/lightbox-plus-jquery.js instead of lightbox.js.
  5. Confirm that the four images loaded by lightbox.css are in the correct location. You can grab the images from the /dist/images folder.

Initialize with HTML

  • Add a data-lightbox attribute to any image link to enable Lightbox. For the value of the attribute, use a unique name for each image. For example:
    <a href="images/image-1.jpg" data-lightbox="image-1" data-title="My caption">Image #1</a>
    Optional: Add a data-title attribute if you want to show a caption.
  • If you have a group of related images that you would like to combine into a set, use the same data-lightbox attribute value for all of the images. For example:
    <a href="images/image-2.jpg" data-lightbox="roadtrip">Image #2</a>
    <a href="images/image-3.jpg" data-lightbox="roadtrip">Image #3</a>
    <a href="images/image-4.jpg" data-lightbox="roadtrip">Image #4</a>

Options

If you want to change any of the default options, call the option method.

<script>
    lightbox.option({
      'resizeDuration': 200,
      'wrapAround': true
    })
</script>
Option Default Description
alwaysShowNavOnTouchDevices false If true, the left and right navigation arrows which appear on mouse hover when viewing image sets will always be visible on devices which support touch.
fadeDuration 500 The time it takes for the Lightbox container and overlay to fade in and out, in milliseconds.
fitImagesInViewport true If true, resize images that would extend outside of the viewport so they fit neatly inside of it. This saves the user from having to scroll to see the entire image.
maxWidth   If set, the image height will be limited to this number, in pixels. Aspect ratio will not be maintained.
maxHeight   If set, the image width will be limited to this number, in pixels. Aspect ratio will not be maintained.
positionFromTop 50 The distance from top of viewport that the Lightbox container will appear, in pixels.
resizeDuration 700 The time it takes for the Lightbox container to animate its width and height when transition between different size images, in milliseconds.
showImageNumberLabel true If false, the text indicating the current image number and the total number of images in set (Ex. "image 2 of 4") will be hidden.
wrapAround false If true, when a user reaches the last image in a set, the right navigation arrow will appear and they will be to continue moving forward which will take them back to the first image in the set.

Browser support

Lightbox2 has been tested successfully in the following browsers:

  • Internet Explorer.
    The lightbox-plus-jquery.js file includes jQuery v2.x and supports IE 9+. If you want to support IE 6, 7, and 8, use your own copy of jQuery v1.x with lightbox.js.
  • Chrome
  • Safari
  • Firefox
  • iOS Safari
  • iOS Chrome
  • Android Browser
  • Android Chrome

License

Lightbox2 is licensed under The MIT License.

  • 100% Free. Lightbox is free to use in both commercial and non-commercial work.
  • Attribution is required.This means you must leave my name, my homepage link, and the license info intact. None of these items have to be user-facing and can remain within the code.

Help

Have a question about how to use Lightbox?

Follow the steps below to get help. Make sure you have read the documentation on this page and looked at the included example first.

  1. Search Stackoverflow to see if other people have run into the same issue you are having.
  2. If your issue is unique, then post a new question on Stackoverflow. Use the lightbox2 tag.

Do not use Github Issues to report personal support requests.

Found a bug?

If you find a bug, follow the instructions in the Contribution Guildelines to report the issue.

Tweet