Documentation
Outline reads the headings of an article and builds a table of contents from them. As the reader scrolls, the current section highlights itself, and an optional progress bar shows how far through the page they are. One JavaScript file, one stylesheet, no dependencies.
Install
Copy outline.js and outline.css into your project and include them once per page.
<link rel="stylesheet" href="outline.css">
<script src="outline.js"></script>
Markup
Put an empty container where the table of contents should appear, and tell it which element holds the content. The headings inside that element become the list.
<aside>
<div class="outline" data-outline data-target="#article"></div>
</aside>
<article id="article">
<h2>First section</h2>
...
</article>
Every element with data-outline is set up on page load. If you add content later, call Outline.autoInit() again or create the instance yourself.
Options
| Option | Default | Notes |
|---|---|---|
target | null | Selector for the content element. Without it, the nearest article is used, then the body. |
levels | ['h2','h3'] | Which headings become entries. h3 entries nest under the previous h2. |
offset | 16 | Pixels between the top of the viewport and the heading after a click, and the point used to decide the active section. |
smooth | true | Smooth scrolling on click. Skipped for visitors who ask for reduced motion. |
highlight | true | Mark the current section with the active class and aria-current. |
progress | null | Selector or element for the progress bar. The plugin sets its width and aria-valuenow. |
numbered | false | Prefix each entry with its number. |
collapse | true | Wrap the list in a <details> element that is open on wide screens and closed on small ones. |
collapseBreakpoint | 900 | Width in pixels where the collapsed state switches. |
minHeadings | 2 | Hide the whole table of contents when the content has fewer headings than this. |
slug | true | Give headings without an id a slug built from their text, unique on the page. |
activeClass | 'is-active' | Class added to the current entry. |
label | 'Contents' | Text of the collapse summary, or the title when collapse is off. |
Data attributes
Options can be given as data attributes, or as a JSON object in data-outline.
<div class="outline" data-outline data-target="#article" data-levels="h2,h3" data-offset="80" data-progress="#progress"></div>
<div class="outline" data-outline='{"target":"#article","numbered":true,"collapse":false}'></div>
API
var toc = new Outline(document.querySelector('#toc'), {
target: '#article',
progress: '#progress',
offset: 80
});
toc.refresh(); // rebuild after adding or removing headings
toc.destroy(); // remove listeners and the elements it created
Outline.autoInit('[data-outline]');
Outline.version; // '1.0.0'
Events
An outline:change event fires on the container when the active section changes. It bubbles.
document.querySelector('#toc').addEventListener('outline:change', function (event) {
console.log(event.detail.id, event.detail.index);
});
Styling
Colours and sizing are CSS custom properties on the .outline element.
| Property | Default | What it styles |
|---|---|---|
--outline-accent | #2f6df6 | Active entry and progress bar colour. |
--outline-text | #101418 | Hover and title colour. |
--outline-muted | #667085 | Normal entry colour. |
--outline-line | #e4e7ec | Borders and the left rail. |
--outline-font-size | 14px | Base size of the list. |
--outline-sticky-top | 24px | Distance from the top when the list is sticky. |
Accessibility
- The list is a real
<ol>of links, so it is navigable with the keyboard and announced as a list. - The current entry gets
aria-current="true". - The progress bar gets
role="progressbar"witharia-valuemin,aria-valuemaxand a livearia-valuenow. - Entries have a visible focus ring through
:focus-visible. - Smooth scrolling is skipped when the visitor has
prefers-reduced-motionset. - The mobile summary is a native
<details>element, so it works without JavaScript inside it.
Using it with a CMS or a framework
WordPress
Enqueue the two files in your theme, add the container above the content, and point data-target at your content wrapper.
function outline_assets() {
wp_enqueue_style('outline', get_template_directory_uri() . '/outline/outline.css');
wp_enqueue_script('outline', get_template_directory_uri() . '/outline/outline.js', array(), null, true);
}
add_action('wp_enqueue_scripts', 'outline_assets');
React, Vue, Svelte
Render the container and the content, then create the instance in an effect or on mount, and call destroy() on unmount. Call refresh() after the content changes.
useEffect(() => {
const toc = new Outline(ref.current, { target: '#article' });
return () => toc.destroy();
}, [content]);
Limits, honestly
- It builds from headings in the page. It does not read your CMS, and it does not parse a rendered table of contents out of the text.
- The scroll spy listens to scroll and resize. On a page with thousands of headings the per-frame loop gets heavier; keep the list to the headings a reader would actually use.
- It does not add anchors or copy-link buttons to your headings. It uses the ids your headings already have, and only generates them when they have none.
- The progress bar measures the content element, not the whole page, so a long footer will not inflate it.
- Modern browsers only (Chrome, Edge, Firefox, Safari and their mobile versions). Internet Explorer is not supported.
- No server side component, no database, no tracking.
Changelog
1.0.0 - first release. Automatic list from headings, nested sub-entries, scroll spy with aria-current, reading progress, smooth scrolling with offset, mobile collapse, numbering, refresh and destroy, events and data attribute setup.