Markdown link to section in same file
How to link to a heading in the same Markdown file: anchor syntax, how GitHub builds heading IDs, handling special characters and duplicates, and how to check the result.
A link to a section in the same Markdown file points to a heading elsewhere in the document. Clicking it scrolls the reader there instead of opening another page. It is the basis of tables of contents, "see below" references and back-to-top links in READMEs and long documentation pages. This guide covers the syntax, how heading anchors are generated, how to handle punctuation and non-English characters, and how to confirm that a link works.
What an in-page anchor link does for your text
The input is an ordinary Markdown link whose destination starts with a hash sign (#) followed by an identifier, called a fragment or anchor. When the document is rendered to HTML, most renderers give each heading an id attribute. A link whose fragment matches that id jumps to the heading. Nothing about the heading text changes. The renderer derives the anchor automatically, or you assign one explicitly. Only the rendered output gains a working jump. The scope is the current rendered page. Pointing at a section in a different file uses the same fragment idea appended to that file's path.
Basic syntax
Write a normal inline link and use a fragment as the destination. Example source:
## Installation
See the [installation steps](#installation) before continuing.
The fragment must match the heading's generated ID, not the heading text as typed. For simple one-word headings the two look almost the same. Once a heading contains spaces, capitals or punctuation, the ID differs, and that is where most broken links come from.
Reference-style links work the same way, which keeps long fragments out of the running text. Example:
Read the [configuration notes][config] first.
[config]: #configuration-notes
How GitHub turns a heading into an anchor
GitHub-flavored rendering, used for READMEs, issues and wiki pages, generates heading IDs with a commonly described set of rules. Other renderers often follow similar rules but not always identical ones, so treat the following as GitHub's behavior rather than a Markdown standard. The core CommonMark specification does not define heading IDs at all.
- 1. Take the heading's text content, without the leading # markers or inline formatting syntax.
- 2. Convert letters to lowercase.
- 3. Remove punctuation and symbols. Letters, digits, spaces, hyphens and underscores are kept, and that includes non-Latin letters.
- 4. Replace each space with a hyphen. Spaces are not collapsed, so two adjacent spaces become two hyphens.
- 5. If the same ID already exists on the page, append -1, -2 and so on to later occurrences.
Step 4 is easy to miss. When punctuation sits between two words, removing it leaves two spaces behind. Those become a double hyphen.
Headings with special characters: worked examples
The table applies the rules above by hand. These are illustrative derivations, not captured renderer output. Confirm them in your target renderer using the check described later. Exact spacing in the heading matters, because each space maps to one hyphen.
| Heading as written | Expected GitHub fragment | Why |
| Getting Started | #getting-started | lowercased, space to hyphen |
| What's new? | #whats-new | apostrophe and question mark removed |
| Install & Configure (v2.0) | #install--configure-v20 | & leaves two spaces, so two hyphens; parentheses and period removed |
| C++ / C# notes | #c--c-notes | + / # removed; the gap around the slash becomes a double hyphen |
| my_variable | #my_variable | underscore kept |
| Über uns | #über-uns | non-ASCII letters kept, lowercased |
| Setup (second heading with this text) | #setup-1 | duplicate suffix |
Emoji and other symbols are dropped like punctuation. A heading that begins with an emoji followed by a space therefore usually produces a fragment that begins with a hyphen, such as #-launch for a heading made of a rocket emoji and the word Launch. Inline code in a heading contributes its text without the backticks. A heading containing npm install yields npm-install for that portion.
Non-English characters in the link
Headings in Cyrillic, Greek, CJK and other scripts keep their letters in the generated ID. A heading such as "## Установка пакета" is expected to become #установка-пакета. You can usually type the characters directly in the link destination. Some tools and browsers display or store them percent-encoded instead, for example %D0%A3... for Cyrillic. The two forms normally refer to the same anchor. Case matters, though: GitHub lowercases the ID, so write the fragment in lowercase too. If a link with raw non-ASCII text fails in a particular renderer, try the percent-encoded form, or add an explicit ASCII anchor as described next.
Spaces and parentheses in the destination
A Markdown link destination cannot contain a bare space. Always use the hyphenated ID, never the heading text with spaces. Balanced parentheses inside a destination are allowed, but generated IDs rarely contain them, because they are stripped from the heading.
Setting your own anchor
When a heading is long, likely to be reworded, or full of symbols, a fixed anchor you choose yourself is more robust than a derived one. Two approaches are common, and support depends on the renderer.
An HTML anchor placed just before or inside the heading. GitHub and many other renderers allow this small piece of inline HTML. Example:
<a id="config"></a>
## Install & Configure (v2.0)
Jump to [configuration](#config).
A heading attribute, supported by Pandoc, kramdown and various static-site generators and editor extensions, but not by GitHub's README rendering. Example:
## Install & Configure (v2.0) {#config}
Where the attribute syntax is unsupported, the braces appear as literal text in the heading. Check which form your publishing platform accepts before relying on it. A custom anchor also survives later edits to the heading wording, whereas a derived anchor changes whenever the heading text changes.
Step by step: create and verify the link
- 1. Find the target heading and copy its exact text, including punctuation and spacing.
- 2. Derive the fragment with the rules above: lowercase it, strip punctuation, turn each space into a hyphen, and add a numeric suffix if the heading text repeats.
- 3. Write the link as visible text, or define a reference-style label for it.
- 4. If the heading is unstable or symbol-heavy, add an explicit anchor and link to that instead.
- 5. Render the file in the same environment where readers will see it, such as the repository view, preview pane or built site.
- 6. Click the link and confirm the page scrolls to the right heading and the address bar ends with the expected fragment.
On GitHub, hovering over a rendered heading shows a small link icon beside it. Copying that link gives you the exact fragment GitHub generated, which is the most reliable way to resolve doubtful cases such as emoji, mixed scripts or unusual symbols. Many editors with Markdown preview offer a similar affordance or an outline view, but anchor rules in a local preview may not match the final platform.
Common reasons a section link does not work
- Capital letters in the fragment while the generated ID is lowercase.
- Spaces in the destination instead of hyphens.
- A single hyphen where the removed punctuation actually left a double hyphen.
- The heading was renamed after the link was written.
- The link targets the first of two identical headings when the second was intended, or the reverse.
- An attribute-style custom ID used on a platform that does not support it.
- Anchor rules differ between the preview tool and the publishing platform.
A note on the limits of this guide
The anchor rules described here reflect GitHub's widely observed behavior and general Markdown conventions. No platform-specific test results were supplied for this article. Renderers such as GitLab, documentation generators and note-taking apps each apply their own slug rules, which can differ for punctuation, Unicode and duplicates. Treat the examples as a starting point and confirm every non-trivial anchor in the renderer your readers actually use.