Skip to content

Story maps in plain HTML

A story map is a map that moves as the reader scrolls: chapters of text and images scroll over a sticky map, and each chapter flies the camera somewhere, shows or hides layers, and can run actions (this is often called scrollytelling). On an HTML page, the <ml-scrollytelling> element shows a story written as one YAML file. It loads from a CDN: no npm, no bundler, no framework.

How to write the story file itself is on Writing a story file. The element is the same one the Astro <Scrollytelling> component renders, so a story file works unchanged in both.

  1. Create index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>My story map</title>
    <link
    rel="stylesheet"
    href="https://unpkg.com/maplibre-gl@6/dist/maplibre-gl.css"
    />
    <!-- Tell the browser where to find maplibre-gl -->
    <script type="importmap">
    { "imports": { "maplibre-gl": "https://unpkg.com/maplibre-gl@6/dist/maplibre-gl.mjs" } }
    </script>
    <!-- Registers <ml-scrollytelling> (and <ml-map>) -->
    <script
    type="module"
    src="https://unpkg.com/@maplibre-yaml/core@0.10/register.js"
    ></script>
    <style>
    body {
    margin: 0;
    font-family: system-ui, sans-serif;
    }
    </style>
    </head>
    <body>
    <ml-scrollytelling src="story.yaml"></ml-scrollytelling>
    </body>
    </html>
  2. Create story.yaml next to it

    type: scrollytelling
    id: three-cities
    theme: dark
    showMarkers: true
    config:
    mapStyle: "https://tiles.openfreemap.org/styles/liberty"
    center: [0, 20]
    zoom: 1.5
    layers:
    - id: cities
    type: circle
    source:
    type: geojson
    data:
    type: FeatureCollection
    features:
    - type: Feature
    properties: { name: Lagos }
    geometry: { type: Point, coordinates: [3.38, 6.52] }
    - type: Feature
    properties: { name: Lima }
    geometry: { type: Point, coordinates: [-77.04, -12.05] }
    - type: Feature
    properties: { name: Jakarta }
    geometry: { type: Point, coordinates: [106.85, -6.21] }
    paint:
    circle-radius: 8
    circle-color: "#f97316"
    circle-stroke-width: 2
    circle-stroke-color: "#ffffff"
    chapters:
    - id: lagos
    title: Lagos
    description: "<p>The largest city in Africa.</p>"
    center: [3.38, 6.52]
    zoom: 5
    - id: lima
    title: Lima
    description: "<p>On the Pacific coast of South America.</p>"
    center: [-77.04, -12.05]
    zoom: 5
    pitch: 40
    alignment: left
    - id: jakarta
    title: Jakarta
    description: "<p>On the north coast of Java.</p>"
    center: [106.85, -6.21]
    zoom: 5
    alignment: right
    onChapterEnter:
    - action: setPaintProperty
    layer: cities
    property: circle-color
    value: "#22c55e"
    footer: "<p>Made with maplibre-yaml.</p>"
  3. Serve the folder and open it

    Terminal window
    npx serve .

    Open the address it prints (for example http://localhost:3000) and scroll. The map flies from Lagos to Lima to Jakarta, a dot per chapter sits at the right edge, and the cities turn green on the last chapter.

Open the page from a web server, not by double-clicking it: a page opened as file:// can’t fetch story.yaml, and shows “Open this page through a local web server” instead. The chapter description is HTML, which is why the text is in <p> tags.

A complete story on a plain page, with a legend and every kind of chapter action, is examples/scrollytelling in the repository: index.html with story.yaml, and inline.html with the YAML in the page. To try a story without saving any files, open a story map in the playground.

The element is an ordinary block. Put content before and after it, and the map is sticky only while the story’s chapters are on screen:

<header>
<h1>Three cities</h1>
<p>Scroll down to start the story.</p>
</header>
<ml-scrollytelling src="story.yaml"></ml-scrollytelling>
<article>
<h2>After the story</h2>
<p>The page carries on as normal.</p>
</article>

To make the map shorter than the window, or stick it below a fixed header, see Styling.

On a GitHub Pages project site (https://<user>.github.io/<repo>/), add base="/<repo>/" to the element and leave the YAML as it is. The story’s src, the paths in its map and chapter image / video paths then load under the subpath. Details, and how to preview it on your computer: Publishing & subpaths.

<ml-scrollytelling src="story.yaml" base="/<repo>/"></ml-scrollytelling>

Children with a slot attribute (top-left, top-right, bottom-left, bottom-right) are placed in that corner of the story’s map, under the cards. A slot="legend" child fills the story’s pinned legend panel instead of the built-in entries.

<ml-scrollytelling src="story.yaml">
<p slot="bottom-left">Population figures: UN, 2024</p>
</ml-scrollytelling>

The element dispatches bubbling events: ml-scrollytelling:chapter when a chapter becomes active, ml-scrollytelling:action when a custom action runs, and ml-scrollytelling:error when the story can’t be loaded or validated. The chapter and action events carry map, the MapLibre map.

A custom action is how a chapter asks your script to do something. In the story:

chapters:
- id: jakarta
title: Jakarta
center: [106.85, -6.21]
zoom: 5
onChapterEnter:
- action: custom
options: { name: pulse }

On the page:

<script type="module">
const story = document.querySelector("ml-scrollytelling");
story.addEventListener("ml-scrollytelling:action", (event) => {
const { action, map } = event.detail;
if (action.options?.name === "pulse") {
map.setPaintProperty("cities", "circle-radius", 14);
}
});
story.addEventListener("ml-scrollytelling:chapter", (event) => {
console.log("now on", event.detail.chapterId);
});
// Or wait for the map directly
const map = await story.mapReady();
console.log("story map loaded at zoom", map.getZoom());
</script>

A chapter’s callback: value arrives as callback on the chapter event. The element also carries the active chapter’s id in data-active-chapter, and goToChapter(id) scrolls to a chapter.

debug outlines each chapter (green while active) so you can see where it starts and ends, and shows the story’s author hints, such as a chapter naming a layer that doesn’t exist, wherever the page is served. Without it they show only while you develop (localhost). quiet hides the on-page notes on a finished site; the console keeps them.

<ml-scrollytelling src="story.yaml" debug></ml-scrollytelling>

The element’s styles are added to the page for you, before your own stylesheets, so plain CSS overrides them:

/* A shorter map: the story then fits between other page content. */
ml-scrollytelling {
--ml-scrolly-map-height: 80vh;
}
/* Under a 64px fixed header: stick the map below it, and shorten it to fit. */
ml-scrollytelling {
--ml-scrolly-map-top: 64px;
--ml-scrolly-map-height: calc(100vh - 64px);
}
/* Wider, squarer cards */
ml-scrollytelling .chapter-content {
max-width: 520px;
border-radius: 4px;
}

Every class and custom property is listed under <ml-scrollytelling> styling hooks.

The map fills the screen and stays put while the chapters scroll; each chapter is a card. The wheel and touch scroll the story, not the map. The legend is pinned in a corner above the cards, and a story that can’t load shows one card listing each problem. The details (which chapter is active, the camera offset beside a card, the chapter rail, the legend, reduced motion) are in How a story behaves.

Problems every map shares are in Troubleshooting.

  • “Open this page through a local web server”. The page was opened as file://. Serve the folder (npx serve .) and open the address it prints.
  • The map scrolls away with the chapters. position: sticky stops working inside an ancestor with overflow: hidden, auto or scroll. Look for one on a wrapper around the element.
  • The map and text come out tiny on a phone. The page is missing <meta name="viewport" content="width=device-width, initial-scale=1">.
  • A PMTiles, FlatGeobuf, GeoParquet or COG file fails locally. The local server must support range requests: npx serve does, python3 -m http.server doesn’t (Range requests and CORS).

Instead of src, put the YAML in a <script type="text/yaml"> inside the element. Start the YAML on the line after the opening tag and keep every line at the same depth relative to the first: indentation is part of YAML. (type="application/yaml" works too, and type="application/json" takes the story written as JSON.)

<ml-scrollytelling>
<script type="text/yaml">
type: scrollytelling
id: inline-story
config:
mapStyle: "https://tiles.openfreemap.org/styles/liberty"
center: [0, 20]
zoom: 1.5
chapters:
- id: start
title: Start here
description: "<p>Scroll down.</p>"
center: [0, 20]
zoom: 1.5
- id: europe
title: Europe
center: [10, 50]
zoom: 3.5
</script>
</ml-scrollytelling>

A page script can also set the story from an object: document.querySelector("ml-scrollytelling").story = { type: "scrollytelling", … }.

Under Vite or another bundler, install @maplibre-yaml/core and maplibre-gl, and import @maplibre-yaml/core/register once in your entry file, with the worker setup that maplibre-gl 6 needs: Installation: using a bundler. The element then works in your HTML or templates. In React, Vue or Svelte, see React & other frameworks.

Story maps in Astro shows the same story file with the <Scrollytelling> component, validated at build time.

The runnable example is linked under the quick start.