How to Keep HTML Anchor Links (#fragments) Working After Translating Headings
Keep anchor links working after translation by treating the HTML id as a stable identifier and translating only the heading and link text. This protects table-of-contents links, bookmarks, and cross-page URLs such as /en/help/#fragments; the main production caveat is that Markdown processors and CMSs often regenerate IDs from the translated heading unless you explicitly pin them.
A browser follows #fragments by looking for the corresponding target in the destination document. It doesn’t translate the fragment or look for a heading with similar wording. If the page URL stays the same but the target ID changes, an old link can still load the right page without taking the reader to the right section.
Translate the heading, not its identifier
Start by separating the three pieces of a jump link: the label readers see, the fragment in href, and the target element’s id. The label can change freely. The fragment and ID have to agree.
Broken: the heading ID changes but the link doesn’t
<nav aria-label="Sur cette page">
<a href="#payment-options">Moyens de paiement</a>
</nav>
<h2 id="moyens-de-paiement">Moyens de paiement</h2>
<p>Vous pouvez payer par carte ou par virement bancaire.</p>
The label and heading are correctly translated, but there’s no element with id="payment-options". Updating the navigation link would fix the click on this page, but it wouldn’t repair a bookmark or a support email containing the old fragment.
Fixed: keep the published ID in both places
<nav aria-label="Sur cette page">
<a href="#payment-options">Moyens de paiement</a>
</nav>
<h2 id="payment-options">Moyens de paiement</h2>
<p>Vous pouvez payer par carte ou par virement bancaire.</p>
The browser can now find the target, and readers see French text in both the navigation and the heading. Put the ID on the heading itself when possible so the link lands where the section starts. Use the same ID on equivalent headings across locales if the locale pages are separate documents and you want to carry the fragment across when switching languages.
Never regenerate a published heading ID from its current wording. Once an anchor ships, treat it as part of the page’s public URL.
Pin the ID before the page renderer generates a slug
A heading written as ## Payment options doesn’t define a stable ID by itself. Any slug comes from the renderer, and rules for punctuation, accents, and repeated headings can differ between tools. Even within one tool, inserting a second heading with the same title can change which section gets a suffix such as -2.
section_id: payment-options
heading: "Moyens de paiement"
toc_label: "Moyens de paiement"
toc_accessible_name: "Sur cette page"
These are example content fields, not a universal CMS format. Store section_id once on the shared section record, or make it a locked field replicated across locales when the CMS can’t share it. Expose the other fields to translators and make sure both the heading and navigation templates read the same stored ID.
<nav aria-label="{{ page.toc_accessible_name | escape }}">
{% for section in page.sections %}
<a href="#{{ section.section_id | escape }}">{{ section.toc_label | escape }}</a>
{% endfor %}
</nav>
{% for section in page.sections %}
<h2 id="{{ section.section_id | escape }}">{{ section.heading | escape }}</h2>
{% endfor %}
This Liquid example assumes IDs are validated at the content boundary against a convention such as ^[a-z][a-z0-9-]*$, with uniqueness checked across the complete page. The escape filter handles HTML escaping; it isn’t a fragment encoder or a uniqueness check. Inspect the rendered HTML because a theme or heading plugin can still overwrite explicit IDs or add competing anchors.
If your Markdown renderer supports explicit heading IDs, use its documented syntax and verify the result after export. If it doesn’t, use a heading component or a raw HTML heading when the renderer allows it. Don’t assume the editor preview and the published site use the same renderer or sanitizer.
Choose the right representation for each link
| Situation | Link and target | What to verify |
|---|---|---|
| Jump within the current page | href="#payment-options" → id="payment-options" | Exactly one matching ID in the document, including shared components. |
| Link to a translated page | href="/fr/aide/#payment-options" → id="payment-options" | The French route resolves and its version of the page exposes that ID. |
| Preserve an existing non-ASCII ID | href="#caf%C3%A9" → id="café" | The fragment decodes to the actual ID. Don’t store caf%C3%A9 as the ID. |
| Link from an email to a web section | href="https://example.invalid/fr/aide/#payment-options" → id="payment-options" | Replace the reserved example host with your site and test the link from an actual send if tracking rewrites it. |
IDs are case-sensitive and can’t contain ASCII whitespace. A lowercase ASCII convention avoids many encoding and normalization problems for new IDs, but it isn’t a reason to rename existing ones. With non-ASCII IDs, two strings can look identical yet use different Unicode sequences. Generate the fragment from the stored ID rather than retyping it from the heading.
Preserve old links during an anchor migration
If a translated page already published #moyens-de-paiement and you’re moving to #payment-options, keep both targets. Point new links to the canonical heading ID and retain the old ID on a wrapper that starts at the same section.
<nav aria-label="Sur cette page">
<a href="#payment-options">Moyens de paiement</a>
</nav>
<section id="moyens-de-paiement" aria-labelledby="payment-options">
<h2 id="payment-options">Moyens de paiement</h2>
<p>Vous pouvez payer par carte ou par virement bancaire.</p>
</section>
The old fragment lands on the section, while the canonical one lands on the heading. The aria-labelledby reference gives the wrapper its accessible name from that same heading. Test both links after deployment, especially if the CMS drops attributes from imported markup.
Don’t rely on a server redirect to map an old fragment to a new one. The browser doesn’t send the fragment in the HTTP request, so the server can’t select a redirect based on it.
For locale switches, carry the current fragment only if the destination has an equivalent target. If older locales use different IDs, map them from a shared section key instead of translating the slug as text. When the destination lacks that section entirely, drop the fragment and link to the page rather than sending readers to a dead target.
Keep the target visible below sticky navigation
A valid anchor can look broken when the browser scrolls the heading under a sticky header. Translated navigation may wrap onto more lines, so the offset that worked in the source language may be too small in another locale.
:root {
--anchor-offset: 5rem;
}
main h2[id],
main section[id] {
scroll-margin-block-start: var(--anchor-offset);
}
@media (max-width: 40rem) {
:root {
--anchor-offset: 7rem;
}
}
The logical scroll margin leaves space before both the canonical heading and the legacy wrapper when they’re scrolled into view. The values are examples: set them to match the actual header height plus a readable gap. Check narrow screens and text zoom in the longest locale; CSS can fix where a valid target appears, but it can’t fix a missing ID.
Validate the published links, not just the translated copy
- Inspect the generated page. Search for each key
idin the delivered HTML and inspect the live DOM after any client rendering. Confirm that each target occurs once and isn’t lost when the page hydrates. - Test cold entry and in-page clicks. Open the full localized URL with
#payment-optionsin a new tab, then test the table of contents by keyboard. A working click after the app has loaded doesn’t prove that direct entry works. - Test routes and aliases separately. Open an old link ending in
#moyens-de-paiement, then switch between locales. If the link goes through a redirect or email tracker, use the distributed link and inspect the final URL. - Check which document resolves the URL. If a local jump leaves the page, look for a
<base href="…">element or a router handler. A fragment-onlyhrefresolves against the document’s base URL, which isn’t always the URL in the address bar.
Use HTML Translate to compare localized HTML exports and spot anchor IDs or link fragments that changed during translation.
Common anchor-link mistakes and fixes
- The link loads the right page but doesn’t scroll. Compare
href="#payment-options"with the exact targetid, including case, and check that the target exists on that locale’s page. - The browser jumps to the wrong section. Look for repeated
idvalues, including in a second instance of a reusable component. Keep the published ID on the intended target and give the other instance a unique one. - The table of contents works until a rebuild. A renderer is deriving IDs from heading text or numbering duplicates. Store an explicit
section_idand have the table of contents consume that same value. - An accented anchor works in one export but not another. Check for mixed Unicode normalization or double encoding, such as
#caf%25C3%25A9. Generate#caf%C3%A9from the actualid="café"once. - An ID looks right but never matches. The markup may contain
id="#payment-options". Keep the leading#inhref, not in the ID for that target. - The heading disappears behind the header. The jump may be working. Apply
scroll-margin-block-startto the actual target and test with the localized header at narrow widths.
HTML File Translation Questions
Do heading IDs have to be in the page’s language?
No. Readers and assistive technology get the heading text from the element’s content, not from its id. You can publish <h2 id="payment-options">Moyens de paiement</h2> without translating the ID.
Can every locale use the same heading IDs?
Yes, if each locale is a separate document. IDs must be unique within a document, not across the site. If multiple translations appear in one DOM, give each heading a distinct ID, such as payment-options-fr, and point each link to the intended version.
Can a heading have two IDs?
No. Put the canonical id on the heading and the legacy ID on a separate element at the same section. Don’t use a second id attribute or put two space-separated values in one attribute.
Should the table of contents be translated independently?
Its label can be translated independently, especially when it needs to be shorter than the heading. Its href should come from the same stored ID as the heading, not from either translated label.
Will anchors still work with JavaScript disabled?
Yes, if the destination document already contains the matching id and the link is a normal <a href="#payment-options">. If JavaScript creates the target or replaces native navigation, test direct fragment URLs with that rendering and routing behavior in place.