Skip to content
Bright Themes

How to add an image lightbox in Ghost

Let readers open any image or gallery photo in your Ghost posts full screen, with a small script and the fslightbox library.

Norbert2 min read

A lightbox opens an image in an overlay on top of the page, with the background dimmed, so readers can see it at full size. Ghost doesn’t add one to posts by itself; some themes include one, several of ours as a setting (see for example the Auden docs). This tutorial adds fslightbox (opens in a new tab) to your theme: it’s plain JavaScript with no dependencies, small, and works without extra styling.

Tip

Only want one image to open larger? In the editor, give the image card a link to the image file itself. No code needed. For a lightbox without a library, see the image zoom widget.

What you need

  • A copy of your theme you can edit and upload again (Settings › Site › Theme › Change theme › Upload theme).
  • For step 1b: a theme build that bundles JavaScript with npm (for example with gulp). Otherwise use step 1a.

1a. Add fslightbox to the theme as a file

Download fslightbox (opens in a new tab), unzip it and copy fslightbox.js to assets/js/ in your theme. Then load it in default.hbs, before </body>:

Handlebars
<script defer src="{{asset "js/fslightbox.js"}}"></script>

defer loads the script without blocking the page and runs it once the page is parsed. The {{asset}} helper builds the file’s URL; it only works in theme files, not in code injection.

1b. Or bundle it with your theme’s JavaScript

If your theme builds its JavaScript with npm, install the package:

Terminal
npm install fslightbox

Then add it to the files your build combines. In a gulp setup, that’s the list of sources in the JavaScript task:

JavaScript
const { src, dest } = require('gulp');
const concat = require('gulp-concat');
const uglify = require('gulp-uglify');

function js() {
  return src([
    // … your other scripts
    'node_modules/fslightbox/index.js',
    'assets/js/main.js',
  ])
    .pipe(concat('app.min.js'))
    .pipe(uglify())
    .pipe(dest('assets/built/'));
}

On the next build, fslightbox is part of your theme’s bundle. Your gulpfile will look different; what matters is adding node_modules/fslightbox/index.js to its sources. (Our themes now build with Vite, described in the development docs.)

2. Connect the lightbox to your images

fslightbox opens links that have a data-fslightbox attribute. Ghost’s image and gallery cards don’t link their images, so a short script wraps each one in a link. Add it after the fslightbox script in default.hbs, or in Settings › Advanced › Code injection › Site footer (see How code injection works in Ghost):

HTML
<script>
  document.addEventListener('DOMContentLoaded', function () {
    const images = document.querySelectorAll(
      '.gh-content .kg-image-card img, .gh-content .kg-gallery-card img'
    );

    images.forEach(function (image) {
      if (image.closest('a')) return; // the image already has a link

      const link = document.createElement('a');
      link.setAttribute('href', image.src);
      link.setAttribute('data-fslightbox', 'gallery');
      link.setAttribute('aria-label', 'Open image in a lightbox');
      image.parentNode.insertBefore(link, image);
      link.appendChild(image);
    });

    refreshFsLightbox();
  });
</script>

What it does:

  • It finds every image in image cards and gallery cards in the post. .gh-content is the post content wrapper in many themes; use your theme’s class if it differs.
  • It skips images you already linked in the editor.
  • It wraps each image in a link to the image file, with data-fslightbox="gallery". All links with the same value form one gallery, so readers can step through every image in the post.
  • refreshFsLightbox() tells fslightbox to pick up the new links. It runs on DOMContentLoaded, after the deferred fslightbox script has loaded.

3. Upload the theme

Zip the theme and upload it in Settings › Site › Theme › Change theme › Upload theme. Keep the zip name the same as the installed theme so your design settings stay (why).

The result

Select any image in a post and it opens full screen, with arrows to move through the other images in the post. See it on the Auden style guide (opens in a new tab).

Related posts

More on design

Get the Bright Themes newsletter

New tutorials and explainers, what changed in Ghost, and our theme releases. No spam, unsubscribe anytime.

Get the Bright Themes newsletter

New tutorials and explainers, what changed in Ghost, and our theme releases. No spam, unsubscribe anytime.