← Back to the demo

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

OptionDefaultNotes
targetnullSelector 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.
offset16Pixels between the top of the viewport and the heading after a click, and the point used to decide the active section.
smoothtrueSmooth scrolling on click. Skipped for visitors who ask for reduced motion.
highlighttrueMark the current section with the active class and aria-current.
progressnullSelector or element for the progress bar. The plugin sets its width and aria-valuenow.
numberedfalsePrefix each entry with its number.
collapsetrueWrap the list in a <details> element that is open on wide screens and closed on small ones.
collapseBreakpoint900Width in pixels where the collapsed state switches.
minHeadings2Hide the whole table of contents when the content has fewer headings than this.
slugtrueGive 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.

PropertyDefaultWhat it styles
--outline-accent#2f6df6Active entry and progress bar colour.
--outline-text#101418Hover and title colour.
--outline-muted#667085Normal entry colour.
--outline-line#e4e7ecBorders and the left rail.
--outline-font-size14pxBase size of the list.
--outline-sticky-top24pxDistance from the top when the list is sticky.

Accessibility

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

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.