Anchor Links
id + href="#id", building a table of contents, and enabling smooth scrolling with CSS.
The id + Hash Pattern
Give any element a unique id, then link to it with href="#that-id". Clicking the link jumps the page to that element — no JavaScript required.
<a href="#pricing">Jump to Pricing</a> <!-- ... further down the page ... --> <h2 id="pricing">Pricing</h2> <p>Our plans start at $10/month...</p>
Building a Table of Contents
Combine anchor links with a <ul> to build a jump-to-section table of contents — exactly what long articles, documentation, and this very lesson page use.
<nav aria-label="Table of contents"> <ul> <li><a href="#intro">Introduction</a></li> <li><a href="#pricing">Pricing</a></li> <li><a href="#faq">FAQ</a></li> </ul> </nav> <h2 id="intro">Introduction</h2> <h2 id="pricing">Pricing</h2> <h2 id="faq">FAQ</h2>
Smooth Scrolling with CSS
By default, jumping to an anchor is instant. One line of CSS — scroll-behavior: smooth — animates the scroll instead, on <html> so it applies to every anchor jump on the page.
<style> html { scroll-behavior: smooth; } </style>
This is exactly how this lesson page's own "back to top" button works — a single anchor link with smooth-behavior scrolling, no JavaScript animation library needed.
The Special #top Behavior & Back-to-Top Links
An empty hash (href="#") or href="#top" scrolls to the very top of the page. This is the standard pattern for "back to top" links at the bottom of long pages.
<a href="#top">↑ Back to top</a> <!-- Works automatically if <body> itself has id="top", or falls back to scrolling to the very top of the document -->
1. Build a table of contents nav with 3 links: "About", "Skills", "Contact".
2. Below it, add 3 sections with matching ids and some placeholder content in each.
3. Add
scroll-behavior: smooth so jumps animate.4. Add a "↑ Back to top" link at the very bottom.
💡 Show hints if you're stuck
- TOC link:
<a href="#about">About</a> - Target:
<h2 id="about">About</h2> - Smooth scroll goes on the html selector, not body