Lesson 17 of 51 · Module 3, lesson 2 of 6 · 4 min

Links

Link your pages to each other and to other websites, with link text that makes sense on its own.

What you'll learn

  • How to make a link with <a> and href
  • How to link to your own pages and to other websites
  • How to write link text that everyone understands

Why this matters

Links are what make the web a web. They connect your pages to each other and to the rest of the internet. Good links also matter for accessibility: many screen reader users open a list of all the links on a page and choose one from the list. Each link has to make sense on its own.

A link is an <a> element (a for anchor). The href attribute holds the address. The content is the clickable text.

html
<a href="about.html">About me</a>

In your my-website folder, index.html links to about.html:

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Home</title>
</head>
<body>
  <h1>Welcome to my cooking notes</h1>
  <p>I write down the recipes I love.</p>
  <p><a href="about.html">Read more about me</a></p>
  <p>I learn a lot from <a href="https://www.wikipedia.org/">Wikipedia</a>.</p>
</body>
</html>

What you'll see: two links, blue and underlined (the browser's default look). Links you have already visited turn purple.

The page "Welcome to my cooking notes" with two blue, underlined links: "Read more about me" and "Wikipedia".

Now create about.html in the same folder, with a link back:

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>About me</title>
</head>
<body>
  <h1>About me</h1>
  <p>I am learning to cook, and to build websites.</p>
  <p><a href="index.html">Back to the home page</a></p>
</body>
</html>

Open index.html in your browser and click between the two pages.

In the Try code playground, links to other pages do not open — the playground shows a message instead. Test page-to-page links in your own folder.

Your pages and other websites

  • For your own pages, use a relative path: about.html, recipes/soup.html, ../index.html (see Folders and relative paths).
  • For other websites, use the full address, starting with https://.

Opening in a new tab

target="_blank" opens the link in a new tab:

html
<a href="https://www.wikipedia.org/" target="_blank">Wikipedia (opens in a new tab)</a>

Use it rarely. A new tab surprises many people, and it breaks the browser's Back button. If you use it, say so in the link text, as above.

html
<a href="mailto:ana@example.com">Email me</a>
<a href="tel:+441632960001">Call +44 1632 960001</a>

mailto: opens the visitor's email program. tel: starts a call on a phone. Write the number with its country code and no spaces in the href. (The number here is an example — use your own.)

Screen reader users often jump from link to link, hearing only the link text. So:

  • ✗ Click here, Read more, Link — meaningless on their own.
  • ✓ Read the tomato soup recipe, Read more about me, Download the shopping list (PDF).

Also avoid adding title to links to explain them. The tooltip appears only for mouse users.

Try it yourself

  1. Build the two pages above in your own my-website folder, and click back and forth.
  2. Add a third page, contact.html, and link to it from both pages.
  3. Change one link to href="About.html" (capital A). Does it still work on your computer? It might — but on most web servers it would not. Change it back.

Common mistakes

  • Forgetting https:// for another website, such as href="www.wikipedia.org". The browser treats it as a file in your folder. Fix: always write the full address.
  • Wrong path to your own page. The browser shows "file not found". Fix: check the path from the page you are on (see Folders and relative paths).
  • Meaningless link text like "click here". Fix: describe where the link goes.
  • Putting a link inside another link. Browsers cannot handle it. Fix: one link at a time.

Check yourself

  1. Which attribute holds the address of a link?
  2. How do you link to https://www.wikipedia.org/, and why do you need the https://?
  3. Rewrite this link text to be clearer: To see my recipes, <a href="recipes.html">click here</a>.

Answers

  1. href.
  2. <a href="https://www.wikipedia.org/">Wikipedia</a>. Without https://, the browser looks for a file with that name in your folder.
  3. For example: <a href="recipes.html">See my recipes</a>.

Next: links can also jump to a place further down the same page — perfect for long pages like recipes.

Chat with us on WhatsApp