Markdown link to section in another file

How to link from one Markdown file to a specific heading in another file: syntax, how heading anchors are generated, relative paths, explicit anchors and how to check the link works.

A link to a section in another Markdown file points to a specific heading inside a different document, not just the top of it. You combine two things in a single link: the path to the target file and an anchor, the name of the heading. When the rendered page is opened, the reader lands directly on that heading. The text of your heading is not changed. Only the link that points to it needs to match.

The basic syntax

Use a normal inline link and add a hash sign followed by the heading's anchor after the file path. Example:

[See the installation steps](setup.md#installation)
  • [See the installation steps] is the visible link text.
  • setup.md is the path to the other file, relative to the file that contains the link.
  • #installation is the fragment identifier, the part after the hash that names the target heading. In this example, it refers to a heading such as "## Installation" in setup.md.

To link to a heading in the same file, leave out the path and keep only the anchor, for example Installation. The cross-file form follows the same pattern. It adds the file path in front of the anchor.

How a heading becomes an anchor

Most Markdown renderers create an anchor for each heading automatically. This anchor is often called a slug. Standard Markdown (CommonMark) does not define how slugs are built. The rules come from the platform or tool that renders your files. A widely used convention, followed by GitHub and many tools modeled on it, works like this:

  • Convert the heading text to lowercase.
  • Replace each space with a hyphen.
  • Remove most punctuation, such as periods, apostrophes, question marks and colons. Hyphens and underscores are kept.
  • Keep letters and digits, including non-Latin letters such as Cyrillic.
  • If the same heading text appears more than once in a file, add a number to the later ones: the second becomes -1, the third -2, and so on.

The table below shows example results under that convention. These are illustrations of the rule, not verified output from any specific tool. Other renderers may produce different anchors.

Heading in the target file | Link under the GitHub-style convention
Installation | other.md#installation
Getting Started | other.md#getting-started
What's New? | other.md#whats-new
API v2.0 | other.md#api-v20
Установка | other.md#установка
Notes (second heading with this text) | other.md#notes-1

Pay attention to exact characters. Two consecutive spaces in a heading usually become two consecutive hyphens. An underscore stays an underscore. Some tools transliterate or percent-encode non-ASCII headings instead of keeping them as-is. If a heading contains formatting such as backticks or emphasis, the markup characters are generally dropped, and the text inside them is kept.

Step by step: linking to a heading in another file

  • 1. Open the target file and find the exact heading you want to link to.
  • 2. Work out its anchor using your renderer's slug rules. Alternatively, open the rendered page and copy the anchor from the heading's link icon or from the address bar after you click it.
  • 3. Write the relative path from the current file to the target file.
  • 4. Join the path and the anchor with a hash sign: path/to/file.md#anchor.
  • 5. Put it inside a Markdown link: Link text.
  • 6. Render or preview the result and click the link. Confirm that it opens the right file and scrolls to the right heading.

Getting the relative path right

Paths are resolved relative to the file that contains the link, not relative to the project root. Example layouts:

Same folder:
[Usage](usage.md#basic-usage)

Subfolder:
[Environment variables](docs/config.md#environment-variables)

Parent folder:
[Back to overview](../README.md#overview)

File names are matched exactly. On case-sensitive systems and hosting platforms, README.md and readme.md are different files. If a file name contains spaces, CommonMark lets you wrap the destination in angle brackets, for example Notes. You can also encode each space as %20. Renaming the file so it has no spaces is often the simplest option.

Using reference-style links for the same target

If you link to the same section in several places, a reference-style link keeps the path and anchor in one definition. Converting an inline link to this form does not change the destination, as long as you copy the path and anchor exactly. Example:

Follow the [setup guide][setup-install] before continuing.

[setup-install]: setup.md#installation

The label in square brackets (setup-install) matches its definition regardless of letter case. The anchor after the hash is still compared to the generated heading anchor. Many renderers treat that comparison as case-sensitive, so keep the anchor in lowercase if your renderer generates lowercase slugs.

Setting your own anchor when generated ones are unreliable

Generated anchors change whenever someone edits the heading text, which breaks links in other files. Two common ways to set a stable anchor:

Inline HTML anchor placed just above the heading:
<a id="install"></a>
## Installation and First-Time Setup

Heading attribute syntax (only in tools that support this extension):
## Installation and First-Time Setup {#install}

With either approach, the link becomes Install. Support for each approach varies. Heading attributes are an extension found in tools such as Pandoc and some static site generators. In renderers that do not support this extension, the {#install} part appears as literal text. Some hosting platforms filter raw HTML or rewrite id values, so test an HTML anchor on the platform where readers will actually see the file.

Checking the link and fixing common problems

  • The link opens the right file but stays at the top: the anchor does not match. Compare it character by character with the rendered heading's anchor, including hyphens, digits and duplicate suffixes.
  • The link reports a missing page: the path is wrong. Check the folder levels, the file extension and the letter case.
  • The link works on one platform but not on another: the two renderers use different slug rules. Consider an explicit anchor instead.
  • The link works in source view but not on a built website: some site generators rewrite .md links to the generated page names, and others leave them unchanged. Check how your generator handles links between Markdown files.
  • A heading was renamed: search the project for the old anchor and update every link that uses it.

Note the limits of this guide: no tool-specific documentation or test results were provided for it. The slug rules described here are a common convention, not a universal standard. Before you rely on a link, confirm how your own editor preview, repository host or site generator creates heading anchors, and click each link once in the final rendered output.