Sub topic 11
Relative Links and Heading Anchors
This chapter assumes you can write basic Markdown links and have committed files to a GitHub repository. It covers how to link to other files in the same repository and to sections within a page, so navigation keeps working on branches, forks, and clones.
Relative links
A relative link gives a path from the current file instead of a full URL. GitHub resolves it against the branch or commit you are viewing.
See [CONTRIBUTING.md](CONTRIBUTING.md) for the workflow.
Read the [setup guide](docs/setup.md).
Back to the [main README](../README.md).Paths work like file paths in a terminal:
docs/setup.mdis a file in thedocsfolder next to the current file.../README.mdgoes up one folder./docs/setup.mdstarts at the repository root, which GitHub also supports.
Absolute URLs such as https://github.com/OWNER/REPO/blob/main/docs/setup.md pin the link to one branch name. If the branch is renamed or the repository is forked, the link can point to the wrong place. Relative links follow the content.
Heading anchors
GitHub gives every heading an anchor, an ID you can link to with #. It builds the ID from the heading text by these rules:
- Convert letters to lowercase.
- Replace spaces with hyphens.
- Remove punctuation other than hyphens.
- If the ID already exists on the page, add
-1,-2, and so on to later duplicates.
| Heading | Anchor |
|---|---|
| `## Installation` | `#installation` |
| `## Getting Started` | `#getting-started` |
| `## FAQ: Common Errors?` | `#faq-common-errors` |
| `## Usage` (second one) | `#usage-1` |
Link to an anchor in the same file with #anchor, or in another file by adding it after the path.
Jump to [Installation](#installation).
See [advanced options](docs/config.md#advanced-options).Common errors
- Renamed heading. Changing
## Setupto## Installationbreaks every#setuplink. GitHub does not warn you, so search the repository for the old anchor. - Wrong case in paths. Linux-based servers treat
Docs/Setup.mdanddocs/setup.mdas different files. Match the case exactly. - Duplicate headings. The second
## Usagegets#usage-1. Reordering sections changes which heading gets the suffix. - Spaces in file names. Write
my%20file.mdor rename the file. Hyphens avoid the problem. - Relative links outside GitHub. Package registries that show your README may not resolve relative paths. Use full URLs for links that must work there.
Exercise
A README has two headings named ## Examples. What anchor links to the second one, and what happens to that link if you add a new ## Examples heading above it?
Show answer
The second one is #examples-1. If a new ## Examples heading is added above it, the new heading takes #examples or the next suffix in order, so the old second heading becomes #examples-2 and links to #examples-1 point to a different section.
Exercise
You are editing docs/setup.md and want to link to the ## License section of the README in the repository root. Write the link.
Show answer
[License](../README.md#license)