28

Following this Stack Overflow answer, I added a named anchor ElimRestErrs to a heading in an answer I posted:

# <a name="ElimRestErrs">Eliminating the rest of the errors</a>

The link to the answer is https://stackoverflow.com/a/76785036/2153235. I expected to be able to link to the heading using https://stackoverflow.com/a/76785036/2153235#ElimRestErrs

It didn't work. The above just links to the answer itself. The leading comment on the answer that I was following says:

You can't see how to link to your heading demo after StackOverflow renders the HTML because their rendered is stripping out your tag. That is, you can't in StackOverflow Markdown.

If I understood that correctly, and it explains why I can't link to a named anchor, then is there any other way to link to a specific location in a question or answer on Stack Overflow? I just want to link to a specific location, even if the mechanism is not a named anchor.

7
  • 2
    No, because (as you know) the SO renderer strips it out; there's no named anchor to link to. You can see this in the rendered answer.
    – jonrsharpe
    Aug 2 at 20:21
  • 4
    cross-site dup? please see Automatically add id attributes to headers to enable link targeting (MSE)
    – starball
    Aug 2 at 21:38
  • 2
    Good question, but since this isn't specific to Stack Overflow, but instead applicable to all Stack Exchange sites, meta.stackexchange.com would be a better place to ask.
    – M. Justin
    Aug 2 at 22:04
  • 4
    I don't believe this question should be closed, and I'm pretty sure this is not how we're supposed to use the "not about SO / software powering SO" close-reason. cc @M.Justin
    – starball
    Aug 2 at 22:12
  • @starball I'm currently trying and failing to find a Meta question or Help Center topic that deals with how to handle network-wide questions when asked for a specific site.
    – M. Justin
    Aug 2 at 22:23
  • @starball: I checked out the Q&A that you cited. It's relevant, but not the same. Your reference talks about a feature request, specifically to allow external links to target headers. I'm asking whether there is currently any way to link to a location within an SO question or answer If fine granularity of positioning is limited, it's still better than nothing, but it needn't be using named anchors or restricted to headers. Aug 3 at 5:39
  • 3
    Related: Make headings anchor-jumpable
    – Erik A
    Aug 3 at 9:28

2 Answers 2

18

You can't, at least not using the formatting (Markdown or HTML) available in Q&A (or even tag wikis). Collective articles (example) automatically get header anchors.


One alternative is to use text fragments. In Chrome (and related browsers), select the text, right click/tap, and "Copy link to highlight"/"Create link". This will work for any browsers that implement this feature (e.g. Chrome; but not Firefox). The links look like this:

https://stackoverflow.com/questions/5319754/cross-reference-named-anchor-in-markdown#:~:text=note%20on%20self%2Dclosing%20tags%20and%20id%3D%20versus%20name%3D:~:text=out%20the%20anchor.)-,Note,-on%20self%2Dclosing

(Unfortunately, it seems to break when posted in an answer. But it works otherwise.)

8
  • Thanks. I'm not fond of a solution that is browser specific, especially to a browser that I don't use. I clarified the question to say that I am not wedded to the named anchor mechanism (but that's the only one I found from web searching). Hopefully this opens the possible answers up to other solutions. However, it seems that your answers already thinks outside the "named anchor" box. Aug 2 at 23:45
  • 7
    I suggest changing your first sentence to something like "You can't on SO/SE questions and answers on main and meta sites, as the Markdown/HTML parser which SO/SE uses for questions and answers on main and meta sites doesn't permit the features which would be needed to accomplish this." There's a lot of hedging there, but such anchors do exist automatically in collective articles example. Interestingly, automatically creating the needed anchors was enabled for posts on main and meta sites for a couple of weeks in July of 2021.
    – Makyen Mod
    Aug 3 at 0:08
  • I checked out the cited "collective articles example", but admit that I don't understand it in the slightest. However, it seems from Makyen's comment above that this potential solution hasn't been available since July of 2021. Aug 3 at 5:20
  • 1
    BTW: URLs using text fragments don't work correctly when using Markdown or bare URLs in posts. They do work when placed in comments. IIRC, you can use an HTML <a href="URL with text fragment here">link text</a> to have a URL with a text fragment work in a question or answer.
    – Makyen Mod
    Aug 3 at 5:20
  • I'd be looking for something to use in posts. Thanks. Aug 3 at 5:40
  • 3
    @user2153235 text fragments have okay (not great) browser support. Not supported on Firefox at the time of this writing
    – starball
    Aug 3 at 6:03
  • 1
    It seems to me that a problem with named anchors and text fragments is that there is no guarantee for uniqueness. Nothing prevents some other user from writing an answer that uses the same anchor name or that has text that also matches the fragment.
    – jamesdlin
    Aug 4 at 5:19
  • 1
    @jamesdlin Explicit anchors could be namespaced, though: the Markdown renderer could prefix the user-supplied anchor ID with the post ID, guaranteeing uniqueness (and also not clashing with UI elements). Aug 4 at 15:17
6

Auto generating header ids would solve this

I also hoped it would be possible to link to a specific header in a Stack Exchange question/answer. I really like that it's possible to do that in, for example, a GitHub readme.

This comment suggests something even more ambitious:

why restrict it to headlines? Examples are list elements, code sections, and a particular line in a code section.

Text fragments are a very inferior workaround

A trick that may work for some browsers is using text-fragments to direct a user to a specific part of the page. This is inferior in a bunch of ways though (currently not supported by all major browsers, it's more complicated and you first have to url encode characters which is an extra step of complexity, and other possible issues1)

E.g. https://stackoverflow.com/questions/5319754/cross-reference-named-anchor-in-markdown/7335259#:~:text=An%20earlier%20version%20of%20this%20post%20suggested%20using

Note: clicking on the link above containing the text fragment is not enough, the user would have to know to copy the link into the address bar.

enter image description here

1 I couldn't get the text fragment to highlight the header 'Note on self-closing ..'. I'm not sure why (perhaps text fragments don't support headers).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .