DocumentationGetting started

Getting started

Install Media Boxes with a script tag or an ES module, then build your first grid from HTML, a JSON block or JavaScript. Includes a full copy-paste page.

Media Boxes is one custom element, <media-boxes>. It has no dependencies, needs no build step and keeps your content in the page as ordinary HTML. This page installs it and builds a first grid three ways.

What you need #

FileWhat it is
media-boxes.cssThe styles. Load it with a <link> tag.
media-boxes.jsThe engine as a classic script. It registers <media-boxes> and creates the global MediaBoxes.
media-boxes.esm.jsThe same engine as an ES module, for import.

Copy the files to your site, for example into assets/media-boxes/. Use either media-boxes.js or media-boxes.esm.js, never both.

Install with a script tag #

This is the simplest setup and works on any page.

html
<link rel="stylesheet" href="assets/media-boxes/media-boxes.css">
<script src="assets/media-boxes/media-boxes.js" defer></script>

The script registers the element when it loads. Elements already in the page are upgraded as soon as the document is parsed, so the script tag can sit in the <head> with defer or at the end of the <body>.

Install as an ES module #

html
<link rel="stylesheet" href="assets/media-boxes/media-boxes.css">
<script type="module">
  import { version, DEFAULTS, renderItem } from './assets/media-boxes/media-boxes.esm.js';
  console.log('Media Boxes', version);
</script>

Importing the module registers <media-boxes>. The named exports are MediaBoxesElement, DEFAULTS, createLightbox, renderItem and version. With the classic script the same things live on window.MediaBoxes.

Your first grid, three ways #

All three produce the same kind of grid. Pick the one that fits how your page is made. The images in the examples are placeholders: point src and href at your own files (a small image for the grid and a large one for the lightbox).

1. From HTML markup #

Each item is an <article class="mb-item">. The a.mb-media link holds the thumbnail and points at the large image, which the lightbox opens.

html
<media-boxes id="work" layout="masonry" columns="3" gap="16" skin="card" filter="category" search>
  <article class="mb-item" data-id="lake" data-category="Nature">
    <a class="mb-media" href="images/lake-1600.jpg" data-mb-width="1600" data-mb-height="1067">
      <img src="images/lake-600.jpg" width="600" height="400" alt="A calm mountain lake" loading="lazy" decoding="async">
    </a>
    <div class="mb-content">
      <h3 class="mb-title">Mountain lake</h3>
      <p class="mb-text">Early light over still water.</p>
    </div>
  </article>

  <article class="mb-item" data-id="stair" data-category="Architecture">
    <a class="mb-media" href="images/stair-1600.jpg" data-mb-width="1600" data-mb-height="1067">
      <img src="images/stair-600.jpg" width="600" height="400" alt="A white concrete stair" loading="lazy" decoding="async">
    </a>
    <div class="mb-content">
      <h3 class="mb-title">White stair</h3>
      <p class="mb-text">A single flight cut into a pale wall.</p>
    </div>
  </article>
</media-boxes>

The attributes on <media-boxes> are options: filter="category" adds filter pills for the data-category terms and search adds a search box. See HTML attributes for the full list and the plain HTML demo for a complete page.

2. From a JSON block #

Put the options, and optionally the items themselves, in a <script type="application/json" data-mb-options> block inside the element. This is the format the WordPress plugin uses, and it is the easiest to generate from a database.

html
<media-boxes id="work">
  <script type="application/json" data-mb-options>
  {
    "layout": "masonry",
    "columns": 3,
    "skin": "card",
    "filters": [{ "group": "category" }],
    "search": true,
    "items": [
      {
        "id": "lake",
        "title": "Mountain lake",
        "text": "Early light over still water.",
        "terms": { "category": ["Nature"] },
        "thumb": { "src": "images/lake-600.jpg", "width": 600, "height": 400, "alt": "A calm mountain lake" },
        "media": { "type": "image", "src": "images/lake-1600.jpg", "width": 1600, "height": 1067 }
      },
      {
        "id": "stair",
        "title": "White stair",
        "text": "A single flight cut into a pale wall.",
        "terms": { "category": ["Architecture"] },
        "thumb": { "src": "images/stair-600.jpg", "width": 600, "height": 400, "alt": "A white concrete stair" },
        "media": { "type": "image", "src": "images/stair-1600.jpg", "width": 1600, "height": 1067 }
      }
    ]
  }
  </script>
</media-boxes>

The items array is turned into the same markup as example 1. Every field of an item is listed in Skins and captions. You can mix both: items written in HTML come first, then the ones from items.

3. From JavaScript #

Create the element, assign options, and add it to the page. Options assigned from JavaScript win over attributes and the JSON block.

html
<div id="mount"></div>
<script>
  // The engine script is loaded with defer, so wait until the element is defined
  customElements.whenDefined('media-boxes').then(() => {
    const grid = document.createElement('media-boxes');
    grid.id = 'work';
    grid.options = {
      layout: 'masonry',
      columns: 3,
      filters: [{ group: 'category' }],
      items: [
        {
          id: 'lake', title: 'Mountain lake', terms: { category: ['Nature'] },
          thumb: { src: 'images/lake-600.jpg', width: 600, height: 400, alt: 'A calm mountain lake' },
          media: { type: 'image', src: 'images/lake-1600.jpg', width: 1600, height: 1067 },
        },
      ],
    };
    document.getElementById('mount').append(grid);

    grid.addEventListener('mb:ready', () => console.log('ready', grid.items.length, 'items'));
  });
</script>

Add more items later with grid.addItems([...]). The API and events page lists every method.

A complete page #

Save this as index.html next to your assets and images folders. It uses a grid with filters, search and the lightbox, written as plain HTML.

html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>My portfolio</title>
  <link rel="stylesheet" href="assets/media-boxes/media-boxes.css">
  <script src="assets/media-boxes/media-boxes.js" defer></script>
  <style>
    body { margin: 0; padding: 32px; font-family: system-ui, sans-serif; }
    media-boxes { max-width: 1200px; margin: 0 auto; }
  </style>
</head>
<body>
  <media-boxes id="portfolio" layout="masonry" columns="3" gap="16" skin="card" filter="category" search>
    <article class="mb-item" data-id="lake" data-category="Nature">
      <a class="mb-media" href="images/lake-1600.jpg" data-mb-width="1600" data-mb-height="1067">
        <img src="images/lake-600.jpg" width="600" height="400" alt="A calm mountain lake" loading="lazy">
      </a>
      <div class="mb-content"><h3 class="mb-title">Mountain lake</h3><p class="mb-text">Early light over still water.</p></div>
    </article>
    <article class="mb-item" data-id="stair" data-category="Architecture">
      <a class="mb-media" href="images/stair-1600.jpg" data-mb-width="1600" data-mb-height="1067">
        <img src="images/stair-600.jpg" width="600" height="400" alt="A white concrete stair" loading="lazy">
      </a>
      <div class="mb-content"><h3 class="mb-title">White stair</h3><p class="mb-text">A single flight cut into a pale wall.</p></div>
    </article>
    <article class="mb-item" data-id="market" data-category="Travel, Nature">
      <a class="mb-media" href="images/market-1600.jpg" data-mb-width="1600" data-mb-height="1067">
        <img src="images/market-600.jpg" width="600" height="400" alt="A morning market" loading="lazy">
      </a>
      <div class="mb-content"><h3 class="mb-title">Morning market</h3><p class="mb-text">Fruit stalls before the crowd.</p></div>
    </article>
  </media-boxes>
</body>
</html>

Open it in a browser. Click a photo to open the lightbox, use the pills to filter and type in the search box.

What happens when the page loads #

  1. The element reads its options: defaults, then the JSON block, then the attributes, then el.options (later sources win).
  2. It reads the .mb-item children, builds the toolbar and footer if you asked for them, and lays the items out. There is no animation on the first layout.
  3. It sets data-ready on itself and fires mb:ready.

Each grid reads its own width, so the same page works in a sidebar, a tab or a full-width section.

Where to go next #