Linking to sections within a page
This lesson covers
- Fragment identifiers and the id attribute
- Building a table of contents
- Linking to a section on another page
- Back to top links and skip links
Short answer
To link to part of a page, give the target element an id, then link to it with a hash: href="#prices". This is called a fragment identifier or jump link. It powers tables of contents, back to top links, and the skip to content link at the top of this page.
Two steps
<!-- 1. Give the section an id -->
<h2 id="prices">Prices</h2>
<!-- 2. Link to it with # -->
<a href="#prices">Jump to prices</a>
The id must be unique on the page, with no spaces. Use lowercase words joined by hyphens, like room-rates.
A table of contents
<nav aria-label="On this page">
<ul>
<li><a href="#rooms">Rooms</a></li>
<li><a href="#rates">Rates</a></li>
<li><a href="#directions">How to get here</a></li>
</ul>
</nav>
Long guides become much easier to scan. Google sometimes shows these section links in search results, called jump to links.
Linking to a section on another page
<a href="/resort/#directions">Directions to the resort</a>
<a href="https://example.com/faq/#refunds">Refund policy</a>
Add the hash and id after the page URL.
Back to top and skip links
<body id="top">
<a class="skip" href="#main">Skip to content</a>
...
<main id="main">...</main>
...
<a href="#top">Back to top</a>
</body>
A skip link lets keyboard users jump past the menu straight to the content. Press Tab once on this page to see it. href="#top" also scrolls to the top in all modern browsers.
Try it: build a table of contents
Add ids to the three headings, then make the list into jump links. Click them in the preview.
Live preview
Quick quiz
Common mistakes
- Spaces in ids. id=“room rates” fails. Use room-rates.
- Mismatched case. #Rates will not match id=“rates” reliably.
- Duplicate ids. The link jumps to the first match only.
Your action
Tick each item when you can do it. Your progress saves on this device.
Key terms
- Fragment identifier
- The # part of a URL that points to an id.
- Jump link
- A link that scrolls to a section.
- id attribute
- A unique name for one element.
- Skip link
- A link that lets keyboard users skip to main content.
- Table of contents
- A list of jump links to page sections.
Questions beginners ask
Can I make the scroll smooth?
Yes, with CSS: html { scroll-behavior: smooth; }.
Do jump links help SEO?
They help usability and can earn jump to links in Google.
Why does my heading hide under a sticky header?
Use CSS scroll-margin-top on the target.
Related lessons
Need a website built or fixed while you practice? The team at Remar Digital can help.