Skip to content

Instantly share code, notes, and snippets.

@asabaylus
Created July 8, 2012 14:12
Show Gist options
  • Save asabaylus/3071099 to your computer and use it in GitHub Desktop.
Save asabaylus/3071099 to your computer and use it in GitHub Desktop.
Github Markdown Heading Anchors

Anchors in Markdown

To create an anchor to a heading in github flavored markdown. Add - characters between each word in the heading and wrap the value in parens (#some-markdown-heading) so your link should look like so:

[create an anchor](#anchors-in-markdown)

@soniatechie
Copy link

I have found here (https://docs.microsoft.com/en-us/contribute/how-to-write-links) that you can create explicit anchor links but it is not recommended.

Why is it not recommended if you can used this to solve the problem with Spanish words with accents?

## <a id="anchortext" />Header text

@DevDotKP
Copy link

DevDotKP commented May 9, 2021

How do I use anchor links to link to another page on the same Wiki?

@firedynasty
Copy link

firedynasty commented May 20, 2021

Here's a tip -- if you're having trouble with your anchor not working due to misspellings or odd characters, simply hover over your heading on Github, then hover or click the link icon that appears to the left. You can then right click to copy the link location, left click it to go to the URL and view it in your address bar, or simply stay hovered over it and view it in the status bar of your browser.

Thanks a lot! droplet icloud : there is a hyphen (#droplet-icloud) to a heading that looks like "# Droplet icloud"

@IZVRStudio
Copy link

How can I link a header with numbers?
Example:
# 1.1 Header

@jabielecki
Copy link

How can I link a header with numbers? Example: # 1.1 Header

Try #11-header

@IZVRStudio
Copy link

Doesn't work. It creates an empty link that doesn't go anywhere :(

@Velojet
Copy link

Velojet commented Sep 28, 2021

How can I link a header with numbers? Example: # 1.1 Header

Surround your header text with a named anchor link (e.g. <a name="1-1-header">1.1 Header</a>) and link to that.

@stasyao
Copy link

stasyao commented Nov 17, 2021

How can I link a header with numbers? Example: # 1.1 Header

The header at the top includes a link that leads to the header at the bottom.
Upd. You can use any id, its value does not have to match the text of the element.

# 1.1 [Header](#1.1)   

...some text...

# 1.1 Header<a id='1.1'></a>

image

@DevJonTaylor
Copy link

IDK if this hack will help anyone at all. However span tags are rendered in GitHub's Markdown Parse. The id attribute is replaced with id="user-content-${whatever you made the id}" I.E.

<span id="85af93540870"></span>
<!-- The above is rendered to the below -->
<span id="user-content-85af93540870"></span>

<!-- So when you do something like the below -->

* [Leading The Way](#85af93540870)

<!-- Filler Code Goes Here -->

## <span id="85af93540870"></span>Dude Where's My Car

It will link directly to the span.

This worked for me, I created a markdown creator that works off of six hex character ids which I utilized this hack to allow the table of contents to be generated.

Hope it helps someone!!! =)

Dev Jon Taylor,

  • Happy Coding!

@HelderMagalhaes
Copy link

So, characters like á,é,í,ó,ú,Á,É,Í,Ó,Ú,Ñ,ñ are just omitted in anchors.

According to the specification, an URL fragment is ASCII-only (no "international characters" accepted directly).
That applies to most other URL components. Browsers usually perform the interpretation automatically in the address bar in order to be more user friendly - copy+pasting into plain text (such as Notepad) allows to see the "real" URL.

@tillmannheigel
Copy link

Upd. You can use any id, its value does not have to match the text of the element.

# 1.1 [Header](#1.1)   

...some text...

# 1.1 Header<a id='1.1'></a>

Nice, that was actually the only real solution I found in the www for this issue. ❤️ Many thanks!

@yadimon
Copy link

yadimon commented Apr 13, 2022

Easiest Way I found to fix this was after writing down the headings on github, just do inspect element from the browser and find what the anchor links are 😁

thx, could not find the link for my complex text.

@AlexDev404
Copy link

Upd. You can use any id, its value does not have to match the text of the element.

# 1.1 [Header](#1.1)   

...some text...

# 1.1 Header<a id='1.1'></a>

Nice, that was actually the only real solution I found in the www for this issue. ❤️ Many thanks!

This works well. Thanks! 👍

@avinal
Copy link

avinal commented Jun 15, 2022

Thanks

@abhishp021
Copy link

abhishp021 commented Jul 1, 2022

Note- If anyone facing issues with anchor linking. Please ensure that the text of anchor and the text of place where you are linking are different.
For e.g. There will be problem if you use

> ### [Anchor Link](#anchor-link)
> ## Anchor Link

Here problem because "Anchor Link" is used as text in both lines
But if different text used in [ ] then no issues. For e.g. we added "1." in [ ]

> ### [1. Anchor Link](#anchor-link)
> ##  Anchor Link

@DiasFranciscoA
Copy link

DiasFranciscoA commented Sep 22, 2022

Is there a way to do the opposite don't generate anchor links. This seems to happen automatically in github wiki everytime you defined a header and that clutters the sidebar navigation.

@Harmyder
Copy link

Every time I change section name I need to go and update all the anchors.

@jdaw1
Copy link

jdaw1 commented Oct 22, 2022

Consider HTML <tr id="XXX">…. Then a link to #XXX is a link to that whole row. Clicking on a <a href="#XXX">to XXX</a> causes the whole row to be on the screen, if possible the top pixel of the row at the top of the screen (not always possible if row near end of page). This is good.

In Markdown, putting | <a name="XXX"></a> … |, and clicking on [to XXX](#XXX) jumps to the middle of the row, at least if there is an image in the row. The top half of the row isn’t visible: above top of viewport. This is not good.

Please, is there a known solution?

Jacks and Numbers
UnionJack One
UnionJack Two
UnionJack Three
UnionJack Four
UnionJack Five
UnionJack Six
UnionJack Seven
UnionJack Eight
UnionJack Nine
UnionJack Ten
UnionJack Eleven
UnionJack Twelve
UnionJack Thirteen
UnionJack Fourteen
UnionJack Fifteen
UnionJack Sixteen
UnionJack Seventeen
UnionJack Eighteen
UnionJack Nineteen
UnionJack Twenty
UnionJack TwentyOne
UnionJack TwentyTwo
UnionJack TwentyThree
UnionJack TwentyFour
UnionJack TwentyFive
UnionJack TwentySix
UnionJack TwentySeven
UnionJack TwentyEight
UnionJack TwentyNine
UnionJack Thirty
UnionJack ThirtyOne
UnionJack ThirtyTwo
UnionJack ThirtyThree
UnionJack ThirtyFour
UnionJack ThirtyFive
UnionJack ThirtySix

@jdaw1
Copy link

jdaw1 commented Oct 26, 2022

Solution found.
• The large image cannot be a link (which is what confounded me).
• Then wrap the <a names="…"></a> around the image.

@micmro
Copy link

micmro commented May 31, 2023

Just wanted to leave a note that this approach does have accessibility/semantic issues (ref: https://developers.google.com/style/headings-targets).

Unfortunately, Github does currently not support the extended markdown syntax heading-ids (e.g. ### My Heading {#my-heading-id}), so the most accessible approach would be to use the HTML equivalent <h3 id="my-heading-id">My Heading</h3> instead.

@Moonbase59
Copy link

Moonbase59 commented Apr 4, 2024

I needed a quick Markdown TOC generator that would be compatible with GitHub, so I made my own: gh-toc.

It’s basically a web page with some JavaScript: You paste in your Markdown and it generates a GitHub-compatible Table of Contents. Your browser does the work and nothing gets sent to the Internet.

Features:

  • Full Unicode support, all international characters.
  • Better handling of (hopefully) all underscore cases.
  • Correct handling of code with backticks ` inside.
  • Much better code block support (backticks, tildes, differing number of them).
  • YAML front matter support.
  • Optional output of the full Markdown file, ToC inserted between <!-- ToC begin --> and <!-- ToC end --> HTML comments, instead of just the ToC.
  • Optional named anchor generation for each ToC element (not needed for GitHub use).
  • Optional use of id instead name attribute in generated anchors.

Markdown test file included.

@k-Gillespie
Copy link

thank you

@Ricky4K
Copy link

Ricky4K commented Jul 10, 2024

Is there any way to cross reference to another files header?

For example:

File1 contains

# Title
## Description
Lorem Ipsum

File2 contains

# Data
Click here to see the Description from File 1: [Information](../File1#Description)

@Ricky4K
Copy link

Ricky4K commented Jul 10, 2024

Is there any way to cross reference to another files header?

For example:

File 1 contains

# Title
## Description
Lorem Ipsum

File2 contains

# Data
Click here to see the Description from File 1: [Information](../File1#Description)

Looks like it does work like that however keep in mind if you have special Characters as a File name (at least for me) it did not properly show up in VSC. However after just pasting it in manually and clicking it via the preview it should work out just fine. Note the %20 stands for a blank space character here.

For example:

File 1 contains

# Title
## Description
Lorem Ipsum

File2 contains

# Data
Click here to see the Description from File 1: [Information](../File%201#Description)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment