144

I'm trying to figure out how to reference another area of a page with Markdown. I can get it working if I add a

<div id="mylink" /> 

and for the link do:

[My link](#mylink)

But my guess is that there's some other way to do an in-page link in Markdown that doesn't involve the straight up div tag.

Any ideas?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Chris
  • 1,479
  • 2
  • 11
  • 4
  • I've always done them just the way you've done here. – Joe Martinez Jul 20 '10 at 18:07
  • 2
    `
    ` can upset the formatter for the rest of the page. Use ``. See my answer.
    – Steve Powell Sep 07 '11 at 14:44
  • Possible duplicate of [Cross-reference (named anchor) in markdown](http://stackoverflow.com/questions/5319754/cross-reference-named-anchor-in-markdown) – Ulysse BN Nov 27 '16 at 01:01
  • Consider using MultiMarkdown instead. It provides the `[anchor][]` syntax to do this. https://github.com/fletcher/MultiMarkdown/wiki/MultiMarkdown-Syntax-Guide#automatic-cross-references – jwpfox Jan 11 '18 at 04:40

6 Answers6

186

See this answer.

In summary make a destination with

<a name="sometext"></a>

inserted anywhere in your markdown markup (for example in a header:

## heading<a name="headin"></a>

and link to it using the markdown linkage:

[This is the link text](#headin)

or

[some text](#sometext)

Don't use <div> -- this will mess up the layout for many renderers.

(I have changed id= to name= above. See this answer for the tedious explanation.)

Community
  • 1
  • 1
Steve Powell
  • 25,354
  • 8
  • 41
  • 44
  • 9
    +1, thanks. It's sad it doesn't do this automatically. Very wet IMHO. – Marc-André Lafortune Feb 26 '13 at 20:31
  • It's ugly when you have to read it in a text editor, but, it _does_ work. Not sure why this isn't the accepted answer. – kayleeFrye_onDeck Nov 16 '16 at 22:02
  • 2
    Defining old style anchors `` is [*obsolete but conforming*](https://www.w3.org/TR/html5/obsolete.html#obsolete-but-conforming-features) in later HTML specifications. If `
    ` might be rendered as a disrupting block (maybe if for undisclosed reasons the CSS is giving a size) maybe `` (an inline element) might do the trick?
    – Javier Nov 09 '17 at 17:11
36

I guess this depends on what you're using to generate html from your markdown. I noticed, that jekyll (it's used by gihub.io pages by default) automatically adds the id="" attribute to headings in the html it generates.

For example if you're markdown is

My header
---------

The resulting html will look like this:

<h2 id="my-header">My header</h2>

So you can link to it simply by [My link](#my-header)

Ryan B
  • 3,364
  • 21
  • 35
bodgix
  • 481
  • 4
  • 5
  • 1
    This is the best and clean answer. Markdown is really cool. it really helps to do fast blogging. ^_^ – Ashok M A Mar 23 '19 at 21:01
  • I can confirm that this works for ruby gems hosted at rubygems.org as well. All the documentation from headers in the .md file will have these id entries. I needed to find out because I had to document ruby-gtk content, and allow quick jumping to the upstream classes (which are written in C, in GTK or GTK+). – shevy May 15 '21 at 02:05
  • I see that pandoc, Markdown Preview Enhanced for vscode and vscode native preview will also add this id attribute. However this is extension behavior and is not in CommonMark specification. CommonMark implementations like [cmark](https://github.com/commonmark/cmark) won't generate the id attribute. – iouzzr Dec 13 '22 at 08:20
18

With the PHP version of Markdown, you can also link headers to fragment identifiers within the page using a syntax like either of the following, as documented here

Header 1            {#header1}
========

## Header 2 ##      {#header2}

and then

[Link back to header 1](#header1)
[Link back to header 2](#header2)

Unfortunately this syntax is currently only supported for headers, but at least it could be useful for building a table of contents.

Klokie
  • 189
  • 1
  • 5
4

The destination anchor for a link in an HTML page may be any element with an id attribute. See Links on the W3C site. Here's a quote from the relevant section:

Destination anchors in HTML documents may be specified either by the A element (naming it with the name attribute), or by any other element (naming with the id attribute).

Markdown treats HTML as HTML (see Inline HTML), so you can create your fragment identifiers from any element you like. If, for example, you want to link to a paragraph, just wrap the paragraph in a paragraph tag, and include an id:

<p id="mylink">Lorem ipsum dolor sit amet...</p>

Then use your standard Markdown [My link](#mylink) to create a link to fragment anchor. This will help to keep your HTML clean, as there's no need for extra markup.

Mike
  • 21,301
  • 2
  • 42
  • 65
  • In my experience using the `

    ` tag in Markdown can strip the CSS of a regular paragraph. I'd say use with caution, I'm new to Markdown but it has some quirks.

    – 2rs2ts Jul 04 '11 at 15:57
  • @user691859 I'm not sure what you mean by "using the `

    ` tag in Markdown can strip the CSS of a regular paragraph". Markdown wraps paragraphs in `

    ` tags, and ignores those that already have `

    ` tags. I can't see how this would affect CSS...

    – Mike Jul 05 '11 at 18:29
  • The behavior is erratic for me. On tumblr, using

    can (not always) strip all the behavior besides the specific behavior I specified. I do not know why.

    ALWAYS does the same though.
    – 2rs2ts Jul 06 '11 at 17:25
3

For anyone use Visual Studio Team Foundation Server (TFS) 2015, it really does not like embedded <a> or <div> elements, at least in headers. It also doesn't like emoji in headers either:

###  Configuration 

Lorem ipsum problem fixem.

Gets translated to:

<h3 id="-configuration-"> Configuration </h3>
<p>Lorem ipsum problem fixem.</p>

And so links should either use that id (which breaks this and other preview extensions in Visual Studio), or remove the emoji:

Here's [how to setup](#-configuration-) // Configuration 
Here's [how to setup](#configuration) //Configuration

Where the latter version works both online in TFS and in the markdown preview of Visual Studio.

Nick
  • 4,901
  • 40
  • 61
0

In Pandoc Markdown you can set anchors on arbitrary spans inside a paragraph using syntax [span]{#anchor}, e.g.:

Paragraph, containing [arbitrary text]{#mylink}.

And then reference it as usual: [My link](#mylink).

If you want to reference a whole paragraph then the most straightforward way is to add an empty span right in the beginning of the paragraph:

[]{#mylink}
Paragraph text.