Articles
Published · December 1, 2026

Markdown Heading IDs and Anchor Links on GitHub

GitHub builds an anchor for every Markdown heading: lowercase, hyphens, punctuation dropped, -1 on duplicates. Learn the rules and how to link to a heading.

Also in: Español中文हिन्दी

GitHub gives every Markdown heading an anchor automatically, so you can link to it with [text](#anchor) and never write an ID yourself. The anchor is the heading text, lowercased, with spaces turned into hyphens and most punctuation removed.

How GitHub turns a heading into an anchor

GitHub's section link rules boil down to five rules:

  • Letters are converted to lowercase.
  • Spaces are replaced by hyphens (-). Any other whitespace or punctuation characters are removed.
  • Leading and trailing whitespace are removed.
  • Markup formatting is removed, leaving only the contents, so _italics_ becomes italics.
  • If the anchor is identical to an earlier one in the same document, a hyphen and an auto-incrementing integer are appended.

A worked example

GitHub's own documentation uses a heading with an apostrophe, italics, a Greek letter and an exclamation mark. Here's how it becomes a link:

## This'll be a _Helpful_ Section About the Greek Letter Θ!

[Link Text](#thisll-be-a-helpful-section-about-the-greek-letter-Θ)

The apostrophe and the exclamation mark disappear. The underscores around Helpful go with the italics. The Greek letter stays, because it's a letter, not punctuation.

Finding the anchor without guessing

In a rendered file, hover over the heading until the link icon appears, then click it. The anchor now sits in your browser's address bar after the #. While you're still editing, apply the rules above, then confirm once the file renders.

Link to a heading from the same file

A link to a heading in the current file needs only the fragment:

- [Install](#install)
- [Configuration](#configuration)
- [Troubleshooting](#troubleshooting)

If the target is another file in the repository, use a relative path first and the fragment second, such as docs/setup.md#install. GitHub's documentation says a relative link is relative to the current file, a link starting with / is relative to the repository root, and GitHub rewrites it for whichever branch you are on. Keep the link text on a single line. Bare web addresses behave differently, as covered in what renders as a link in GFM.

You may not need a hand-written contents list at all. For the rendered view of any Markdown file, GitHub generates a table of contents from the headings. It appears behind the Outline menu icon in the file header, and the basic syntax page says it needs two or more headings.

Duplicate headings and the numeric suffix

READMEs often repeat a heading such as Example or Usage. The first one keeps the plain anchor and each later one gets a number:

## Example
## Example
## Example

[first](#example) [second](#example-1) [third](#example-2)

There's a catch. GitHub warns that if you edit a heading, or change the order of headings with identical anchors, the anchors change and you must update the links. Swap two Example sections and #example-1 now points at the other one, and nothing warns you. The simplest fix is to word repeated headings differently, such as Example: Python and Example: Shell.

Custom anchors when a heading will change

If a link must survive a rewording, put a named anchor above the heading. GitHub supports standard HTML anchor tags for this:

<a name="release-notes-anchor"></a>
## What changed in this release

[Jump to the release notes](#release-notes-anchor)

The docs suggest a unique naming scheme, such as a prefix, to avoid ambiguous references. A custom anchor is linked exactly like a generated one, but it is left out of the table of contents and ignored by the automatic naming and numbering of heading links. The anchor tag is HTML, so a renderer that blocks raw HTML may drop it. Check where the file will actually be read.

Headings that never get an anchor

No heading, no anchor. And some lines that look like headings aren't. The GFM spec says an ATX heading starts with one to six # characters followed by a space or the end of the line:

#hashtag
#5 bolt
####### seven hashes
    # indented four spaces

None of these is a heading. The first two lack the space. The third has more than six #. The last is an indented code block, because four spaces of indentation is too many. If a link to one of them does nothing, that's why. Our notes on how CommonMark reads a tab cover the space and tab rules, and when four spaces mean code covers the indentation case.

Why the same heading gives different anchors elsewhere

The CommonMark spec and the GFM spec both render headings as bare h1 to h6 elements. Neither defines an id attribute or how to build one. The anchor rules above are GitHub's behavior, and other renderers may lowercase differently, handle punctuation differently or add no IDs at all. If a README is also published to a documentation site, test the links there, not only on GitHub.

Doing this with Carets

Anchor links break on small details: a missing space after #, a heading reworded in passing, a duplicate you forgot about. Carets is a native notes and code editor for iPhone, iPad and Mac, and it keeps your README as a plain Markdown file you can open anywhere, so the heading text you write is the heading text GitHub sees.

Syntax highlighting makes it easy to see which lines are headings, and files, projects and tags keep a README draft next to the notes you are collecting for it. You edit on an iPhone, iPad or Mac, then commit the file with your usual Git workflow.

Get Carets for iPhone, iPad and Mac on the App Store.

Frequently Asked Questions

How do I find the anchor for a heading on GitHub?

Hover over the heading in the rendered file, click the link icon, and read the fragment after the # in the address bar. You can also apply the rules: lowercase, spaces to hyphens, other punctuation removed.

Can I set my own heading ID in GitHub Markdown?

Not on the heading itself. Add an HTML anchor tag with a name attribute above the heading and link to that name. Custom anchors do not appear in the table of contents.

Why does my link to a duplicate heading go to the wrong section?

The first heading keeps the plain anchor, and later identical headings get -1, -2 and so on. If you reorder identical headings, each link then reaches a different section.

Do CommonMark and GFM define heading IDs?

No. Both specs render plain h1 to h6 elements with no id, so anchor rules come from the renderer, not the Markdown spec.