Markdown
Web Development
HTML
Technical Writing
Documentation

How to link to a section or heading in the same Markdown file

Codemia Knowledge Hub ·

Use standard link syntax with a # anchor built from the heading text: [link text](#heading-slug). The slug is the heading lowercased, with spaces replaced by hyphens and special characters removed. This is how every Markdown table of contents works, and it is supported on GitHub, GitLab, VS Code previews and most static site generators.

Linking to a heading

Every heading automatically becomes an anchor target:

markdown
1## Table of Contents
2- [Installation](#installation)
3- [Configuration Options](#configuration-options)
4- [API Reference](#api-reference)
5- [Troubleshooting](#troubleshooting)
6
7## Installation
8Steps to install the tool...
9
10## Configuration Options
11How to configure the tool...
12
13## API Reference
14Endpoint documentation...
15
16## Troubleshooting
17Common issues and fixes...

#configuration-options comes from ## Configuration Options: lowercased, with the space replaced by a hyphen.

How heading slugs are generated

The rules vary slightly between processors, but the common convention (GitHub, GitLab and most static site generators) is:

  1. Convert the heading text to lowercase
  2. Remove all punctuation except hyphens
  3. Replace each space with a hyphen (-)

GitHub does not collapse repeated hyphens, so ## Setup - Linux becomes #setup---linux (the hyphen is kept and each space around it adds one more). GitLab documents an extra step that merges consecutive hyphens into one, so the same heading there is #setup-linux.

markdown
1## Heading Text                    -> #heading-text
2## What's New in v2.0?             -> #whats-new-in-v20
3## C++ Template Metaprogramming    -> #c-template-metaprogramming
4## Step 1: Install Dependencies    -> #step-1-install-dependencies
5## Don't Repeat Yourself (DRY)     -> #dont-repeat-yourself-dry
6## Setup - Linux                   -> #setup---linux (GitHub)

Differences between processors

PlatformPunctuation handlingDuplicate headingsEmoji in headings
GitHub Flavored MarkdownRemoves most punctuation, keeps hyphensAppends -1, -2, etc.Strips emoji
GitLabSame as GitHubAppends -1, -2, etc.Strips emoji
Hugo (Goldmark)Removes punctuation, keeps hyphensAppends -1, -2, etc.Strips emoji
DocusaurusRemoves punctuationAppends -1, -2, etc.Strips emoji
PandocRemoves punctuation, prefix with section- optionalAppends -1, -2, etc.Keeps emoji in ID
VS Code previewFollows CommonMark extensionsDoes not deduplicateStrips emoji

When in doubt, inspect the id attribute on the heading in the rendered HTML.

Custom anchors with HTML or {#id}

When the generated slug is inconvenient, or you need a target that is not a heading, insert a raw HTML anchor:

markdown
1## Using HTML Anchors
2
3You can place an anchor anywhere in the document:
4
5<a id="custom-anchor"></a>
6
7This paragraph is the target of the custom anchor.
8
9Link to it from anywhere: [Jump to custom section](#custom-anchor)

This lets you:

  • Link to a specific paragraph, not just a heading
  • Use short, stable anchor names that survive heading renames
  • Link to positions inside code blocks or tables

Some processors also support a heading ID in curly braces:

markdown
## My Long Heading Title {#short-id}

[Link to it](#short-id)

This works in Pandoc, Hugo, Docusaurus and several others, but not in GitHub Flavored Markdown.

Building a table of contents

A manual table of contents is a list of internal links at the top of the document:

markdown
1# Project README
2
3## Table of Contents
41. [Overview](#overview)
52. [Prerequisites](#prerequisites)
63. [Installation](#installation)
7   - [macOS](#macos)
8   - [Linux](#linux)
9   - [Windows](#windows)
104. [Usage](#usage)
115. [Contributing](#contributing)
12
13---
14
15## Overview
16Brief description of the project...
17
18## Prerequisites
19What you need before installing...
20
21## Installation
22
23### macOS
24```brew install mypackage```
25
26### Linux
27```sudo apt install mypackage```
28
29### Windows
30Download the installer from...
31
32## Usage
33How to use the tool...
34
35## Contributing
36How to contribute to the project...

Tools can generate a TOC for you (for example the Markdown All in One extension for VS Code, or TOC plugins in many static site generators), but writing it by hand gives full control over ordering and nesting.

Linking to a section in another file

Combine a file path with an anchor:

markdown
1<!-- Link to a heading in another file -->
2See the [authentication section](./docs/security.md#authentication)
3
4<!-- Relative path from a nested directory -->
5Refer to the [API docs](../api/endpoints.md#post-users)

These work when browsing a repository on GitHub. In static site generators, the file extension is usually replaced with .html or removed, depending on the URL scheme.

markdown
1# Deployment Guide
2
3## Table of Contents
4- [Prerequisites](#prerequisites)
5- [Environment Setup](#environment-setup)
6- [Database Migration](#database-migration)
7- [Rolling Deployment](#rolling-deployment)
8- [Rollback Procedure](#rollback-procedure)
9- [Health Checks](#health-checks)
10
11## Prerequisites
12
13Ensure you have:
14- Docker 24+
15- kubectl configured for the target cluster
16- Access to the container registry
17
18For registry setup, see [Environment Setup](#environment-setup).
19
20## Environment Setup
21
22Export the required variables:
23...
24
25## Database Migration
26
27Run migrations **before** deploying new code.
28If migration fails, see [Rollback Procedure](#rollback-procedure).
29
30## Rolling Deployment
31
32The deployment uses a rolling update strategy.
33After deployment, verify with [Health Checks](#health-checks).
34
35## Rollback Procedure
36
37To roll back, redeploy the previous image tag.
38Check [Prerequisites](#prerequisites) for required access.
39
40## Health Checks
41
42Verify that `/healthz` returns 200 on all pods.

The links between sections let readers jump straight to what they need.

  • Uppercase in the anchor. Anchors are case-insensitive in some processors and case-sensitive in others. Use lowercase: [Link](#my-heading), not [Link](#My-Heading).
  • Punctuation in the anchor. ## What's New? becomes #whats-new, not #what's-new?. Apostrophes, question marks, periods and colons are stripped.
  • Duplicate headings. A second ## Overview becomes #overview-1, and inserting a new heading can shift the numbering. Use unique headings or manual anchors.
  • Spaces in anchors. Anchors use hyphens, not spaces or %20. [Link](#my heading) will not work.
  • Curly-brace IDs on GitHub. {#custom-id} is not supported there. Use <a id="custom-id"></a> instead.
  • Renamed headings. Renaming a heading silently breaks every link to it. Search the document for the old anchor after any heading change.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms