Code

js/views/gallery.js — the grid with a DocumentFragment

In the code column — js/views/gallery.js. renderGallery(pictures) clears the grid, puts all the cards into a DocumentFragment, and appends it in one go. (For now we pass pictures as an argument; in lesson 8 they'll come from central state.)

First, index.html needs a grid container:

<div id="gallery" class="grid"></div>

(class="grid" is a CSS grid; the styles ship in style.css.)

And main.js now renders the gallery instead of text:

import { renderGallery } from './views/gallery.js';
// ...inside load(), on success:
renderGallery(pictures);

The whole of renderGallery is three steps: clear (innerHTML = ''), build into a fragment, append once. The last line — revealOnScroll(...) — turns on the fade-in reveal (the next step).

Tip. gallery.innerHTML = '' at the start matters: without it, each renderGallery call would append cards to the old ones and they'd duplicate. The render model is simple: clear everything, then rebuild from data. It's less efficient than "surgical" updates, but far simpler and less bug-prone — and it's exactly what frameworks later optimize for you (the virtual DOM).