Thuta Learning
HTML
IntermediateWeb DevelopmentBeginner

Links

HTMLLesson 1325 min

What you'll walk away with

  • Tell where absolute URLs, root-relative paths, relative paths and #fragments point
  • Write mailto: and tel: links
  • Decide when target="_blank" is appropriate and explain why rel="noopener noreferrer" goes with it
  • Write link text that makes sense out of context instead of "click here"

Explain it simply

Explain it simply

Links are what make the web a web: threads tying one page to the next. In HTML a link is the <a> (anchor) element. Its href attribute says where to go, and the text between <a> and </a> is what the user clicks.

The href can be a website, another page on your own site, an email address, a phone number or a section of the same page. Think of a signpost: the words on it must be clear, and it must point the right way.

Problem → Why → Solution

The problem: links that point the wrong way

The page below is published at https://mysite.com/blog/post.html. Clicking the first link does not reach Wikipedia; it lands on a "page not found" at mysite.com/blog/www.wikipedia.org. And because all three links say "click here", a screen reader's list of links reads "click here, click here, click here".

post.html
<p>To learn more, <a href="www.wikipedia.org">click here</a>.</p>
<p>For our menu, <a href="menu.html">click here</a>.</p>
<p>To email us, <a href="info@example.com">click here</a>.</p>

Why did they break?

An href without https:// is treated as a relative path from the current page's folder. An email address without mailto: is likewise just taken as a file name. As for "click here", it says nothing about the destination unless you read the surrounding sentence, and screen reader users often listen to links as a list, apart from their sentences.

Core concept: five kinds of href, and link text

How the browser reads an href depends on how it starts. https:// makes it an absolute URL that goes to the same place from any page. A leading / makes it root-relative, starting from your domain's root: /about.html means mysite.com/about.html. Starting with a plain name such as about.html makes it relative to the current page's folder, and ../ climbs one folder up. A leading # jumps to the element with that id on the same page. Schemes such as mailto: and tel: open the email app or the phone dialer.

target="_blank" opens the link in a new tab. The page in that new tab used to be able to reach back to yours through window.opener, for example to swap your tab for a phishing page, an attack called reverse tabnabbing. rel="noopener" cuts that connection. Modern browsers apply noopener to target="_blank" by default, but writing it explicitly still protects older browsers and makes the intent clear to readers of the code. rel="noreferrer" additionally stops your page's address from being sent in the Referer header.

Link text is the link's name. Screen readers can list every link on a page, and search engines use link text as a clue about the target page. So write text that explains itself, such as "Download the 2025 price list (PDF)". If the link opens a new tab, users may feel the previous page vanished, so say "(opens in a new tab)" in the text. An <a> without href is not a link and cannot be reached with the keyboard; for an action rather than a destination, use <button>.

Mental model: a signpost
  1. The start of href picks the route
  2. https:// / name # mailto: tel:
  3. Link text is the sign's wording
  4. New tab? Add rel and say so

A wrong href points down the wrong road; vague link text hides where the road goes.

General pattern

html
<a href="https://other-site.example/page">Descriptive text</a>
<a href="/folder/page.html">Page on this site</a>
<a href="#section-id">Section on this page</a>
<a href="mailto:name@example.com">Email name</a>
<a href="tel:+15550100">Call +1 555 0100</a>
<a href="https://other-site.example" target="_blank" rel="noopener noreferrer">
  Text (opens in a new tab)
</a>

Look at how each href begins: https://, /, #, mailto:, tel:. Whenever target="_blank" appears, rel and a visible hint come with it.

Example: five kinds of link

This contact page has an internal link, an external link, an email link, a phone link and a jump to a section of the same page. Look at how each href starts and predict where it goes.

contact.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Contact Golden Leaf</title>
</head>
<body>
  <h1>Contact Golden Leaf</h1>
  <p><a href="/about.html">About Us</a></p>
  <p><a href="https://www.wikipedia.org" target="_blank" rel="noopener noreferrer">
    Visit Wikipedia (opens in a new tab)</a></p>
  <p><a href="mailto:info@example.com">Email Us</a></p>
  <p><a href="tel:+959123456789">Call +95 9 123 456 789</a></p>
  <p><a href="#hours">Jump to opening hours</a></p>
  <h2 id="hours">Opening hours</h2>
  <p>Every day, 7 am to 6 pm.</p>
</body>
</html>

Code breakdown

  1. Line 9<a href="/about.html">About Us</a>

    It starts with /, so it is root-relative: from any folder on the site it reaches about.html at the domain root.

  2. Lines 10-11<a href="https://www.wikipedia.org" target="_blank" rel="noopener noreferrer"> Visit Wikipedia (opens in a new tab)</a>

    https:// makes this an absolute URL. target="_blank" opens a new tab, rel cuts the new page off from your tab and withholds the referrer, and "(opens in a new tab)" warns the user in advance.

  3. Line 12<a href="mailto:info@example.com">Email Us</a>

    The mailto: scheme opens the user's default email app with that address filled in. Without mailto:, the browser would treat the address as a file name.

  4. Line 13<a href="tel:+959123456789">Call +95 9 123 456 789</a>

    A tel: link opens the dialer on a phone. The href uses the country code (+95) and no spaces, while the link text adds spaces for readability.

  5. Lines 14-15<a href="#hours">Jump to opening hours</a></p> <h2 id="hours">Opening hours</h2>

    #hours scrolls to the element with id="hours". An id must be unique on the page; with duplicates, the browser jumps to the first one.

You should see
Five blue, underlined links appear: About Us, Visit Wikipedia (opens in a new tab), Email Us, Call +95 9 123 456 789 and Jump to opening hours, followed by an "Opening hours" heading and its sentence. The Wikipedia link opens a new tab, the email link opens the mail app, and the last link jumps to the Opening hours heading.

Where relative paths really point

Suppose the page lives at https://mysite.com/blog/post.html. The table shows the full URL the browser builds from each href, checked by reading each link's href property in headless Chromium. Notice that the href starting with "www." is looked up inside the blog folder.

hrefURL the browser visits
about.htmlhttps://mysite.com/blog/about.html
/about.htmlhttps://mysite.com/about.html
../img/a.pnghttps://mysite.com/img/a.png
www.example.comhttps://mysite.com/blog/www.example.com

Use relative or root-relative paths for pages on your own site, and always use a full absolute URL with https:// for other sites. File paths get a lesson of their own next.

In real projects

Where this shows up in real projects

Every site's navigation menu is a set of root-relative links such as /, /menu.html and /contact.html, which work from any page on the site. Long documentation pages use "On this page" #fragment links to jump to headings, and copying a URL with its fragment sends a friend straight to that section.

On mobile sites for shops and clinics, the tel: link is often the most important button, and footers commonly carry a mailto: link. Many teams open external sites in the same tab and keep new tabs for cases where leaving would lose work, such as a help page opened while filling in a form. Link checkers running in CI pipelines catch hrefs that end in a 404, and accessibility audits flag vague link text such as "click here" and "read more".

Common mistakes

Common mistakes

1. Leaving https:// off an external link

href="www.wikipedia.org" is a relative path, so it looks for a page on your own site that does not exist. Write the full URL with https:// for every external site.

2. Link text like "click here"

Five "click here" links in a links list are impossible to tell apart. Describe the destination in the link text: "Read our refund policy".

3. Opening new tabs without warning

target="_blank" on every link makes the Back button seem broken and can disorient screen reader users. Use it only when needed, together with rel="noopener noreferrer" and "(opens in a new tab)".

4. Using href="#" as a button

<a href="#"> used for a JavaScript action jumps to the top of the page and is announced as a link. If nothing navigates, use a <button>.

Best practices

Best practices

  • Before publishing a page, click every link once or run a link checker.
  • On download links, state the file format and size in the text: "Price list (PDF, 2 MB)".
  • Give fragment targets short, meaningful ids such as #hours or #pricing, because they appear in the URL.
  • When styling links in CSS, keep an underline or another marker; colour alone cannot be told apart by colour-blind readers.

Mini challenge

Mini challenge: a shop's contact footer

Write the contact section that goes at the bottom of a shop's page. Use five kinds of link, each with a correct href and clear link text.

  • A root-relative link to /menu.html
  • A link to a map website that opens in a new tab, with rel and a visible hint
  • A mailto: link with a subject, and a tel: link
  • A "Back to top" link to a heading with id="top"
footer.html
<!-- Place inside the <body> of a full page -->
<!-- TODO: an h1 with id="top" -->

<!-- TODO: a long paragraph or two so the page can scroll -->

<h2>Find us</h2>
<!-- TODO: link to the menu page (root-relative) -->
<!-- TODO: link to a map website, new tab, rel + "(opens in a new tab)" -->
<!-- TODO: mailto: link with ?subject=... -->
<!-- TODO: tel: link with the country code and no spaces in href -->
<!-- TODO: "Back to top" fragment link -->

Expected: Five links appear. The map link opens in a new tab, the email link opens the mail app with the subject filled in, and "Back to top" returns to the heading at the top of the page.

Show hint

Write the subject as mailto:info@example.com?subject=Table%20booking; a space in a URL is written %20. Then read each link's text on its own and check it still makes sense.

Show solution
footer.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Golden Leaf Tea House</title>
</head>
<body>
  <h1 id="top">Golden Leaf Tea House</h1>
  <p>Fresh tea, warm snacks and a quiet corner to read, every day of the week.</p>
  <p>Ask about our Saturday tea-tasting class for beginners.</p>

  <h2>Find us</h2>
  <p><a href="/menu.html">See our full menu</a></p>
  <p>
    <a href="https://www.openstreetmap.org/" target="_blank" rel="noopener noreferrer">
      Open the map (opens in a new tab)</a>
  </p>
  <p><a href="mailto:info@example.com?subject=Table%20booking">
    Email us to book a table</a></p>
  <p><a href="tel:+959123456789">Call +95 9 123 456 789</a></p>
  <p><a href="#top">Back to top</a></p>
</body>
</html>

Quick quiz 1 / 3

On https://mysite.com/blog/post.html, where does <a href="www.example.com"> lead?

Quick quiz 2 / 3

Which link text is most useful in a screen reader's list of links?

Quick quiz 3 / 3

What does rel="noopener" do on a link with target="_blank"?

What to remember

  • <a href> is a link; the start of the href (https://, /, a name, #, mailto:, tel:) decides where it goes.
  • Write external sites as full URLs with https://.
  • Use target="_blank" only when needed, with rel="noopener noreferrer" and a visible hint.
  • Link text must describe the destination even when read on its own.

HTML Living Standard — Links — WHATWG

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Links | Thuta Learning