Articles on Stack Overflow and Stack Overflow for Teams can have tables of content
For articles, the backend Markdown to HTML conversion process automatically adds an id
attribute to header elements, which allows targeting a link to the headers. The format of the automatically generated id
has changed at least once over time. The automatically generated id
is based on the text of the header, so if the header text changes, then the id
, and thus the URL, will change. Currently, it's the text "header-" followed by "all-the-words-in-the-header-converted-to-lowercase-and-joined-with-hyphens".
While you can determine prior to saving what the id
will be, and thus what URL to use, you're probably better off first saving a draft of the article, copying the URLs needed for the headers you desire to link to in your table of contents, then editing the article to add links to those URLs. Links can be in any supported Markdown or HTML link format (e.g. the basic Markdown format of [link text](URL)
; see the Markdown editing help for more detail about how to create links).
You can probably manually assign an id
to explicit HTML header elements (I haven't verified current availability of this feature)
Being able to manually assign an id
was possible for <hx>
elements which were explicitly placed within the Markdown as HTML text. In other words, <h4 id="my-unique-manual-id">Some header text</h4>
would produce a level 4 header element with the id
of my-unique-manual-id
. This feature was available when header ID generation was briefly turned on for Stack Exchange in general. I don't know if it was retained after id
support was limited to only in articles.
Other types of posts can not have a table of contents which is linked to content within a Stack Exchange post.
Unlike articles, the Markdown to HTML conversion for other types of posts does not add id
attributes to header elements. There is, currently, no way to create link targets (i.e. anchors with a name
1 or elements with an id
) within a Stack Exchange post.2 Given that you can't create a link target within an SE post, there's no way to have links within your SE post which link to somewhere else within the same post (or target within almost any Stack Exchange post).
You can link to other SE posts, but you must use either a path-relative URL (e.g. /a/123456
) or a protocol-relative URL (e.g. //stackoverflow.com/a/123456
), as using a fragment-relative (e.g. #answer-123456
, normally used for links within the same page) will result in the <a>
element being stripped when Stack Exchange converts the post from Markdown to HTML.
For more detail of what HTML is permitted in Stack Exchange posts, see: "What HTML tags are allowed on Stack Exchange sites?"
- Using a
name
attribute with an <a>
element is deprecated. An id
attribute should be used instead.
- There was a brief time (couple/few weeks in 2021-07) where Stack Exchange automatically created
id
attributes for header elements within posts and allowed custom id
attributes for explicit HTML header elements (e.g. <h4 id="foobar">
). Unfortunately, generating/permitting those id
attributes was turned off after only a short time. These were created during the Markdown➞HTML conversion process. Stack Exchange only does the Markdown➞HTML conversion process upon the post being posted or edited, so you may find some posts which were posted or edited in the timeframe during which this was enabled which still have internal anchors.
https://stackoverflow.com/c/
. I would appreciate you confirming in a comment here, or in your question, which version of the Stack Overflow for Teams product you're using.