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.
Quick start
Section titled “Quick start”-
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><linkrel="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>) --><scripttype="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> -
Create
story.yamlnext to ittype: scrollytellingid: three-citiestheme: darkshowMarkers: trueconfig:mapStyle: "https://tiles.openfreemap.org/styles/liberty"center: [0, 20]zoom: 1.5layers:- id: citiestype: circlesource:type: geojsondata:type: FeatureCollectionfeatures:- type: Featureproperties: { name: Lagos }geometry: { type: Point, coordinates: [3.38, 6.52] }- type: Featureproperties: { name: Lima }geometry: { type: Point, coordinates: [-77.04, -12.05] }- type: Featureproperties: { name: Jakarta }geometry: { type: Point, coordinates: [106.85, -6.21] }paint:circle-radius: 8circle-color: "#f97316"circle-stroke-width: 2circle-stroke-color: "#ffffff"chapters:- id: lagostitle: Lagosdescription: "<p>The largest city in Africa.</p>"center: [3.38, 6.52]zoom: 5- id: limatitle: Limadescription: "<p>On the Pacific coast of South America.</p>"center: [-77.04, -12.05]zoom: 5pitch: 40alignment: left- id: jakartatitle: Jakartadescription: "<p>On the north coast of Java.</p>"center: [106.85, -6.21]zoom: 5alignment: rightonChapterEnter:- action: setPaintPropertylayer: citiesproperty: circle-colorvalue: "#22c55e"footer: "<p>Made with maplibre-yaml.</p>" -
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.
Common tasks
Section titled “Common tasks”In the middle of a page
Section titled “In the middle of a page”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.
Sites under a subpath (GitHub Pages)
Section titled “Sites under a subpath (GitHub Pages)”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>Corner slots
Section titled “Corner slots”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>Reacting to the story from JavaScript
Section titled “Reacting to the story from JavaScript”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.
Check chapter positions with debug
Section titled “Check chapter positions with debug”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>Styling
Section titled “Styling”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.
How a story behaves
Section titled “How a story behaves”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.
Reference
Section titled “Reference”<ml-scrollytelling>reference: every attribute (src,base,debug,quiet), property, method, event, slot and styling hook.mapElementgives you the story’s own<ml-map>, whose methods and events work as on any map.- Writing a story file: chapters, camera moves, layers, actions, the rail and the legend.
- YAML reference for stories: every key, with its type and default.
Troubleshooting
Section titled “Troubleshooting”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: stickystops working inside an ancestor withoverflow: hidden,autoorscroll. 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 servedoes,python3 -m http.serverdoesn’t (Range requests and CORS).
Alternatives
Section titled “Alternatives”The story inline, in one file
Section titled “The story inline, in one file”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: scrollytellingid: inline-storyconfig: mapStyle: "https://tiles.openfreemap.org/styles/liberty" center: [0, 20] zoom: 1.5chapters: - 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", … }.
With a bundler
Section titled “With a bundler”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.
In an Astro site
Section titled “In an Astro site”Story maps in Astro shows the same story file
with the <Scrollytelling> component, validated at build time.
Moved sections
Section titled “Moved sections”Runnable example
Section titled “Runnable example”The runnable example is linked under the quick start.