Lesson 5 of 12

HTML Links

HTML links are what turn separate documents into a web. A single element, <a> (short for “anchor”), lets visitors move between pages, jump to a section, start an email or even dial a phone number. In this lesson you will learn how to write each kind of link and how to make links clear for everyone. You already know how attributes work from HTML Elements and Attributes – links depend on one in particular: href.

The anchor element and href

A link is written like this: <a href="https://developer.mozilla.org/">MDN Web Docs</a>. The href (hypertext reference) attribute holds the destination, and the content between the tags is the clickable link text. Without an href, an <a> element is just a placeholder and is not focusable or clickable.

Absolute and relative URLs

An absolute URL is the full address, including the scheme (https://) and the domain. Use absolute URLs when linking to another website.

A relative URL is a path worked out from the current page’s location. Use relative URLs for pages on your own site – they are shorter and keep working if your domain changes. Imagine this folder structure:

my-website/
├── index.html
├── contact.html
└── tours/
    ├── index.html
    └── kerala.html
FromToRelative href
index.htmlcontact.htmlcontact.html
index.htmltours/kerala.htmltours/kerala.html
tours/kerala.htmlcontact.html../contact.html (the ../ means “go up one folder”)
Any pageThe site’s home page/ (a leading slash starts from the site root)

Tip: Root-relative paths that begin with / only work when your files are served by a web server. When you open a file directly from your computer, / points to the root of your drive, so use plain relative paths while practising locally.

You can link to a specific part of a page using a fragment: a # followed by the id of the target element. Long articles often use this for a table of contents. To link to a section on a different page, add the fragment after the file name, e.g. tours/kerala.html#houseboats.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Jump links</title>
  <style>
    section, h2 { scroll-margin-top: 1rem; }
    h2 { margin-top: 70vh; }
  </style>
</head>
<body>
  <h1>Kerala travel notes</h1>
  <nav aria-label="On this page">
    <a href="#munnar">Munnar</a> |
    <a href="#alappuzha">Alappuzha</a> |
    <a href="#varkala">Varkala</a>
  </nav>

  <h2 id="munnar">Munnar</h2>
  <p>Tea estates, cool weather and misty mornings.</p>

  <h2 id="alappuzha">Alappuzha</h2>
  <p>Backwaters and houseboat stays.</p>

  <h2 id="varkala">Varkala</h2>
  <p>Red cliffs above a quiet beach.</p>
  <p><a href="#top">Back to top</a></p>
</body>
</html>

The large top margin in the CSS is only there to make the page long enough to see the jump. The special fragment #top always scrolls to the top of the document.

The href does not have to point to a web page:

  • mailto: opens the visitor’s email app with a new message, e.g. href="mailto:[email protected]".
  • tel: starts a phone call on mobiles, e.g. href="tel:+914132000000". Write the number in international format without spaces.
  • The download attribute asks the browser to download the linked file instead of opening it. It only works for files on the same site (same origin).
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Kinds of links</title>
</head>
<body>
  <ul>
    <li><a href="https://developer.mozilla.org/en-US/docs/Web/HTML" target="_blank">MDN HTML docs</a> (opens in a new tab)</li>
    <li><a href="mailto:[email protected]">Email our bookings team</a></li>
    <li><a href="tel:+919876543210">Call +91 98765 43210</a></li>
  </ul>
</body>
</html>

Adding target="_blank" opens the link in a new tab or window. Use it sparingly. Opening new tabs without warning can confuse people, especially screen reader users, and breaks the browser’s Back button. It is reasonable for things like a help page opened in the middle of filling in a form. When you do use it, tell visitors, for example with “(opens in a new tab)” in the link text.

In older tutorials you will see rel="noopener" added for security. Modern browsers now apply that protection automatically for target="_blank", but adding it does no harm.

Screen reader users can pull up a list of all the links on a page, read out of context. A list full of “click here” and “read more” is useless. Good link text describes the destination on its own.

  • Poor: To see our Kerala tours, click here.
  • Better: See our Kerala tour packages. – with only the words “Kerala tour packages” inside the link: See our <a href="kerala.html">Kerala tour packages</a>.

Watch out: Do not use links for actions that do not navigate, such as opening a menu or submitting data. Links take you somewhere; buttons do something. Use a <button> for actions – you will meet it in HTML Forms and wire it up in JavaScript Events.

HTML links are often styled to look like buttons or menu items. You will learn how in CSS Selectors, using states like :hover and :focus-visible. Next, you will add pictures to your pages in HTML Images.

Further reading

Key takeaways

  • Links use <a href="…">link text</a>; without href it is not a working link.
  • Use absolute URLs for other websites and relative URLs (page.html, ../page.html) for your own pages.
  • A fragment such as #section-id jumps to the element with that id.
  • mailto: and tel: links start emails and calls; download downloads same-site files.
  • Use target="_blank" sparingly and write link text that makes sense out of context.

Quick quiz

Check what you learned – pick an answer.

1. From tours/kerala.html, which href links to contact.html in the parent folder?

2. Which link jumps to <h2 id="faq"> on the same page?

3. Which link text is the most accessible?

Practice exercise

Create a mini contact page with three links: an email link to [email protected], a phone link to +91 44 2345 6789, and a link to the MDN HTML page that opens in a new tab and says so in its text.

Show a hint

Remove spaces from the phone number in the tel: href, but you may keep them in the visible text.

Show the solution
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Contact us</title>
</head>
<body>
  <h1>Contact us</h1>
  <p><a href="mailto:[email protected]">Email [email protected]</a></p>
  <p><a href="tel:+914423456789">Call +91 44 2345 6789</a></p>
  <p><a href="https://developer.mozilla.org/en-US/docs/Web/HTML" target="_blank">Learn HTML on MDN (opens in a new tab)</a></p>
</body>
</html>

Progress is saved in this browser only.