Section links jump to the wrong place: duplicate HTML IDs
Find duplicate IDs behind broken section links, trace heading-slug collisions and repeated components, and verify stable anchors after the repair.
By Crawlito · 5 min read
In short
- An HTML id should identify one element within the document.
- Repeated headings or cloned components can create ambiguous section destinations.
- Repair the ID generator and update references together.
On this page
If a section link jumps to the wrong heading, inspect the document for duplicate IDs before changing scroll behavior. Repeated heading labels, copied components, or multiple article blocks can generate the same identifier. Give each intended target a stable unique ID and update its links, then verify direct fragment navigation.
A page can look correct while its table of contents, shared section links, and accessibility references point to the wrong element. The defect is in the relationship between identifiers and references.
Reproduce a direct fragment visit
Copy the failing section URL and open it in a fresh tab. Record the intended heading and where the browser actually lands.
Then use the page's own table of contents link. If the direct URL and internal click differ, inspect any client-side scroll code as well as the underlying IDs.
MDN's id reference describes the identifier's uniqueness within the document and its role in links, scripts, and styling.
Do not assume that a URL ending in #setup refers to a unique section merely because the heading text appears once visually. A hidden mobile layout or cloned component may contain another target.
The heading structure guide provides broader context. Here the acceptance test is a specific reference reaching its intended section.
Inventory duplicate identifiers
Run a read-only check in the browser console:
const groups = new Map();
for (const element of document.querySelectorAll('[id]')) {
const items = groups.get(element.id) ?? [];
items.push(element);
groups.set(element.id, items);
}
[...groups].filter(([, items]) => items.length > 1);Inspect the returned elements and their contexts. Do not automatically rename them from the console; the publishing system would recreate them on reload.
Compare the original HTML with the rendered document. A build may emit unique IDs while client-side code duplicates a component later.
Also inspect empty or malformed IDs separately. They are not the same issue as two valid-looking identifiers referring to different elements.
Save a small example containing the duplicated ID, both elements, and the link that uses it. That makes the defect easy to reproduce without sharing the entire page.
Trace collisions to the generator
Find how heading slugs and component IDs are created. A repeated heading such as “Setup” can collide if the generator only lowercases the text.
A page combining several imported articles can also reuse identifiers that were unique in their original documents. Uniqueness must hold in the final combined document.
Check responsive duplication. Rendering both desktop and mobile versions with the same IDs can create ambiguity even when CSS shows only one version.
For forms and interactive controls, inspect references such as label for and aria-labelledby. Those may depend on the same IDs and need to remain aligned after a repair.
The raw and rendered HTML guide helps identify whether the collision begins in source generation or browser composition.
Do not fix only the first duplicate found. Identify the pattern so the next repeated heading or component instance gets a valid identifier automatically.
Choose stable unique IDs
Use the site's supported slug or component ID mechanism to generate unique targets. A deterministic suffix or a meaningful context prefix can distinguish repeated headings.
Keep stability in mind. If every heading's ID changes when an unrelated section is inserted, old shared links may become fragile.
For an article table of contents, generate the links from the same resolved ID list as the headings. Separate slug functions can disagree about punctuation, repeated labels, or language characters.
For repeated interactive components, use the framework's supported ID strategy and keep each label reference connected to its own control.
Do not use random values that change on every page load for anchors intended to be shared publicly. That would replace a collision problem with unstable links.
Document which IDs are part of the public section-link contract and which are internal component identifiers. They can have different stability requirements.
Verify references after the change
Open each affected fragment URL directly and from the page's own navigation. Confirm the intended heading is targeted.
Then test labels and accessible names that referenced changed IDs. A table-of-contents repair should not break a form's label relationship.
Use a small matrix:
| Reference | Expected target |
|---|---|
| Shared section URL | Intended public heading |
| Table of contents | Same heading ID |
| Repeated component label | Its own control |
| Mobile layout | Correct supported target |
| Browser navigation back | Expected document state |
Check the rendered DOM for duplicates again. A source-only fix can leave a client-rendered duplicate.
The JavaScript link guide helps preserve real navigable links rather than replacing anchors with click-only workarounds.
Handle existing public fragment links carefully
If the old ID is already widely shared, decide how to preserve its intended destination where possible. A fragment is handled within the document, so a normal server redirect does not receive it as a path component.
Avoid leaving two elements with the old ID as a compatibility measure. That preserves the ambiguity.
You may keep the old identifier on the intended heading and assign a new ID to the accidental duplicate. Where a broader heading scheme changes, document the impact on shared links and use the site's supported compatibility approach.
Do not claim every AI citation uses fragments or that duplicate IDs always change search results. The concrete issue is that the tested section link did not reliably identify the intended part of the page.
A repaired anchor is useful to readers, documentation references, and any client that follows that link, regardless of how it was discovered.
Test future content composition
Add a test article with repeated heading text and a page containing multiple instances of the relevant component. These cases express the actual collision risk.
Also test non-English headings if the site supports them. A slug generator that strips many characters can collapse distinct headings into the same result.
Keep the check focused on the final rendered document and its references. Searching source files for repeated strings alone can miss runtime composition or flag harmless IDs on separate pages.
For a CMS migration, compare a small set of existing shared section links before and after publication. A visual screenshot will not reveal that the public anchors changed.
A durable repair establishes one owner for generating IDs and references, then verifies that the final page contains unique targets with stable intended links.
If the repaired link still appears to land in the wrong place, inspect layout separately. A fixed header can cover the correct target, and late-loading content can move a section after the fragment navigation. Confirm which element owns the requested ID before changing the identifier again. This avoids breaking an otherwise correct public anchor to compensate for a spacing issue.
Test the destination after images and dynamic components have settled, then repeat with a direct visit to the full fragment URL. A successful click from an already loaded page does not cover the initial-load sequence. Keep the identifier fix and any scrolling or layout repair distinct in the change record so future editors understand which problem each change solved.
Frequently asked questions
Are duplicate IDs harmless if one element is hidden?
No. Hidden layout does not create a separate document namespace. Inspect the actual target relationships.
Can a server redirect fix an old fragment by itself?
The fragment is not sent as part of the HTTP request path. Plan compatibility within the document or supported client behavior.
Should I replace anchors with JavaScript scrolling?
Not merely to hide duplicate IDs. Repair the identifiers and preserve useful real links.
Use the free Crawlito check on the page after verifying its section links and document structure.
Filed under Structure and schema. Topics: html, internal-links, accessibility