Sub topic 4
Linking to a section of a page
A fragment link points to a specific spot inside a page instead of to the top of it. Tables of contents, "back to top" links, and skip links all use fragment links. This chapter assumes you can write an a element with an href and that you know the three URL forms from the previous chapter: absolute, root-relative, and relative.
How a fragment link works
A URL can end with a fragment, which is a # followed by a name. The browser uses the fragment to find an element on the page whose id attribute has exactly that value, then scrolls that element into view.
Two attributes work together:
- The target element has an
id, such asid="install". - The link has an
hrefthat starts with#followed by the same value, such ashref="#install".
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Fragment link demo</title>
</head>
<body>
<nav aria-label="On this page">
<ul>
<li><a href="#install">Installation</a></li>
<li><a href="#usage">Usage</a></li>
<li><a href="#faq">FAQ</a></li>
</ul>
</nav>
<h2 id="install">Installation</h2>
<p>Steps go here.</p>
<h2 id="usage">Usage</h2>
<p>Examples go here.</p>
<h2 id="faq">FAQ</h2>
<p>Questions and answers go here.</p>
</body>
</html>To see scrolling, add enough content between the sections to make the page taller than the window. You can put an id on any element, not only headings. A paragraph, a table row, or an image can all be targets.
Rules for id values
The browser matches the fragment against id values, so the id must follow these rules.
- It must be unique in the page. If two elements share an
id, browsers scroll to the first one and ignore the rest. - It must not be empty and must not contain whitespace. Use hyphens instead of spaces, as in
id="getting-started". - It is case-sensitive.
#Installdoes not matchid="install". - It is better to use lowercase letters, digits, and hyphens. HTML allows other characters, but these are easy to type, read, and use in CSS selectors.
Choose ids that describe the content, not its position. id="pricing" stays valid when you reorder sections. id="section-3" does not.
Linking to a section of another page
Put the fragment after the path of the other page.
<a href="guides/setup.html#install">Installation steps</a>
<a href="/docs/intro.html#faq">Introduction FAQ</a>
<a href="https://example.com/docs/#usage">Usage on the example site</a>The browser loads the page first, then scrolls to the element. The fragment is never sent to the server. The server sees only the part of the URL before the #, so you cannot use fragments to select different content on the server side.
Special cases
Two fragments have defined behavior even without a matching id:
href="#"andhref="#top"both scroll to the top of the page. This is part of the HTML specification, so a "Back to top" link needs noid.- An empty fragment on its own,
#, adds a#to the address bar. Do not use it as a placeholder for a button. If the element runs a script, use abuttonelement instead.
<footer>
<a href="#top">Back to top</a>
</footer>Highlighting the target with CSS
The :target pseudo-class matches the element that the current fragment points to. It lets you show readers where they landed.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Highlight the target</title>
<style>
:target {
background: #fff3bf;
}
</style>
</head>
<body>
<p><a href="#note">Go to the note</a></p>
<div style="height: 120vh"></div>
<p id="note">This paragraph is highlighted when you arrive.</p>
</body>
</html>Fixing headings hidden under a sticky header
If your page has a header fixed to the top of the window, a scrolled-to heading can end up behind it. The CSS property scroll-margin-top adds space above the target when the browser scrolls to it.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Sticky header offset</title>
<style>
header {
position: sticky;
top: 0;
height: 3rem;
background: #222;
color: #fff;
}
h2[id] {
scroll-margin-top: 4rem;
}
html {
scroll-behavior: smooth;
}
@media (prefers-reduced-motion: reduce) {
html {
scroll-behavior: auto;
}
}
</style>
</head>
<body>
<header>Site header</header>
<p><a href="#details">Jump to details</a></p>
<div style="height: 150vh"></div>
<h2 id="details">Details</h2>
<p>The heading stays visible below the header.</p>
</body>
</html>scroll-behavior: smooth animates the jump. Wrapping it in a prefers-reduced-motion check turns the animation off for people who have asked their system to reduce motion.
Keyboard and screen reader behavior
When you follow a fragment link, browsers move the sequential focus starting point to the target. The next press of Tab continues from there. The target itself is not focused unless it is already focusable, such as a link or button.
This matters for skip links, which let keyboard users jump past repeated navigation. A skip link is a fragment link to the main content. To make sure focus moves reliably in all browsers, give the target tabindex="-1". That makes it focusable by script and by link activation, but it does not add it to the Tab order.
<body>
<a href="#main">Skip to main content</a>
<nav aria-label="Main">
<ul>
<li><a href="/">Home</a></li>
<li><a href="/articles/">Articles</a></li>
</ul>
</nav>
<main id="main" tabindex="-1">
<h1>Welcome</h1>
</main>
</body>Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Link does nothing | `id` is misspelled or has different capitalization | Compare the `href` and `id` character by character |
| Link tries to load a file named `install` | Missing `#` in `href="install"` | Write `href="#install"` |
| Page scrolls to the wrong place | Duplicate `id` values | Make every `id` unique |
| Link works on the same page but not from another | Missing page path | Write `page.html#install` |
| Heading hidden behind the header | Fixed or sticky header covers the target | Add `scroll-margin-top` |
| Old `<a name="install">` anchors in legacy code | The `name` attribute on `a` is obsolete | Move the identifier to an `id` on the heading |
Fragments also depend on your headings staying put. If you rename an id, every link to it, including links from other sites and bookmarks, stops scrolling. Treat published ids as part of your page's public address.
Exercise
A page has <h2 id="Pricing">Pricing</h2>, and a link in the nav reads <a href="#pricing">Pricing</a>. Clicking the link does not scroll. Explain why, and give two different fixes.
Show answer
The fragment match is case-sensitive, so #pricing does not match id="Pricing". One fix is to change the link to href="#Pricing". The other is to change the heading to id="pricing", which is preferred because lowercase ids are easier to type and use consistently. If other pages or bookmarks already link to #Pricing, change the link instead.
Exercise
Write the HTML for a link on /docs/index.html that goes to the section with id faq on /docs/setup.html, using a relative URL.
Show answer
<a href="setup.html#faq">Setup FAQ</a>. Both pages are in the /docs/ folder, so the path is just the file name, followed by #faq.