Lesson 18 of 51 · Module 3, lesson 3 of 6 · 3 min

Links to a place on the same page

Let readers jump to a section of a long page, and back to the top.

What you'll learn

  • How to link to a section of the same page
  • How to make a "Back to top" link
  • How to link to a section of another page

Why this matters

On a long page, readers want to jump straight to the part they need: the ingredients, the steps, the questions. A short list of links at the top — like a table of contents — saves them from scrolling, and helps keyboard users too.

Two steps

  1. Give the target an id attribute: a name that is unique on the page.
  2. Link to it with # and the same name.
html
<p><a href="#steps">Go to the steps</a></p>
<h2 id="steps">Steps</h2>

A complete example

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Tomato soup</title>
</head>
<body>
  <h1 id="top">Tomato soup</h1>
  <p>Jump to: <a href="#ingredients">Ingredients</a> · <a href="#steps">Steps</a></p>

  <h2 id="ingredients">Ingredients</h2>
  <p>Six tomatoes<br>One onion<br>One litre of water<br>Salt</p>

  <h2 id="steps">Steps</h2>
  <p>Cut the tomatoes and the onion.</p>
  <p>Cook them in the water for twenty minutes.</p>
  <p>Blend the soup and add salt to taste.</p>

  <p><a href="#top">Back to top</a></p>
</body>
</html>

What you'll see: two links under the title. Clicking Steps jumps down to the Steps heading. Back to top jumps up again. (On a short page, the jump is small, because there is little to scroll.)

The tomato soup page with a line "Jump to: Ingredients · Steps" under the title, the ingredients section, the steps section, and a "Back to top" link at the bottom.

Rules for id values

  • Unique: no two elements on one page may have the same id.
  • No spaces: use hyphens, such as id="serving-ideas".
  • Lower case, to match your file-name habit. id values are case-sensitive: #Steps does not find id="steps".
  • The # goes in the link only, not in the id itself.

Linking to a section on another page

Put the page's path first, then # and the id:

html
<a href="soup.html#steps">See the soup steps</a>

Try it yourself

  1. Select the complete example and choose Try it. Click the jump links in the preview.
  2. Add a new section, "Serving ideas", with id="serving", and add it to the "Jump to" line.
  3. Break it on purpose: change the link to href="#Serving". It stops working, because the case does not match.

Common mistakes

  • Forgetting the # in the link, such as href="steps". The browser looks for a file called steps. Fix: href="#steps".
  • Putting # in the id, such as id="#steps". Fix: id="steps".
  • Two elements with the same id. The link jumps to the first one only. Fix: every id is unique.
  • Different spelling or case between the link and the id. Fix: copy the name exactly.

Check yourself

  1. Which attribute marks the place to jump to?
  2. Write a link that jumps to id="faq" on the same page.
  3. Write a link to the section id="steps" on the page soup.html.

Answers

  1. id.
  2. <a href="#faq">Questions</a> (any clear link text).
  3. <a href="soup.html#steps">Soup steps</a>.

Next: your pages have text and links. Now add pictures — and learn to describe them for people who cannot see them.

Chat with us on WhatsApp