Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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)

@ntrel
Copy link

ntrel commented Jun 29, 2020

Does Markdown have any way of just linking to a heading without repeating yourself?

I want to link to [Heading] without having to type [Heading](#heading).

Heading
-------

When Heading is long this is very tiresome.

@JeffreyBenjaminBrown
Copy link

JeffreyBenjaminBrown commented Jun 29, 2020

Linking to a subsection of another file in the same repo is also possible:

[description](../relative/link/to/file.md#section-name-with-dashes-instead-of-spaces)

Capitalization matters.

Some extraneous newlines make the above a little more readable:

[description]
(../relative/link/to/file.md
#
section-name-with-dashes-instead-of-spaces)

I imagine the same trick works if you give an absolute link to a markdown file in a different repo.

@HugoCortell
Copy link

HugoCortell commented Jul 4, 2020

How can I make an anchor that does not render?
I have a page that is constantly updated and I wanted to make a link that leads to the button of the page, so I added a textless anchor to the button, but it does not work!

@rupertjeff
Copy link

rupertjeff commented Jul 6, 2020

@ntrel Try this alternate method of linking:

I want to link to [Heading] without having to type [Heading].

Heading
--------

... more copy here as needed...

[Heading]: #heading

Wherever it sees [Heading], Markdown will look for a corresponding link definition, and if it finds one, it will associate it, as long as it's an exact match.

@ntrel
Copy link

ntrel commented Jul 7, 2020

@rupertjeff That's useful if I have multiple links to a long anchor in the same document. I meant that if the link title and anchor id are the same except for the automatic lowercase and hyphenation, it would be nice not to have to repeat the anchor, just like on a wiki.

@jesshart
Copy link

jesshart commented Jul 15, 2020

Note if you test this in "Edit file" mode, you may be warned that it will take you off the page. In my case, I got this warning. Once I committed the changes my anchor link was working perfectly.

@Pavivenkatesan
Copy link

Pavivenkatesan commented Jul 17, 2020

What if I have two headings with same name and I want the anchor link to take me to the latter one and not the former?

@saulhg
Copy link

saulhg commented Aug 19, 2020

I am using Markdown web part in Sharepoint and the anchors were not working for me... Then, I realised why.
I wrote #Definición (note the Spanish accent in the "o") and was trying to link to it with [Ir a la definición](#definición).
This didn't work, But then, I checked by viewing source in the Chrome browser and noticed that the accented o was omitted. So, writing [Ir a definición](#definicin) worked just fine!
So, characters like á,é,í,ó,ú,Á,É,Í,Ó,Ú,Ñ,ñ are just omitted in anchors.

@chongchonghe
Copy link

chongchonghe commented Aug 23, 2020

Thanks! Very helpful.

Is there a way to create a short name for the section title? Something like this:

# [Star Formation Theory][sft]
[Reference to this section](#sft)

@rv404674
Copy link

rv404674 commented Sep 20, 2020

If one of my heading is this "## What is goRubu? 🚀", then doing this isn't working?

  • What is goRubu?

Any idea how to fix this?

@Amityville-DE
Copy link

Amityville-DE commented Nov 4, 2020

👍 thank you

@Praful932
Copy link

Praful932 commented Nov 7, 2020

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 😁

@Cavitedev
Copy link

Cavitedev commented Nov 16, 2020

Write with this structure

[anchor](#heading-to-achor)

you can write anything on anchor.
on the heading you can simply copy and paste the link when clicking on this symbol
image...
on github.
The url will end with the #heading-to-anchor text
image...

@yaronelh
Copy link

yaronelh commented Nov 26, 2020

A few additional comments that I don't know if were mentioned is that

  1. It doesn't matter if you're using # ## ### to size your titles they link will always have only one # like this [text text](#link-text)
  2. Obviously your title Won't have - rather they'll have spaces, In the link itself replace spaces with dashes
  3. If you're title ends with that a ? remove the ? in the link not [text text](#link-text?) rather [text text](#link-text)
  4. At the point of writing, anchor links are not reflected in the preview, you'll have to commit to see if it worked

Hope these helped someone

@AlphaPav
Copy link

AlphaPav commented Dec 18, 2020

Works like a charm. Make sure the fragment link is all in lowercase

# [Heading Link](#section-i-want)

## [Section I Want] 

thanks so much!!!

@mcascone
Copy link

mcascone commented Jan 14, 2021

This is great, thanks! I just wanted to add:

  1. At the point of writing, anchor links are not reflected in the preview, you'll have to commit to see if it worked

In VSCode with the GitHub Markdown Preview extension, anchor links work perfectly, as do file links.

@yaronelh

@showcove
Copy link

showcove commented Jan 22, 2021

@Aissaoui-Ahmed
Copy link

Aissaoui-Ahmed commented Jan 27, 2021

@Lemanja
Copy link

Lemanja commented Feb 16, 2021

Thank you so much, I find this works amazingly! :)

@Esemega
Copy link

Esemega commented Feb 20, 2021

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

IZVRStudio commented Sep 16, 2021

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

@jabielecki
Copy link

jabielecki commented Sep 28, 2021

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

Try #11-header

@IZVRStudio
Copy link

IZVRStudio commented Sep 28, 2021

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

DevJonTaylor commented Feb 27, 2022

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

HelderMagalhaes commented Mar 3, 2022

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

tillmannheigel commented Mar 3, 2022

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!

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