Jekyll One

Fulltext Search

The Justified Gallery module arranges images in evenly aligned rows as a block. Each row in such a block has the same height, but the images inside it can have different widths. This is the layout style you see on photo sites like Flickr or Google Photos.

The photos you take are usually a mix of landscape and portrait orientations. A simple grid would have to crop them or leave gaps. JustifiedGallery solves this by keeping each image in its original shape and adjusting the widths so every row fills the page neatly.

The Justified Gallery API documentation pages are based on version 3.8.x for the current J1 template version of 2026.x. The idea of providing this documentation is not to simply copy the original Justified Gallery pages as duplicates. For better readability and usability, all pages are restructured and enhanced by code examples or improved description texts.

Overview

The Justified Gallery module arranges images in evenly aligned rows as a block. Each row has the same height, but the images inside a row can have different widths. Because every image keeps its original shape, landscape and portrait photos sit together without being cropped, and each row is stretched so it fills the full width of the page. This is the layout style you see on photo sites like Flickr or Google Photos.

Justified Gallery

The Justified Gallery API is the set of options, methods and events you can use from JavaScript to build and control a gallery. In the J1 Template you normally do not call this API yourself. Instead, you write a few YAML settings and the J1 Gallery adapter turns them into the correct JavaScript for every gallery on the page. Reading this manual is still useful, because the options you set in YAML use the same names as the API options described on the following pages.

In J1, a gallery is configured with three YAML files. They are merged together in the order shown below, and later files override earlier ones (this is called the inheritance chain). A key you leave out of a higher file is simply inherited from the file above it:

_data/modules/defaults/gallery.yml

The built-in default settings that apply to every gallery (for example the default rowHeight, the lightbox type, and the video player options). You normally do not edit this file.

_data/modules/gallery_control.yml

Your own settings, one entry per gallery (the gallery id, its type, the row height and gutters, the lightbox, and so on). Here you only need to write the values that differ from the defaults.

_data/modules/gallery_media.yml

The content of each gallery (the list of images or videos). Content is matched to a gallery by its id.

This three-file layout mirrors the Masonry module (masonry_control.yml / masonry_media.yml). The important idea is the split between configuration and content: gallery_control.yml says how a gallery should look and behave, gallery_media.yml says what it shows.

Every gallery has a type that decides what it shows:

image

A gallery of photos, optionally opened in a lightbox when clicked.

video

A gallery of videos. The extra key video chooses the source: html5 for self-hosted video files, or youtube for videos hosted on YouTube. Videos are played with lightGallery.

Features

The Justified Gallery module for J1 provides the following key features:

Name Description

Justified rows

Images are placed into rows of equal height. Each row is stretched to fill the full page width, so there are no ragged edges on the right.

Keeps aspect ratio

Every image keeps its original shape. Landscape and portrait photos can be mixed freely without being cropped.

Responsive

When the browser window is resized, the gallery rebuilds itself to fit the new width.

Captions

An optional caption can appear over an image when the mouse is over it.

Lightbox support

A gallery can open its images or videos in a lightbox. J1 supports PhotoSwipe (ps), Lightbox2 (lb) and lightGallery (lg).

Image and video galleries

The same module builds galleries of photos, of self-hosted HTML5 videos, or of YouTube videos.

CSS filters

Optional CSS effects (grayscale, contrast, brightness) can be applied to the gallery images.

Initialization

When the gallery’s HTML is on the page, Justified Gallery is started by calling justifiedGallery() on the gallery element and passing the options you want:

// init Justified Gallery
$('#myGallery').justifiedGallery({
  rowHeight: 150,
  margins:   1,
  lastRow:  'nojustify'
});

You can also react to the moment the layout is finished by listening for the jg.complete event:

$('#myGallery')
  .justifiedGallery({ rowHeight: 150 })
  .on('jg.complete', function () {
    // the gallery layout is ready
  });

Initialisation in J1 Template

In a J1 site you do not write the call above yourself. The J1 Gallery adapter (gallery.js) builds it for you from your YAML configuration.

The adapter:

  1. reads the merged settings for every gallery from the three YAML files described above (defaults ← control ← media),

  2. loads the HTML for each gallery on demand using AJAX. The HTML is not written into the page directly; it is generated into a data file and fetched from the path set by xhr_data_path (default /assets/data/gallery),

  3. waits until the page is ready and the gallery’s HTML has been inserted,

  4. starts Justified Gallery for every gallery whose enabled key is true, passing the keys from that gallery’s gallery_options block,

  5. once the layout is finished (the jg.complete event), attaches the lightbox on top of the gallery. The kind of lightbox (PhotoSwipe, Lightbox2 or lightGallery) is chosen by lightbox.type.

You can still reach a running gallery from JavaScript. Justified Gallery stores its controller on the gallery element, so you can get it with jQuery using $('#<your_gallery_id>').data('jg.controller').

Getting Started

This page shows how Justified Gallery works with plain HTML and a small <script>. It is useful for understanding the options, even though in a J1 site you do not write this code by hand.

In J1 you configure a gallery through YAML, not through a <script> block. The options shown on this page (like rowHeight, lastRow and margins) are the same options you set under gallery_options in _data/modules/gallery_control.yml. The J1 Gallery adapter reads that YAML and calls justifiedGallery() for you. See the Initialisation in J1 Template section above.

Here we have a div, with the id basicExample, that holds a list of links pointing to full-size images. Inside each link there is a thumbnail. When Justified Gallery runs, the thumbnails are resized so they fill the whole row (this filling is called justification).

<div id="basicExample">
    <a href="path/to/image1.jpg">
        <img alt="caption for image 1" src="path/to/image1_thumbnail.jpg"/>
    </a>
    <a href="path/to/image2.jpg">
        <img alt="caption for image 2" src="path/to/image2_thumbnail.jpg"/>
    </a>
    ...
</div>

The most important option of the module is rowHeight (see Properties). Say you set a height of 160px — the algorithm then tries to build rows about that tall:

rowHeight

Justification may resize the images a little, so the real row height can end up slightly different from 160px. In other words, rowHeight is your preferred height (a lower bound). You can use the maxRowHeight option to put an upper limit on the row height; but remember that this option can crop images if they would have to be bigger to be justified.

The algorithm builds one row after another until it reaches the last one. That last row may not have enough images to fill the whole width. With the lastRow option you decide what happens: leave a blank space, justify the images anyway, or hide the last row if there would be too much empty space.

The next example uses smaller images (rowHeight: 70), does not justify the last row (lastRow: 'nojustify'), and uses a margin of 3px between images (margins: 3):

<div id="example" class="justified-gallery">
    <a href="path/to/image1.jpg">
        <img alt="caption for image 1" src="path/to/image1_thumbnail.jpg"/>
    </a>
    <a href="path/to/image2.jpg" title="Just in a dream Place">
        <img alt="caption for image 2" src="path/to/image2_thumbnail.jpg"/>
    </a>
    ...
</div>
<script>
    $('#example').justifiedGallery({
        rowHeight : 70,
        lastRow : 'nojustify',
        margins : 3
    });
</script>

In a J1 site you would write the same settings as YAML instead. Note that in J1 the spacing key is called gutters (the adapter maps it to the library’s margins option for you):

# _data/modules/gallery_control.yml
- gallery:
    enabled: true
    id:      example
    type:    image
    gallery_options:
      rowHeight: 70
      gutters:   3        # J1 name for the library option "margins"
      lastRow:   nojustify

Thumbnails

If your server stores the same image in several sizes (for example a small thumbnail for phones and a bigger one for retina screens), the plugin can load the best available size automatically, so the images always look sharp.

To switch this on you set sizeRangeSuffixes. For example, to match the Flickr suffixes you would use:

sizeRangeSuffixes: {
    100 : '_t', // used for images that are less than 100px on the longest side
    240 : '_m', // used for images between 100px and 240px on the longest side
    320 : '_n', // ...
    500 : '',
    640 : '_z',
    1024 : '_b' // used for images that are more than 640px on the longest side
}

The plugin then works out the path of a larger or smaller thumbnail just by changing the filename suffix. For example, from a thumbnail named path/to/image1_t.jpg it can load path/to/image1_b.jpg.

By default this feature is off — the plugin only uses the thumbnail you provide in the HTML. That default looks like this:

sizeRangeSuffixes: { }

Properties

Properties (also called options) control how a gallery is built and how it behaves. You set them once, when the gallery is created. In J1 you write them as YAML under the gallery_options key of a gallery in gallery_control.yml; the adapter passes them on to Justified Gallery.

Two small J1 details to keep in mind:

  • In J1 the spacing between images is written as gutters, not margins. The adapter maps gutters to the library option margins for you.

  • The library default for rowHeight is 120, but the J1 default (set in gallery.yml) is 150. A gallery that does not set rowHeight gets 150.

The table below lists the options you will use most often.

Property Default Description

rowHeight

150

The preferred height of each row, in pixels. Justification may change the real height a little, so treat this as your target height rather than an exact value.

maxRowHeight

false

An upper limit for the row height. Use false (or a negative value) to turn it off. A number sets the limit in pixels (e.g. 200). A percentage string sets it relative to rowHeight (e.g. "200%" means a row can be at most 2 * rowHeight).

This option can crop images if they would need to be taller to be justified.

maxRowsCount

0

Limits how many rows are shown. Extra rows are hidden; if the page is made wider or narrower, more or fewer images fit and the number shown changes. 0 means no limit. This option does not make images smaller to fit more rows.

margins (J1: gutters)

1

The space between images, in pixels.

border

-1

The size of the border around the whole gallery. A negative value uses the same size as margins; 0 disables the border; any other number sets it in pixels.

lastRow

nojustify

What to do with the last row when it cannot fill the width. Use 'justify' to stretch it anyway, or 'hide' to hide it. With 'nojustify' the images keep their size and are aligned left; you can also use 'center' or 'right'.

captions

true

Show a caption over an image when the mouse is over it.

randomize

false

Show the photos in a random order.

filter

false

Show only some of the entries. Can be:

  • false: no filtering (show everything).

  • a string: an entry is kept if entry.is(filter string) is true (see jQuery’s .is()).

  • a function: called with (entry, index, array); return true to keep the entry (see Array.prototype.filter).

sort

false

Sort the photos before showing them. Use false for no sorting, or a comparator function (see Array.prototype.sort()).

rtl

false

Right-to-left mode (lay the images out from right to left).

Advanced settings

These options are useful in special cases. Most galleries never need them.

Property Default Description

justifyThreshold

0.90

Controls when the last row is justified even though lastRow is nojustify. If row width (available space) is greater than this value (between 0 and 1), the row is justified anyway.

cssAnimation

true

Use CSS animations to fade the images in. With CSS animations it is easier to change the effect by overriding the CSS rules of Justified Gallery. When true, the two options below are ignored.

imagesAnimationDuration

500

How long the image fade-in takes, in milliseconds. Ignored when cssAnimation is true.

captionSettings

{ animationDuration: 500, visibleOpacity: 0.7, nonVisibleOpacity: 0.0 }

Fine control over captions: the fade duration (ms), the caption opacity when visible (mouse over), and the opacity when not visible. Ignored when cssAnimation is true.

waitThumbnailsLoad

true

Wait for the thumbnails to load before building the layout. If your thumbnails have width and height attributes you can set this to false to build the layout immediately; the images then appear one by one as they load.

selector

a

Which direct children of the gallery are treated as entries. For performance reasons, entries must be direct children of the gallery element.

imgSelector

'> img',
'> a > img',
'> svg',
'> a > svg'

Given an entry, how to find its thumbnail image inside it.

extension

/\.[^.\\/]+$/

A regular expression that matches an image’s file extension. It is used when rebuilding a filename (for example together with sizeRangeSuffixes). Change it only if you need to match a specific extension.

refreshTime

200

How often (in milliseconds) the plugin checks whether the page width has changed. If it has, the layout is rebuilt.

refreshSensitivity

0

How many pixels the width may change without rebuilding the gallery.

rel

null

Rewrites the rel attribute of every link to this value. Often used to group images for a lightbox.

target

null

Rewrites the target attribute of every link. For example, with '_blank' (and no lightbox) images open in a new tab.

sizeRangeSuffixes

{ }

Filename suffixes for different thumbnail sizes, so the plugin can pick the best size automatically. Off by default. See Loading different thumbnail sizes.

thumbnailPath

undefined

A function that returns the thumbnail path for a given size, for full control over which file is loaded. It receives the current path, the target width and height, and the image element. If this is set, sizeRangeSuffixes is ignored.

$("#myGallery").justifiedGallery({
  thumbnailPath: function (currentPath, width, height, image) {
    if (Math.max(width, height) < 250) {
      return currentPath.replace(/(.*)(_[a-z]+)(\..*)/, "$1_small$2");
    } else {
      return currentPath.replace(/(.*)(_[a-z]+)(\..*)/, "$1_medium$2");
    }
  }
});

triggerEvent

function

How the plugin fires its events (jg.complete, jg.resize, jg.rowflush). By default it calls $.trigger on the gallery element. You rarely need to change this.

Commands

Commands let you control a gallery after it has been created. You call a command by passing its name (a string) to justifiedGallery() on the same element:

$('#myGallery').justifiedGallery('norewind');

In J1 the adapter creates the gallery for you, so you usually do not call commands. If you do need to, first get the element and then call the command on it, for example after adding new images to the page. You can reach a running instance with $('#<your_gallery_id>').data('jg.controller').

Command Description

(options object)

Calling justifiedGallery() again with an options object (for example { rowHeight: 200 }) updates the settings of an existing gallery and lays it out again with the new values.

norewind

Run Justified Gallery again, but only justify the newest images (the ones added since the last run). Because only the last images are looked at, any filter, sort or randomize you set only affects those new images. This is much faster, but be aware it does not touch the images that are already placed.

destroy

Remove the Justified Gallery layout from the element. The images go back to their normal, un-justified state and the plugin’s data is cleared.

Events

Events let your own code react to things that happen inside the gallery, such as the layout being finished. You listen for an event with jQuery’s .on() on the gallery element:

$('#myGallery')
  .justifiedGallery({ rowHeight: 150 })
  .on('jg.complete', function (e) {
    // runs when the gallery layout is ready
  });

The J1 Gallery adapter uses the jg.complete event internally: once a gallery’s layout is ready, the adapter attaches the configured lightbox (PhotoSwipe, Lightbox2 or lightGallery). So in a normal J1 site you do not need to handle these events yourself.

Event Description

jg.complete

Fired when the whole gallery layout has been built.

jg.resize

Fired when the gallery has been resized (for example after the browser window changed width).

jg.rowflush

Fired each time a new row is ready.