On this page
Long web pages are easier to scan and share when readers can jump directly to a section. You can use URL fragments for document navigation by giving headings unique identifiers and linking to them with ordinary HTML links.
Key Takeaways
- Add a unique, stable
idto each heading you want readers to reach, then link to it withhref="#id". - Use descriptive link text and a labeled navigation area so readers know where each link leads.
- Make both keyboard focus and the destination visible; scrolling to a heading does not guarantee that every browser and assistive technology will announce it.
- Check fragment links in your supported browsers. Application routing and dynamically rendered content may need additional handling.
How fragment links work
A fragment is the part of a URL after #. In https://example.com/guide#preparing-content, the fragment is preparing-content. A link to #preparing-content refers to an element with id="preparing-content" on the current page. A link can also include the full page URL to send someone to that section from elsewhere.
When the destination exists, the browser normally scrolls it into view. If it does not exist, the URL can still contain the fragment, but there is no matching element to navigate to.
Use IDs that are unique within the document, short, and meaningful. Keep them stable: changing an ID can break bookmarks and links shared from other pages. Lowercase words separated by hyphens are easy to read and copy. Fragment identifiers are matched to IDs case-sensitively.
Advertisement
Add an accessible table of contents
This example uses a labeled navigation region, descriptive links, and headings in document order. Place it within the body of a page with an appropriate page title.
<main>
<h1>Publishing a long-form guide</h1>
<nav aria-label="On this page">
<h2>On this page</h2>
<ul>
<li><a href="#prepare-content">Prepare the content</a></li>
<li><a href="#review-accessibility">Review accessibility</a></li>
<li><a href="#publish-guide">Publish the guide</a></li>
</ul>
</nav>
<section aria-labelledby="prepare-content">
<h2 id="prepare-content">Prepare the content</h2>
<p>Organize the guide into meaningful sections.</p>
</section>
<section aria-labelledby="review-accessibility">
<h2 id="review-accessibility">Review accessibility</h2>
<p>Check headings, links, and keyboard navigation.</p>
</section>
<section aria-labelledby="publish-guide">
<h2 id="publish-guide">Publish the guide</h2>
<p>Confirm that shared section links still work.</p>
</section>
</main>
The link text describes the destination without relying on surrounding text. The nav label distinguishes this navigation region from others, such as the site's main menu. The heading IDs also make it possible to share a link such as https://example.com/guide#review-accessibility.
Make the destination and focus visible
A sticky header can cover a heading after the browser scrolls to it. The following CSS gives targeted headings some space above them and adds a visual destination indicator. The focus rule gives keyboard users a visible indication when a link receives focus.
/* Adjust this value to the actual height of your sticky header. */
main h2[id] {
scroll-margin-top: 5rem;
}
main h2[id]:target {
border-inline-start: 0.25rem solid currentColor;
padding-inline-start: 0.5rem;
}
a:focus-visible {
outline: 3px solid currentColor;
outline-offset: 3px;
}
Check the indicator against your page's colors and layout, including high-contrast settings. The :target style marks the destination; it is not a substitute for a focus indicator or an announcement by assistive technology. Keep focus styles for buttons, form fields, and other controls as well.
Check orientation after a jump
Native fragment navigation can scroll to a heading without giving that heading the same programmatic focus behavior in every browser and assistive-technology combination. Do not assume that a screen reader will announce the destination or that focus will visibly move to the heading. Check whether a keyboard user can tell where they landed and continue through the content in a sensible order.
If your application manages navigation itself, it may need to move focus deliberately after rendering. For example, a heading with tabindex="-1" can receive programmatic focus without becoming a normal Tab stop. Test the resulting announcement and focus styling; do not move focus automatically without considering the user's context.
Advertisement
Native anchors versus application routing
For a document already in the page, a normal <a href="#review-accessibility"> link lets the browser handle the fragment without JavaScript. It also gives readers an address they can copy and revisit.
A single-page application may use the fragment for routing, or it may render the target only after data or a route has loaded. In those cases, a native jump alone may not reach the intended element. Coordinate section navigation with the application's router: preserve a shareable URL, wait until the target is in the DOM, and handle scrolling and focus as appropriate. Avoid competing with a router that already uses the fragment for its own state. Test direct visits and browser Back and Forward, not just clicks within the running app.
The fragment is not sent as part of the HTTP request for the page. Server-side rendering therefore cannot rely on the server receiving it as a request parameter; client-side code can read it after the page loads.
Browser testing checklist
The examples in this article have not been browser-tested here. Before publishing, test them in the browsers and assistive technologies your audience uses, including relevant desktop and mobile combinations:
- Open the page and use Tab to reach each contents link. Confirm that focus is visible and that each link's purpose is clear.
- Activate a link with the keyboard. Check that the correct heading is visible, is not hidden behind a sticky header, and gives you enough context to continue.
- Continue with Tab and, where applicable, screen-reader navigation. Check where focus and the reading cursor are, and whether the destination is announced or otherwise easy to find.
- Copy a section URL into a new tab, reload it, and use Back and Forward after navigating between sections. Confirm that the destination remains usable.
- Repeat with a missing ID and, for an application, after a direct load before asynchronous content has appeared. If the page is intended to work without JavaScript, check that path too.
Keyboard navigation settings can vary by browser and operating system; check those settings if links do not receive Tab focus as expected.
Common Mistakes
- Duplicating IDs: A fragment should identify one destination. Give every target a unique ID.
- Using vague links: Replace repeated text such as “click here” with the name of the destination.
- Hiding focus outlines: A visual target marker does not tell users which control currently has keyboard focus.
- Changing IDs casually: Existing external links and bookmarks may stop reaching their sections.
- Assuming a scroll is an announcement: Verify the experience with keyboard and assistive technology rather than relying on the visual jump alone.
- Ignoring the router: A fragment-based router or a late-rendered heading can prevent the expected native jump.
Best Practices
Use semantic headings that reflect the document's hierarchy, and link to the heading rather than an unrelated decorative element. Keep section links working across edits, account for fixed headers, and include direct-link and keyboard checks in your publishing workflow. For application-managed navigation, decide explicitly how URL updates, rendering, scrolling, and focus work together.
Conclusion
Fragment links are a lightweight way to make long documents easier to navigate and share. Stable heading IDs, descriptive links, visible focus, and checks in real browsers make them more dependable for readers.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.