Markdown Internal Links: Anchor Link Syntax That Works on GitHub
Create internal links in Markdown with anchor syntax. Covers heading ids, spaces and special characters, links between files, custom ids, and why anchor links break.
Markdown internal links are a powerful tool to help users navigate your document. These internal links can direct your readers to different sections inside the current page or to a specific part of another Markdown document within the same repository.
What are Markdown Internal Links?
Markdown internal links, also known as anchor links, offer a way to link to specific areas within a document. These links come incredibly handy for long documents, providing a seamless navigation experience.
Before diving into how to create internal links, here is Markdown's basic syntax for creating links. For every link form (inline, reference, autolinks, images, escaping), see the full Markdown link syntax reference.
[Link Text](URL)
This Markdown code will render as a clickable hyperlink with "Link Text" being the clickable text, and "URL" being the webpage the user will be directed to when clicking on the link.
Creating Markdown Internal Links
To create an internal link in Markdown:
- First, you need to create an ID for the section you wish to link. This is done by adding an anchor tag before the header. The ID can be anything you want, but it's best to keep it related to the section's title.
<a id="section_id"></a>
### Section Title
We're using a simple HTML anchor tag (<a>) with an id attribute (?section_id?). The anchor tag is self-closed, meaning it doesn't need a closing </a> tag.
- Now that you've assigned an ID to the section, you can create an internal link pointing to it. Use the normal syntax for creating a link in Markdown, but instead of a URL, use the ID prefixed with a
#.
[Section Title](#section_id)
When you click on "Section Title," you will be led to the section with the "section_id." Here's an example:
<a id="installation"></a>
## Installation
To install the package do...
[Go to Installation](#installation)
Clicking "Go to Installation" will take the user to the "Installation" section.
Linking to Headers
Markdown automatically turns your headers into anchors, so you can link directly to them without having to create a custom ID. From my earlier Post on How to link to a header in markdown, Markdown converts your headers to anchors using the header's text, all lowercase, and spaces replaced by hyphens (-).
Example:
### Frequently Asked Questions
[Link to FAQ](#frequently-asked-questions)
Clicking on "Link to FAQ" takes you directly to the "Frequently Asked Questions" header.
However, keep in mind that this feature may not be available in all Markdown renderers. Therefore, using custom IDs is the safer choice, as demonstrated earlier.
Generate the links for you
Anchor links break quietly when a heading is renamed. If you are linking to many headings, the Markdown Table of Contents generator builds a correct list of #heading-id links from your headings, and the Markdown Linter flags links that point to a heading that does not exist. To check how an anchor renders, paste your document into the live preview editor and click the link.
Conclusion
Markdown internal links are a vital tool in creating navigable and reader-friendly documentation. Learning how to extend Markdown syntax with a bit of HTML expands the utility and value of your markdown documents, enabling more structured navigation.
In this brief guide, we've unpacked how to use anchor links in Markdown to facilitate internal linking within your text, making it dynamically navigable for your readers.