In short
A table of contents is a list of links placed at the top of long content, pointing to its sections through anchors: an id attribute on each heading and a #id link in the table of contents. Once the check is done, every guide or article longer than a few screens displays a table of contents in HTML, every H2 subheading (and H3 where useful) carries a readable, stable identifier, and an address such as https://example.com/guides/infusion-the-vert/#temperature-eau opens the page directly at the right section.
Why it matters
For readers. On long content, most visitors are looking for a specific piece of information. The table of contents shows them straight away whether the page answers it and takes them there in one click. The WCAG (Web Content Accessibility Guidelines) list a table of contents among the ways to find one’s bearings, particularly for people with cognitive disabilities.
For sharing and citations. A stable anchor lets you point to a specific passage: in an internal link, an email to support, a forum reply or an AI assistant’s citation. Without an id, you can only link to the top of the page. See also add stable anchors (opens in a new tab), which covers the topic from the point of view of generative engines.
For search engines, a modest effect. Google states that it generally does not support URL fragments (#…): the page stays indexed under a single URL, and each section is not a separate document. When a featured snippet is clicked, Google scrolls the page to the relevant passage without any markup on your part. A table of contents is therefore not a ranking lever; above all it helps make the structure explicit, provided the subheadings are descriptive. See structure H2/H3 headings (opens in a new tab).
What breaks without this check: readers who give up because they cannot find the answer, internal links that can only target the top of a page, and randomly generated anchors (#h-section-3) that change with every edit and break existing links.
What it improves
- Reading experience Direct access to the section you are looking for, especially on mobile.
- Accessibility An extra way to navigate, useful for screen readers and keyboard navigation.
- Internal linking You can link to a specific section of a guide from another page.
- Citations in AI answers Addressable, clearly delimited passages are easier to cite and verify. No practice guarantees a citation.
- Rankings No proven direct effect. A badly made table of contents (broken links, vague subheadings) helps nothing.
How to check
List the headings and their identifiers in the HTML served:
curl -s https://example.com/guides/infusion-the-vert/ | grep -o -E '<h[2-3][^>]*>' | head -20
curl -s https://example.com/guides/infusion-the-vert/ | grep -o -E 'href="#[^"]+"' | sort | uniq
Expected result: every <h2> carries a readable id="…", and every href="#…" link in the table of contents matches an id present in the page.
Quick check in the browser, in the developer tools console:
[...document.querySelectorAll('a[href^="#"]')]
.map(a => a.getAttribute('href').slice(1))
.filter(id => id && !document.getElementById(decodeURIComponent(id)))
An empty array means no anchor is broken. Then test two things by hand: opening the URL with #identifier in a new tab lands on the section, and the heading is not hidden behind a fixed header.
Across the whole site, Screaming Frog can extract the id of H2 headings with custom extraction (XPath //h2/@id) to find long content without anchors.
How to implement
Before you start
When to add a table of contents. A rule of thumb, not a rule: content with at least three or four H2 sections, or longer than two or three screens on mobile. A product page or a contact page does not need one.
Rules for identifiers:
| Rule | Good | Avoid |
|---|---|---|
| Readable, taken from the subheading | temperature-eau |
h-section-3, toc-2 |
| Lowercase, hyphens, no accents | choisir-the-vert |
Choisir_Thé_Vert |
| Short: three or four words | conservation |
comment-bien-conserver-son-the-vert-a-la-maison |
| Unique within the page | faq-livraison |
two id="faq" |
| Stable: does not change if the subheading is edited | set by hand once published | regenerated with every edit |
An id must be unique in the document and must not contain spaces. Once published, treat it like a URL: other pages, emails or AI answers may link to it.
Reference HTML template, valid for every environment:
<nav class="sommaire" aria-label="Contents">
<p class="sommaire-titre">Contents</p>
<ol>
<li><a href="#choisir-the-vert">Choosing your green tea</a></li>
<li><a href="#temperature-eau">Water temperature</a>
<ol>
<li><a href="#sencha">Sencha</a></li>
<li><a href="#matcha">Matcha</a></li>
</ol>
</li>
<li><a href="#conservation">Storage</a></li>
</ol>
</nav>
<h2 id="choisir-the-vert">Choosing your green tea</h2>
<h2 id="temperature-eau">Water temperature</h2>
<h3 id="sencha">Sencha</h3>
The “Contents” label is not an <h2> heading: it must not appear in the page outline. Limit the table of contents to H2 headings, and to H3 headings only if the sections are long.
Shared CSS: offset below a fixed header and smooth scrolling, turned off for people who reduce motion.
:target,
h2[id],
h3[id] {
scroll-margin-top: 5rem;
}
html {
scroll-behavior: smooth;
}
@media (prefers-reduced-motion: reduce) {
html {
scroll-behavior: auto;
}
}
Set 5rem to the height of your fixed header. On mobile, you can fold the table of contents into a <details> element: its content stays in the HTML.
WordPress
Versions WordPress 6.4 and later, Rank Math 1.0, Easy Table of Contents 2.0.
Anchors by hand (block editor). Select the Heading block, then in the Block > Advanced panel, “HTML anchor” field, enter temperature-eau. In the table of contents, link the text to #temperature-eau. The anchor must be unique, start with a letter and contain no spaces; it is case-sensitive.
Automatic table of contents. WordPress has no table of contents block in core. Two common options:
- Rank Math: the “Table of Contents by Rank Math” block, in the block editor only. Excluded heading levels are set in
Rank Math SEO > General Settings > Blocks(tab visible when the Schema module is active) or in each block. - Easy Table of Contents:
Settings > Table of Contents, check the content types where the table of contents should be inserted automatically and limit the levels to H2 and H3. Manual shortcode:[ez-toc]. Also works with the classic editor and page builders.
Check the generated identifiers: some plugins create anchors from the text, which change if you edit the subheading. For important guides, set the anchor by hand in the “HTML anchor” field.
Shopify
Versions Online Store 2.0 themes (Horizon, Dawn), all Shopify plans, November 2026.
Shopify adds neither a table of contents nor anchors to blog posts. In Content > Blog posts, open the post and switch the editor to HTML mode (the <> button), then add the id attributes to the headings and the <nav> block from the reference template at the top of the content. Save and check the published source code: the editor must have kept the attributes.
To automate this across all posts, add a script that numbers the headings to the theme’s article template (sections/main-blog-post.liquid in Horizon, sections/main-article.liquid in Dawn). The limit: the table of contents will only exist once the JavaScript has run. Prefer anchors written in the HTML for your key content.
Drupal
Versions Drupal 10 and 11 (CKEditor 5), TOC filter 2 and TOC API 2 modules.
With the “Full HTML” text format, CKEditor 5’s Source button lets you enter id attributes on headings. For a restricted format such as “Basic HTML”, open Configuration > Content authoring > Text formats and editors, configure the format, go to the “Source editing” plugin tab and add <h2 id> <h3 id> to the manually editable HTML tags; without this, the attribute is stripped on save. For an automatic table of contents, contributed modules such as TOC filter or TOC API generate it from the headings of the text field.
Wix, Squarespace and Webflow
- Webflow: select the heading, open the element settings panel, ID field. The table of contents links point to this section. For CMS collection items, check the published code: if the headings in the rich text field have no
id, you need a script or a dedicated field. - Wix and Squarespace: the editors offer page or section anchors for internal links; their name and location vary with the editor version. Check the published source code: the anchor must appear as a readable
id.
Laravel
Versions Laravel 11 to 13, league/commonmark 2.4 and later.
For content written in Markdown, league/commonmark (included with Laravel) provides the HeadingPermalink and TableOfContents extensions. With Str::markdown():
// app/Support/GuideMarkdown.php
namespace App\Support;
use Illuminate\Support\Str;
use League\CommonMark\Extension\HeadingPermalink\HeadingPermalinkExtension;
use League\CommonMark\Extension\TableOfContents\TableOfContentsExtension;
use League\CommonMark\Normalizer\TextNormalizerInterface;
class GuideMarkdown implements TextNormalizerInterface
{
public static function toHtml(string $markdown): string
{
return Str::markdown($markdown, [
'heading_permalink' => [
'id_prefix' => '',
'fragment_prefix' => '',
'apply_id_to_heading' => true,
'insert' => 'after',
'symbol' => '#',
],
'table_of_contents' => [
'position' => 'placeholder',
'placeholder' => '[TOC]',
'min_heading_level' => 2,
'max_heading_level' => 3,
'html_class' => 'sommaire',
'style' => 'ordered',
],
'slug_normalizer' => [
'instance' => new self(),
],
], [
new HeadingPermalinkExtension(),
new TableOfContentsExtension(),
]);
}
public function normalize(string $text, array $context = []): string
{
return Str::slug($text, '-', 'fr');
}
}
Put [TOC] in the Markdown where the table of contents should go. The normalizer produces identifiers without accents (temperature-de-leau) instead of température-de-leau. Do not set insert to none: the table of contents extension relies on the heading permalinks and then produces nothing. Hide or style the .heading-permalink link in CSS.
If identifiers must survive edits to subheadings, store them: an anchors field per section, or an explicit attribute syntax with the Attributes extension (## Water temperature {#temperature-eau}).
Next.js
Versions Next.js 15 and 16 (App Router), @next/mdx 15 and 16, rehype-slug 6.
For MDX, add rehype-slug, which puts an id on every heading:
// next.config.mjs
import createMDX from '@next/mdx'
const withMDX = createMDX({
options: {
rehypePlugins: ['rehype-slug'],
},
})
export default withMDX({
pageExtensions: ['ts', 'tsx', 'md', 'mdx'],
})
Next.js 16 compiles with Turbopack by default, which does not accept an imported function in the MDX options: declare the plugin by its name, as a string, with an up-to-date version of @next/mdx. rehype-slug keeps accents in identifiers. For accent-free anchors or content coming from a CMS, generate the identifiers server-side and render the table of contents in a server component, so that it is in the initial HTML:
// components/Sommaire.tsx
type Section = { id: string; title: string; level: 2 | 3 }
export function Sommaire({ sections }: { sections: Section[] }) {
if (sections.length < 3) {
return null
}
return (
<nav className="sommaire" aria-label="Contents">
<p className="sommaire-titre">Contents</p>
<ol>
{sections.map((section) => (
<li key={section.id} className={section.level === 3 ? 'niveau-3' : undefined}>
<a href={`#${section.id}`}>{section.title}</a>
</li>
))}
</ol>
</nav>
)
}
Nuxt
Versions Nuxt 3 and 4, Nuxt Content 2 and 3.
With Nuxt Content, Markdown headings get an id and the document exposes a table of contents (body.toc.links) that you display in the template. By default, it only contains H2 headings: set the depth to 3 to include H3 headings.
Nuxt Content 3
Nuxt Content 3 is the version compatible with Nuxt 4. In nuxt.config.ts:
export default defineNuxtConfig({
modules: ['@nuxt/content'],
content: {
build: {
markdown: {
toc: { depth: 3 },
},
},
},
})
In the page, fetch the document with queryCollection('guides').path(route.path).first(), then loop over page.body.toc.links (each link has an id and a text; H3 links are nested in children).
Nuxt Content 2
With Nuxt 3 and Nuxt Content 2, the option goes directly under content.markdown:
export default defineNuxtConfig({
modules: ['@nuxt/content'],
content: {
markdown: {
toc: { depth: 3 },
},
},
})
The document is fetched with queryContent(route.path).findOne() and the table of contents is in doc.body.toc.links. Nuxt Content 2 is no longer developed: plan the migration to version 3.
Astro
Versions Astro 5 to 7.
Astro automatically adds an id to every heading in Markdown and MDX files. For collection content, render() returns the list of headings:
---
// src/pages/guides/[slug].astro
import { getEntry, render } from 'astro:content'
const guide = await getEntry('guides', Astro.params.slug)
const { Content, headings } = await render(guide)
const sections = headings.filter((heading) => heading.depth === 2 || heading.depth === 3)
---
<nav class="sommaire" aria-label="Contents">
<p class="sommaire-titre">Contents</p>
<ol>
{sections.map((heading) => (
<li class={`niveau-${heading.depth}`}>
<a href={`#${heading.slug}`}>{heading.text}</a>
</li>
))}
</ol>
</nav>
<Content />
Common mistakes
- A table of contents built only in JavaScript: it does not exist in the HTML served and the anchors may be missing at crawl time. Generate it server-side or at build time.
- Identifiers regenerated with every edit: a reworded subheading changes the anchor and breaks every link that pointed to it.
- Opaque identifiers (
#h-3,#section-2): unreadable in a shared or cited link. - Duplicate
idvalues: two “Conclusion” or “FAQ” sections in the same page; the link goes to the first one. - Heading hidden by the fixed header after the jump: set
scroll-margin-top. - A table of contents on a short page: it adds noise above the useful content.
- Vague subheadings (“Introduction”, “Part 2”): the table of contents tells the reader nothing. See avoid purely decorative headings (opens in a new tab).
- Fragments used to load different content: Google does not treat
#…as a separate page. Anchors are for navigating within a page, not for creating pages.
Frequently asked questions
Does a table of contents improve Google rankings?
Not directly. Google states that it generally does not support URL fragments: the page stays indexed under a single address, and sections are not ranked separately. A table of contents mainly helps readers find an answer and makes the page structure explicit, provided the subheadings are descriptive. Add one for the reading experience and for sharing precise links, not to gain positions.
Should H3 headings be included in the table of contents?
Only if the H2 sections are long or if the H3 headings match questions that readers search for directly. A thirty-line table of contents loses its point: readers must be able to scan it at a glance. For most guides, H2 headings are enough. H3 headings can keep their own anchor without appearing in the table of contents, so they can be linked from other pages.
Does a table of contents plugin slow down WordPress?
The effect is usually small, but some plugins load CSS and JavaScript on every page, even pages without a table of contents. Check in the Network tab of the developer tools which files they add, limit automatic insertion to the relevant content types and turn off animated scrolling if your theme already handles it. A table of contents written by hand in the editor adds no files.
What happens if I rename a subheading?
If the anchor is generated automatically from the text, it changes, and every link that pointed to the old anchor lands at the top of the page. Set the identifier by hand for important sections, and keep it even when you reword the heading. If you really must change it, add an empty element carrying the old identifier just before the section.
Resources
- Google Search Central: URL structure best practices (fragments)developers.google.com
- Google Search Central: Featured snippetsdevelopers.google.com
- W3C: Technique G64, Providing a Table of Contentsw3.org
- MDN: id attribute and scroll-margin-topdeveloper.mozilla.org
- WordPress: Page jumps and HTML anchorswordpress.org
- league/commonmark: Table of Contents extensioncommonmark.thephpleague.com
- Astro: Headings and anchors in Markdowndocs.astro.build
- rehype-sluggithub.com
Updated on
Topic checklist (PDF)