Skip to content
Bright Themes

Table of contents for Ghost

A floating table of contents for long posts, built from the post's headings, that marks the section the reader is in.

  • Works with any Ghost theme

New to code injection?

It adds your own CSS and scripts to every page of your site, from Ghost Admin, without editing the theme.

Demo

Open Contents and choose a heading, then scroll: the section you're in is marked in the list.

When to use it

  • Long tutorials and documentation-style posts, where readers jump to the step they're stuck on.
  • Buyer's guides and roundups, so readers can go straight to the product or section they came for.
  • Reference posts readers come back to, such as a glossary or a list of settings, to look up one part.

When not to: If your theme has one built in, as many of ours do, use the theme's, so the post doesn't get two.

The code

.bt-toc,
.bt-toc-target {
  --bt-toc-top: 5rem;
}
.bt-toc {
  --bt-toc-width: 300px;
  --bt-toc-bg: #fff;
  --bt-toc-color: #222;
  --bt-toc-accent: var(--ghost-accent-color, #4433ff);
  position: fixed;
  top: var(--bt-toc-top);
  right: 1rem;
  z-index: 100;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
  font-size: 0.875rem;
  line-height: 1.4;
}
.bt-toc-target {
  scroll-margin-top: var(--bt-toc-top);
}
.bt-toc-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin: 0;
  padding: 8px 14px;
  border: 1px solid rgb(0 0 0 / 0.12);
  border-radius: 999px;
  background: var(--bt-toc-bg);
  color: var(--bt-toc-color);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 2px 10px rgb(0 0 0 / 0.08);
}
.bt-toc-toggle svg {
  flex: none;
  width: 18px;
  height: 18px;
}
.bt-toc-toggle[aria-expanded="true"] .bt-toc-icon-open,
.bt-toc-toggle[aria-expanded="false"] .bt-toc-icon-close {
  display: none;
}
.bt-toc-panel {
  box-sizing: border-box;
  width: min(var(--bt-toc-width), calc(100vw - 2rem));
  max-height: calc(100vh - var(--bt-toc-top) - 5rem);
  overflow-y: auto;
  padding: 12px 8px;
  border: 1px solid rgb(0 0 0 / 0.12);
  border-radius: 8px;
  background: var(--bt-toc-bg);
  color: var(--bt-toc-color);
  box-shadow: 0 4px 20px rgb(0 0 0 / 0.08);
}
.bt-toc-panel[hidden] {
  display: none;
}
.bt-toc-list {
  margin: 0;
  padding: 0;
  list-style: none;
}
.bt-toc-list li {
  margin: 0;
  padding: 0;
}
.bt-toc-link {
  display: block;
  padding: 4px 8px 4px 12px;
  border-left: 3px solid transparent;
  color: inherit;
  text-decoration: none;
}
.bt-toc-item-h3 .bt-toc-link {
  padding-left: 24px;
}
.bt-toc-link:hover {
  text-decoration: underline;
}
.bt-toc-link[aria-current="true"] {
  border-left-color: var(--bt-toc-accent);
  font-weight: 600;
}
.bt-toc-toggle:focus-visible,
.bt-toc-link:focus-visible {
  outline: 2px solid var(--bt-toc-accent);
  outline-offset: 2px;
}
@media print {
  .bt-toc {
    display: none;
  }
}

Add it to your site

  1. Copy the CSS and paste it in Settings › Advanced › Code injection › Site headerSettings › Advanced › Code injection › Site header (opens your Ghost Admin in a new tab), between <style> and </style>. Once per site.
  2. Copy the JS and paste it in Settings › Advanced › Code injection › Site footerSettings › Advanced › Code injection › Site footer (opens your Ghost Admin in a new tab), between <script> and </script>. Save.
  3. Open any post on your site to see it: it works on every post, with nothing to add to them.

How it works

The script collects the H2 and H3 headings of your post and builds a list of links to them. It adds a Contents button to the top right of the window; choosing it opens the list, and choosing it again, pressing Esc or choosing anywhere else closes it. As the reader scrolls, the section they’re in is marked with a bar and bold text, and screen readers hear it as the current one.

Ghost already gives every heading an ID, so the links work without editing your posts. Posts with fewer than two headings get no table of contents, and pages without post text are left alone. There’s no library to load.

Things to know

  • Your theme’s content class: the script looks for .gh-content, .post-content or .ghost-content, which covers most Ghost themes. If yours uses another class, add it to the CONTENT line at the top of the script.
  • Only on some posts: to show it on some posts only, put the CSS and the script in each post’s Post settings › Code injection instead of the site-wide one.
  • Dark themes: set --bt-toc-bg and --bt-toc-color to your theme’s colors.

Ghost has no table of contents of its own, but some themes include one, as many of ours do. Check your theme’s settings before adding this. For more on code injection, see Ghost code injection.

Customize

VariableDefaultWhat it changes
--bt-toc-top5remThe distance from the top of the window to the button, and the space kept above a heading after a jump, so a sticky header doesn't cover it.
--bt-toc-width300pxThe width of the open list.
--bt-toc-bg#fffThe background of the button and the list.
--bt-toc-color#222The text color of the button and the list.
--bt-toc-accentvar(--ghost-accent-color, #4433ff)The bar beside the current section and the focus ring. Defaults to your site's accent color.
Runs on every post, with no changes to your theme. Something not right? Ask us.

All widgets

Launch a better Ghost site

From a solo newsletter to a busy newsroom, there is a theme that fits. No code needed.