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.
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, itstype, 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 ( |
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
videochooses the source:html5for self-hosted video files, oryoutubefor videos hosted on YouTube. Videos are played with lightGallery.
Features
The Justified Gallery module for J1 provides the following key features:
| Name | Description |
|---|---|
| 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. |
| Every image keeps its original shape. Landscape and portrait photos can be mixed freely without being cropped. |
| When the browser window is resized, the gallery rebuilds itself to fit the new width. |
| An optional caption can appear over an image when the mouse is over it. |
| A gallery can open its images or videos in a lightbox. J1 supports PhotoSwipe ( |
| The same module builds galleries of photos, of self-hosted HTML5 videos, or of YouTube videos. |
| 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:
-
reads the merged settings for every gallery from the three YAML files described above (defaults ← control ← media),
-
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), -
waits until the page is ready and the gallery’s HTML has been inserted,
-
starts Justified Gallery for every gallery whose
enabledkey istrue, passing the keys from that gallery’sgallery_optionsblock, -
once the layout is finished (the
jg.completeevent), attaches the lightbox on top of the gallery. The kind of lightbox (PhotoSwipe, Lightbox2 or lightGallery) is chosen bylightbox.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 |
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:
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: nojustifyThumbnails
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:
|
The table below lists the options you will use most often.
| Property | Default | Description | ||
|---|---|---|---|---|
| 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. | ||
| 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.
| ||
| 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. | ||
| 1 | The space between images, in pixels. | ||
| -1 | The size of the border around the whole gallery. A negative value uses the same size as | ||
| nojustify | What to do with the last row when it cannot fill the width. Use | ||
| true | Show a caption over an image when the mouse is over it. | ||
| false | Show the photos in a random order. | ||
| false | Show only some of the entries. Can be:
| ||
| false | Sort the photos before showing them. Use false for no sorting, or a comparator function (see Array.prototype.sort()). | ||
| 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 |
|---|---|---|
| 0.90 | Controls when the last row is justified even though |
| 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 |
| 500 | How long the image fade-in takes, in milliseconds. Ignored when |
| { 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 |
| true | Wait for the thumbnails to load before building the layout. If your thumbnails have |
| a | Which direct children of the gallery are treated as entries. For performance reasons, entries must be direct children of the gallery element. |
| '> img', | Given an entry, how to find its thumbnail image inside it. |
| /\.[^.\\/]+$/ | A regular expression that matches an image’s file extension. It is used when rebuilding a filename (for example together with |
| 200 | How often (in milliseconds) the plugin checks whether the page width has changed. If it has, the layout is rebuilt. |
| 0 | How many pixels the width may change without rebuilding the gallery. |
| null | Rewrites the |
| null | Rewrites the |
| { } | Filename suffixes for different thumbnail sizes, so the plugin can pick the best size automatically. Off by default. See Loading different thumbnail sizes. |
| 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, |
| function | How the plugin fires its events ( |
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 |
| Command | Description |
|---|---|
| Calling |
| 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 |
| 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 |
| Event | Description |
|---|---|
| Fired when the whole gallery layout has been built. |
| Fired when the gallery has been resized (for example after the browser window changed width). |
| Fired each time a new row is ready. |