DocumentationLayouts

Layouts

Choose masonry, grid, justified or mosaic. Set columns, gap, breakpoints that follow the grid width, row height and mosaic spans, with copy-paste examples.

The layout option decides how items are placed. All four layouts animate when items move, work with every skin and read the width of the grid element, not the window.

LayoutLookUses
masonryColumns of equal width. Each item goes to the shortest column. Items keep their own ratio.columns, gap, breakpoints
gridEqual columns and rows of the same height.columns, gap, aspectRatio
justifiedRows of equal height that fill the full width (the Flickr look).rowHeight, lastRow, gap
mosaicA dense wall of cells of different sizes.columns, gap, aspectRatio, mosaicPattern, item spans

The default is masonry. Change it with an attribute or an option:

html
<media-boxes layout="grid" columns="4" gap="16">
  <!-- items -->
</media-boxes>
js
grid.options = { layout: 'justified', rowHeight: 220 };

Changing layout on a running grid re-lays the items out and animates them to their new places. Try it in the effects playground.

Columns #

columns is a whole number of 1 or more, or 'auto'.

html
<media-boxes layout="masonry" columns="3"> ... </media-boxes>
<media-boxes layout="grid" columns="auto" min-column-width="240"> ... </media-boxes>

With 'auto' the number of columns is as many as fit while keeping every column at least minColumnWidth pixels wide (default 260). The formula is max(1, floor((width + gap) / (minColumnWidth + gap))). No breakpoints are derived for auto, because the count already follows the width.

The justified layout ignores columns. See masonry columns for a live slider.

Gap #

gap is the space between items in pixels. A single number sets both directions. An array sets [rowGap, columnGap].

html
<media-boxes gap="16"> ... </media-boxes>
<media-boxes gap="24 12"> ... </media-boxes>
js
grid.options = { gap: [24, 12] }; // 24px between rows, 12px between columns

Breakpoints #

Breakpoints are measured against the width of the <media-boxes> element, not the viewport. A grid in a narrow sidebar gets its small-screen settings even on a wide monitor.

Derived breakpoints #

If you do not set breakpoints, they are derived from columns and gap:

Grid widthColumnsGap
480px or less2 when columns is 4 or more, otherwise 1at most 10px
481 to 720pxmin(columns, 2), or 3 when columns is 6 or moreat most 12px
721 to 1024pxmin(columns, 3), or 4 when columns is 6 or moreyour gap
Wideryour columnsyour gap

So columns="4" gives 2 columns on a phone, 2 on a small tablet, 3 on a laptop sidebar and 4 when there is room. For most sites you never need to write breakpoints.

Your own breakpoints #

Set breakpoints to an array. The first entry whose maxWidth is greater than or equal to the grid width wins, after sorting by maxWidth. Values you leave out fall back to the top-level options.

js
grid.options = {
  layout: 'grid',
  columns: 5,
  gap: 20,
  breakpoints: [
    { maxWidth: 420, columns: 1, gap: 8 },
    { maxWidth: 700, columns: 2 },
    { maxWidth: 1100, columns: 3, gap: [16, 12] },
  ],
};

A breakpoint can set columns, gap, rowHeight, aspectRatio and minColumnWidth. A breakpoint list you provide is used exactly as given, and an empty list [] turns breakpoints off, so the grid keeps columns at every width.

Grid: aspect ratio #

The grid layout crops every image to the same cell shape. aspectRatio is width divided by height: a number (1.5), or a string ('3/2', '16:9'). The default is 4 / 3.

html
<media-boxes layout="grid" columns="3" aspect-ratio="1/1" skin="plain"> ... </media-boxes>

With a card skin the text block sits below the cell, and all cards in a row end on the same line. The same aspectRatio sets the cell shape in mosaic.

Justified: row height and last row #

justified places items in rows of equal height, as wide as the grid. It adds items to a row until the row, scaled to full width, would be no taller than 1.25 times rowHeight, and then scales it to fit exactly.

html
<media-boxes layout="justified" row-height="220" gap="8" skin="hover"> ... </media-boxes>

lastRow decides what happens to the final, usually incomplete, row:

ValueResult
'left' (default)The row keeps the target height and sits at the start.
'justify'The row is stretched to the full width, but never above 1.5 times rowHeight. Beyond that it is laid out as left.
'hide'An incomplete last row is hidden.
js
grid.options = { layout: 'justified', rowHeight: 240, lastRow: 'justify' };

In justified the text content is always drawn over the image, so use the hover or overlay skin. See the justified gallery demo. lastRow has no attribute, so set it in JSON or JavaScript.

Mosaic: spans and the automatic pattern #

mosaic places items on a fixed grid of columns cells and packs them densely. An item can span several cells with data-cols and data-rows:

html
<media-boxes layout="mosaic" columns="4" gap="8" aspect-ratio="1/1" skin="overlay">
  <article class="mb-item" data-cols="2" data-rows="2"> ... </article>
  <article class="mb-item"> ... </article>
  <article class="mb-item" data-cols="2"> ... </article>
</media-boxes>

Spans are clamped to the number of columns. In JSON items use span: { cols: 2, rows: 2 }.

Items without an explicit span follow an automatic pattern, mosaicPattern: 'auto' (the default). It repeats two bands of two rows. Each band holds one 2 by 2 lead item and the 1 by 1 cells that fill the rest, and the lead alternates between the left and the right. With 2 columns the lead is a 2 by 1 strip, and with 1 column everything is 1 by 1.

Set mosaicPattern: 'none' to make every item 1 by 1 unless it has its own span. An explicit data-cols or data-rows always wins over the pattern. See the mosaic demo.

Masonry also honours data-cols: an item with data-cols="2" spans two columns when it fits. data-rows is used by mosaic only, and grid ignores spans.

Choosing a layout #