Lightbox

by Lokesh Dhakar

The original lightbox script

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 a zip of the latest release (or any previous one) from the Github Releases page.

    Or install using npm:

    npm 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 CSS and Javascript. You can grab both these files from the /dist folder.

    • Include the CSS at the top of your page in your <head> tag:
    • Include the Javascript at the bottom of your page before the closing </body> tag:
  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. jQuery 1.7 or greater is required, and since the effects module is utilized, the jQuery slim build is not supported.
    • 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

  • Single images. 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:
    Optional:
    • Add a data-title attribute if you want to show a caption.
    • Add a data-alt attribute if you want to set the alt attribute of the linked image.
  • Image sets. 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:

Options

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

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.
albumLabel "Image %1 of %2" The text displayed below the caption when viewing an image set. The default text shows the current image number and the total number of images in the set.
disableScrolling false If true, prevent the page from scrolling while Lightbox is open. This works by settings overflow hidden on the body.
fadeDuration 600 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.
imageFadeDuration 600 The time it takes for the image to fade in once loaded, in milliseconds.
maxWidth   If set, the image width will be limited to this number, in pixels. Aspect ratio will not be maintained.
maxHeight   If set, the image height 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, please file a Github issue.