Sub topic 13
Link between sections with anchors and smooth scrolling
This chapter assumes you can write a basic HTML page with semantic landmarks and link a stylesheet. It also assumes you know how CSS rules and selectors work. You will connect the navigation links to the page sections and make the jump between them smooth and accessible.
How anchor links work
An anchor link points to an element on the same page. It uses a fragment, which is a # followed by the value of an element's id attribute. When you click href="#projects", the browser scrolls to the element with id="projects" and updates the URL to end in #projects.
Each id must be unique in the document. The match is case-sensitive, so #About will not find id="about".
<nav aria-label="Main">
<ul>
<li><a href="#about">About</a></li>
<li><a href="#projects">Projects</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
<main>
<section id="about">
<h1>Jane Doe</h1>
<p>I build small, fast websites.</p>
</section>
<section id="projects">
<h2>Projects</h2>
</section>
<section id="contact">
<h2>Contact</h2>
</section>
</main>This works with no CSS and no JavaScript. The browser also adds the jump to its history, so the Back button returns the reader to the previous position.
Add smooth scrolling
By default the page jumps instantly. Set scroll-behavior on the html element to animate the movement instead.
html {
scroll-behavior: smooth;
}Some people get motion sickness from animated scrolling, and their system settings can ask for less motion. Respect that with a media query for prefers-reduced-motion.
@media (prefers-reduced-motion: reduce) {
html {
scroll-behavior: auto;
}
}Keep targets visible with scroll-margin-top
When the browser scrolls to a section, it aligns the top of that section with the top of the viewport. If you later make the header sticky, it can cover the section heading. The scroll-margin-top property adds space above the target when it scrolls into view.
section[id] {
scroll-margin-top: 1rem;
}Set the value to at least the height of any fixed header. The selector section[id] matches only sections that have an id, so it does not affect other sections.
Make the links usable by keyboard
Keyboard users move between links with the Tab key. They need to see which link has focus. Use :focus-visible, which shows the outline for keyboard focus and usually hides it for mouse clicks.
a:focus-visible {
outline: 3px solid currentColor;
outline-offset: 2px;
}Do not write outline: none on links unless you replace it with another clear focus style.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Link does nothing | The `id` is missing or misspelled | Compare the `href` value with the `id`, including case |
| Link reloads the page | The `href` is `index.html#about` or lacks the `#` | Use `href="#about"` |
| Heading hides under a sticky header | No scroll offset | Add `scroll-margin-top` |
| Scrolling stays instant | `scroll-behavior` is on `body` or the browser is in reduced-motion mode | Set it on `html` and check system settings |
| Two sections react to one link | Duplicate `id` values | Make each `id` unique |
Exercise
A reader with a system setting for reduced motion opens your page and clicks "Projects". Which CSS makes the page jump instantly for them while everyone else gets smooth scrolling, and why does the order of the two rules matter?
Show answer
Use html { scroll-behavior: smooth; } first, then @media (prefers-reduced-motion: reduce) { html { scroll-behavior: auto; } }. Both selectors are html, so they have equal specificity, and the later rule wins when the media query matches. If the media query came first, the plain smooth rule would override it.
Exercise
Your sticky header is 4rem tall and it covers the <h2> of each section after a nav click. What single declaration fixes this, and on which selector?
Show answer
Add scroll-margin-top: 4rem; (or slightly more) to section[id]. This leaves space above each section when the browser scrolls to it.