Directive Reference

The primary API is standard Sphinx image and figure markup. The .. lightbox:: directive is also available when directive-specific sizing is useful.

Common project setup:

lightbox_images = "explicit"
lightbox_figures = "all"
lightbox_default_class = "with-shadow"
lightbox_gallery = "document"
lightbox_gallery_wrap = False

Standard image And figure Directives

Add lightbox to the directive class list to make an ordinary Sphinx image clickable in HTML output:

.. image:: /images/photo.png
   :alt: Photo.
   :class: lightbox

Figures can also be configured as explicit, but the default is to wrap all figures:

.. figure:: /images/photo.png
   :alt: Photo.
   :class: with-border

   Figure caption.

   Longer explanatory legend text.

For figure nodes, the normal page caption remains in place and the caption and legend text are copied into the lightbox overlay. Plain image nodes do not have built-in Sphinx captions, so their overlays show only the image. Other classes, such as with-border, are preserved on the thumbnail and overlay image. The control classes lightbox and no-lightbox are not copied to the rendered image elements.

Use these settings in conf.py to decide what is transformed:

lightbox_images = "explicit"   # "explicit", "all", or "none"
lightbox_figures = "all"       # "explicit", "all", or "none"

When a policy is "all", opt out of an individual image or figure with no-lightbox:

.. image:: /images/icon.png
   :alt: Small icon.
   :class: no-lightbox

The class-based transform runs only for HTML builders. LaTeX/PDF and other builders keep the original Sphinx image or figure nodes.

lightbox_default_class sets classes applied to transformed HTML images in addition to any classes on the source directive. The default is "with-shadow". Set it to an empty string to disable default styling:

lightbox_default_class = ""

Directive Examples

Minimal:

.. lightbox:: /images/photo.png

Fully specified:

.. lightbox:: /images/archives/frozen-archives.png
   :alt: Frozen Archives screenshot with example archives.
   :caption: Example of the Archives page with a finalised archive.
   :class: with-border
   :percentage: 80 95

Independent PDF sizing:

.. lightbox:: /images/architecture.png
   :alt: System architecture diagram.
   :caption: High-level architecture overview.
   :percentage: 60 90
   :latex-width: 0.8

Here the HTML thumbnail is 60% wide with a 90% overlay, while the PDF figure is constrained to 80% of \linewidth.

Document-relative path:

.. lightbox:: ../images/detail-view.png
   :alt: Detail view of the settings panel.
   :percentage: 50

Node Architecture

The extension defines four custom docutils nodes:

LightboxContainer

Outer wrapper grouping all child nodes. In HTML: a <div class="lightbox-container">. In LaTeX: handles the complete figure environment and raises SkipNode to prevent children from rendering a second time.

LightboxTrigger

The thumbnail <img> and its <label> wrapper. Clicking opens the overlay via the CSS checkbox toggle. Suppressed outside HTML.

LightboxOverlay

The full-size image, caption paragraph, close button, and backdrop label. Suppressed outside HTML; the container visitor handles LaTeX output directly.

LightboxCollector

A wrapper around a standard image node. In HTML, it is present in the doctree during Sphinx’s read phase so that ImageCollector registers the file and copies it to _images/; its visitor then raises SkipNode so it is not rendered. In LaTeX, the container visitor handles output and skips the children. In epub, text, man, and texinfo builders, the collector’s child image supplies the plain image fallback.

Each node has visitor function pairs registered for html, latex, epub, text, man, and texinfo builders.

Content Security Policy (CSP)

The lightbox extension uses an external JavaScript file (lightbox.js) for keyboard activation, Escape-to-close, gallery navigation, and focus management. It does not inject inline JavaScript.

The generated HTML still uses inline styles for thumbnail width and overlay sizing. A strict CSP that disallows inline styles will block those declarations. If your documentation is served with CSP headers, allow inline styles for the documentation pages:

Content-Security-Policy: style-src 'self' 'unsafe-inline';