HTML Links

Link to other pages, sections, emails and phone numbers.

Lesson 13 of 44Beginner13 min read

Links are what make the web a web. The <a> element (short for anchor) turns text, or even an image, into something clickable that takes the visitor somewhere else.

Links connect your pages to each other and to the rest of the internet. They also guide search engines: crawlers discover new pages by following links, and the words inside a link tell them what the destination page is about.

In this lesson you'll learn how to write a link, how relative and absolute addresses differ, and how to make links that open email, start a phone call or jump to a section of the same page.

Absolute and relative URLs

  • An absolute URL includes everything, starting with https://, for example https://www.example.com/about. Use it when linking to other websites.
  • A relative URL is a path from the current page, such as about.html, images/cat.jpg or /contact. Use it for pages on your own site.
hrefGoes to
contact.htmlcontact.html in the same folder
blog/post.htmlpost.html inside the blog folder
../index.htmlindex.html one folder up
/pricing/pricing from the root of the site

Relative links keep working when you move your site to a new domain, which is one reason developers prefer them for internal pages.

HTML Links Syntax

Link syntax

<a href="URL">link text</a>
<a href="URL" target="_blank" rel="noopener">opens in new tab</a>
<a href="#section-id">jump to section</a>

HTML Links Summary

  • The <a> element creates a link, and its href attribute sets the destination.
  • Write link text that describes where the link goes.
  • Use absolute URLs for other websites and relative URLs for your own pages.
  • #id, mailto: and tel: links jump to sections, open email and start calls.
  • Use links to go somewhere and buttons to do something.

HTML Links Examples

Each example runs in the page. Edit the code and press Run to see what changes.

Example 1

Different types of links

Click them in the result. (Navigation is disabled in previews, so the console tells you where each link would go.)

<h2>Useful links</h2>
<p><a href="https://developer.mozilla.org">MDN Web Docs</a>: the web's reference manual.</p>
<p><a href="https://www.w3.org" target="_blank" rel="noopener">W3C</a> opens in a new tab.</p>
<p>Questions? <a href="mailto:help@sproutbakery.com">Email the bakery</a>.</p>
<p>Call us: <a href="tel:+441179460000">0117 946 0000</a></p>
ResultEdit the code, then press Run or Ctrl+Enter
Line-by-line explanation
  1. Line 2A normal absolute link.
  2. Line 3target="_blank" opens a new tab; rel="noopener" is a safety habit.
  3. Line 4mailto: opens an email app with the address filled in.
  4. Line 5tel: lets phone users tap to call.
Example 2

Jump links (table of contents)

Click "Toppings" in the result and the page jumps to that section.

<nav>
  <a href="#dough">Dough</a> ·
  <a href="#sauce">Sauce</a> ·
  <a href="#toppings">Toppings</a>
</nav>

<h2 id="dough">Dough</h2>
<p>Flour, water, yeast, salt. Rest it overnight.</p>
<p style="height: 300px">(lots of dough wisdom…)</p>

<h2 id="sauce">Sauce</h2>
<p>Crushed tomatoes, garlic and basil.</p>
<p style="height: 300px">(sauce secrets…)</p>

<h2 id="toppings">Toppings</h2>
<p>Mozzarella. Always mozzarella.</p>
ResultEdit the code, then press Run or Ctrl+Enter
Line-by-line explanation
  1. Lines 2 to 4Each link points to an id with a # in front.
  2. Line 7id="dough" is the target for href="#dough".
  3. Line 9Extra height so you can see the page actually jump.
Example 3

An image as a link

Anything can go inside <a>, including an image.

<a href="https://www.nasa.gov">
  <img src="https://picsum.photos/id/1043/240/140" alt="NASA website" width="240" height="140">
</a>
ResultEdit the code, then press Run or Ctrl+Enter
Line-by-line explanation
  1. Lines 1 to 3The whole image is clickable. Its alt text becomes the link's name for screen readers, so describe where it goes.

Common HTML Links Mistakes

Vague link text

Don't
<p>To see our menu, <a href="menu.pdf">click here</a>.</p>
Do
<p>See our <a href="menu.pdf">lunch menu (PDF)</a>.</p>

Screen-reader users often browse a list of links. Ten links all saying "click here" are useless. Descriptive text also helps SEO.

Forgetting https:// for other websites

Don't
<a href="www.example.com">Example</a>
Do
<a href="https://www.example.com">Example</a>

Without https://, the browser thinks www.example.com is a file in your own folder.

HTML Links in Real Projects

Internal links build your site

Navigation menus, "related articles", breadcrumbs and footers are all just lists of links. Search engines discover your pages by following links, so linking related pages together (like the "Related topics" at the bottom of this lesson) helps people and Google find everything.

HTML Links Exercises

Answer each one and check it instantly. If you get stuck, open the hint before the answer.

Fill in the blankseasy

Make a link to https://www.bbc.co.uk that opens in a new tab.

<a ="https://www.bbc.co.uk" target="">BBC News</a>
Multiple choiceeasy

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

Multiple choicemedium

Which link text is best?

Fix the bugmedium

This contact section is broken: the email link doesn't open an email app and the website link goes to a local file. Fix both.

<p><a href="team@greenfield.org">Email the team</a></p>
<p><a href="www.greenfield.org">Visit our website</a></p>
ResultEdit the code, then press Run or Ctrl+Enter
Checks
  • Email link uses mailto:
  • Website link is absolute with https://

HTML Links Quiz

4 questions to check what you learned.

  1. What does the a in <a> stand for?

    • address
    • anchor
    • action
    • attribute
  2. Which attribute opens a link in a new tab?

    • new="tab"
    • href="_new"
    • target="_blank"
    • open="tab"
  3. <a href="images/logo.png"> is an example of…

    • an absolute URL
    • an email link
    • a broken link
    • a relative URL
  4. What does href="../about.html" point to?

    • about.html in the parent folder
    • about.html in a sub-folder
    • about.html on another website
    • The about section of the current page

HTML Links Challenge

Build a mini table of contents

Create a page with:

  1. A <nav> containing two links: "Rules" → #rules and "Prizes" → #prizes
  2. An <h2 id="rules"> and an <h2 id="prizes"> each followed by a paragraph
  3. A paragraph at the end with a link to mailto:quiz@example.com
<h1>Pub Quiz Night</h1>
ResultEdit the code, then press Run or Ctrl+Enter
Checks
  • Nav links point to #rules and #prizes
  • Targets exist with matching ids
  • An email link to quiz@example.com