Template:Anchor
| This template uses Lua: |
| This template is used in system messages, and on approximately 91,000 pages. Changes to it can cause immediate changes to the Wikipedia user interface. To avoid major disruption, any changes should be tested in the template's /sandbox or /testcases subpages, or in your own user subpage. The tested changes can be added to this page in a single edit. Please discuss changes on the talk page before implementing them. |
| File:Logo wikibot.svg | This template is used by one or more bots in their standard operation. Any breaking changes to this template, including moving it or nominating it for deletion, must be communicated in advance to the bot operator(s). The relevant bots are: User:Cewbot (BRFA). |
The template {{anchor}} inserts one or more invisible anchor names (HTML fragment identifiers) in a page.
Anchors are most useful with sections, since links to a section will not work if a section is renamed. However, anchors can be used as needed anywhere in an article.
Basic format
| This template should always be substituted within a section heading when the intended target is a section title, otherwise it will cause technical issues. |
The basic format is {{anchor|Anchor name}} for parts on a page, and {{subst:anchor|Anchor name}} for use in headings.
More than one name can be given, separated by |, which can be useful for alternative spellings such as "humor" and "humour": {{anchor|humor|humour}}
In general, if the intended target of an anchor is a section title, then it should be placed at the end of the section header by substitution:
== Section header{{subst:anchor|Anchor name}} ==
which will be saved in the article as:
== Section header<syntaxhighlight lang="text" class="" style="" inline="1"></syntaxhighlight> ==
An anchor name can be arbitrary, but the first letter should generally be capitalized in keeping with header style.
Note that anchor names are case sensitive, so [[#Anchor name]] and [[#Anchor Name]] are not equivalent.
Linking to an anchor
To link to an anchor from within the same page, use [[#Anchor name|display text]]. To link to an anchor from another page, use [[Article name#Anchor name|display text]].
Note that #Anchor name, used by the MediaWiki software to (usually) direct users to sections within a page, is not a wikitext directive like <syntaxhighlight lang="wikitext" inline>#REDIRECT</syntaxhighlight>.
Limitations
| Character | Code | Template | Magic word | Meaning |
|---|---|---|---|---|
| " | ""
|
— | — | (double) quotation mark |
| # | ##
|
{{hash}}
|
— | hash |
| | | |&vertbar;
|
{{pipe}}
|
{{!}}
|
pipe |
| = | ==
|
— | {{=}}
|
equals |
- Anchor names that contain any character shown in the table will not work as expected. However, any of these characters can be replaced with the HTML character entity reference "&#" codes shown. Or, the pipe symbol and equals sign can be worked around with
{{!}}and{{=}}, respectively. Markup such as<sup>and<sub>(superscript and subscript) cannot be used. Most other characters, including whitespace and punctuation, are not a problem. - Anchor names must be unique on a page, and must not duplicate any heading titles. Duplicate anchors will not work as expected since the
#locationlinks go to the first anchor with that name. Duplicate anchors result in invalid HTML; you can check for duplicate anchors by running the page through the W3C Markup Validation Service. - Anchor links are case-sensitive in some browsers, so treat all anchor links as case-sensitive when creating links to them. For example, if you create the anchor with
=== Foo{{subst:anchor|Bar}} ===, link to it with[[Foo#Bar]], not[[Foo#bAR]]. However, because some browsers are not case-sensitive, do not create section titles or anchors that differ only in case from others on the page. That is, do not create both=== Foo{{subst:anchor|Bar}} ===and=== Foo{{subst:anchor|bAR}} ===. - If a template is left unsubstituted inside a section title (which should never be done, see #Rationale for substitution in the header), then the template code will appear in the edit summary window each time a section edit of that section begins, as in
/* Limitations{{anchor|Issues}} */ New issue. The editor must then manually remove the anchor template from the edit summary window, or else when they save the section, the browser may not return to the section and the section link of that edit in the history page will not work.
Use in tables
| To see how to set anchors in tables, see Help:Tables and locations § Section link or map link to a row anchor. Only use this template in tables if you need multiple anchors for the same row. |
Anchors may be used within tables, subject to certain restrictions. The {{anchor}} template may be used in the caption and cells of a table, but not those portions of a table that are outside the caption and cells. Unlike in section headings, the template does not have to be substituted. It is used on the table's caption thus:
<syntaxhighlight lang="wikitext">
|+ A table caption
</syntaxhighlight>
and the following forms of cell are valid:
<syntaxhighlight lang="wikitext">
! A header cell
! style="background:white;" | A header cell with styling
| A data cell
| rowspan="2" | A data cell spanning two rows
</syntaxhighlight>
You need to ensure that the {{anchor}} is not in that portion of the markup intended for the classes, styles etc. Thus, {{anchor}} cannot be placed anywhere on lines that begin with {| (start of table) or |- (new row), and the following forms of cell are not valid:
<syntaxhighlight lang="wikitext">
! | A header cell
! style="background: white;" | A header cell with styling
| | A data cell
| rowspan="2" | A data cell spanning two rows
</syntaxhighlight>
If it is necessary for an anchor to be in any of these positions, a different technique is used—the id= attribute. This is placed in that portion of the markup where the classes, styles etc. may be used, as follows:
<syntaxhighlight lang="wikitext">
| A header cell | A header cell with styling | A data cell | A data cell spanning two rows
</syntaxhighlight>
The If Rationale for substitution in the headerAnchors should never be left unsubstituted within a section header, as this violates MOS:HEADINGS and causes technical complications. More specifically: if To show why placement in the header is preferred, the following anchor is placed in the Basic format section header:
Since the anchor is actually used on this documentation page, the links #Basic format, #Foo and #In-Foo all work and can be tested to see the effect. For contrast, an anchor named #Under-Foo has also been placed underneath the Basic format section header and can be clicked to illustrate how this placement hides the section title when jumping to the linked target on some web browsers:
Another anchor named #Above-Foo has been placed above the section header. This anchor does work correctly for standard desktop skins, but because the anchor is technically not in the section but before it, mobile views will not expand the desired section, and it cannot be edited by editing the section.
Explanations and examplesReasons for the above being best practice are detailed in the following additional, numbered examples, as well as in § Limitations section:
TemplateDataThis is the TemplateData for this template used by TemplateWizard, VisualEditor and other tools. See a monthly parameter usage report for Template:Anchor in articles based on its TemplateData.
TemplateData for Anchor <templatedata>{ "description": "Add HTML anchors to a page. Make a link to your anchor with #Anchor on same page or Page you're linking to#Anchor on that page. Every anchor on a page must be different. Do not use the characters \" or # or | or =.", "params": { "1": { "label": "First anchor", "type": "string", "required": true, "description": "Name of anchor", "example": "Anchor name 1" }, "2": { "label": "Second anchor", "type": "string", "required": false, "description": "Name of anchor" }, "3": { "label": "Third anchor", "type": "string", "required": false, "description": "Name of anchor" }, "4": { "label": "Fourth anchor", "description": "To add more than four anchors, go to source editing.", "type": "string", "required": false } }, "format": "inline" }</templatedata> See also
|
|---|