4 / 10 · Intro
Links
The H in HTML stands for hypertext: text that links to other text. One element, a, makes every link on the web.
Jump to the 10 practice questionsYou will learn
- Choose absolute, relative and same-page addresses
- Write link text that says where the link goes
Before this: Headings and paragraphs
1Explain
The anchor element
A link is made with the <a> element, short for anchor. Its href attribute holds the address to go to, and its content is the text people click. In <a href="https://developer.mozilla.org/">MDN Web Docs</a>, the words MDN Web Docs appear underlined, and activating them loads that address. Links work with a mouse, a finger, or the keyboard: press Tab to move from link to link and Enter to follow one.
<p>Read the <a href="https://developer.mozilla.org/">MDN guide</a>.</p>2Explain
Absolute and relative addresses
An absolute URL contains everything: the scheme (https:), the domain and the path, for example https://example.com/menu/bread.html. Use it for links to other sites. A relative URL leaves out the parts that are the same as the current page. On a page at https://example.com/menu/index.html, the link bread.html means the file in the same folder, ../about.html means one folder up, and /contact.html starts from the root of the site.
<a href="https://www.w3.org/">W3C</a> <!-- another site --> <a href="bread.html">Bread</a> <!-- same folder --> <a href="../about.html">About us</a> <!-- one folder up --> <a href="#hours">Opening hours</a> <!-- same page --> <a href="mailto:hello@example.com">Email us</a> <h2 id="hours">Opening hours</h2>
Five kinds of link target. The last line is the element that #hours jumps to.
3Problem
A relative address
On https://example.com/menu/index.html, where does bread.html point?
Hint
A relative address starts from the folder of the current page.
Solution
Answer: https://example.com/menu/bread.html
A plain file name is relative to the current folder, /menu/.
4Problem
One folder up
On the same page, where does ../about.html point?
Hint
.. means the folder above.
Solution
Answer: https://example.com/about.html
../ goes up one folder, from /menu/ to the root.
5Explain
Links inside the same page
Give any element an id, and a link whose href is a hash followed by that id jumps straight to it. <a href="#hours"> scrolls to <h2 id="hours">. An id must be unique within the page and cannot contain spaces. Long articles use this for a table of contents.
Click the link in the preview: it jumps to the element whose id matches.
<p><a href="#hours">Opening hours</a></p>
<p style="height:260px">Scroll down…</p>
<h2 id="hours">Opening hours</h2>
<p>Daily from 7 am.</p>6Problem
href="#prices"
What does href="#prices" link to?
Hint
A hash links to an id on the same page.
Solution
Answer: The element with id="prices" on this page
A hash plus an id is a fragment link within the page.
7Problem
A jump that does not work
<p>
<a href="#Hours">Opening hours</a>
</p>
<h2 id="hours">Opening hours</h2>Clicking the link does not jump to the heading. Why?
Hint
Compare the two spellings letter by letter.
Solution
Answer: #Hours and hours differ in capital letters
The part after # must match the id exactly, capital letters included. Write href="#hours".
8Explore
Email, phone, downloads and link text
target="_blank" opens the link in a new tab. Use it sparingly: it takes control away from the visitor, and the Back button no longer brings them back. When you do use it, mention it in the link text, for example (opens in a new tab). Most important of all is the link text itself. Screen reader users often pull up a list of every link on a page, and a list of ten entries that all say click here or read more tells them nothing.
Compare the link texts: the first three say where they lead; click here says nothing when read on its own.
Tap a choice: the code and the preview change together.
<p><a href="mailto:hello@example.com">Email the bakery</a></p>Nothing is drawn: there is no visible content.
9Problem
The best link text
Which link text is best?
Hint
Imagine hearing only the link text in a list of links.
Solution
Answer: Download the 2026 timetable
It makes sense even in a list of links with no surrounding text.
10Problem
Real links
<p>
<a href="#hours">Hours</a> ·
<a>Prices</a> ·
<a href="tel:+12025550123">Call</a>
</p>How many of these a elements are links that can be followed?
Hint
A link needs an address.
Solution
Answer: 2
The first and the last a have href. An a without href is only a placeholder, not a link, so the answer is 2.
11Problem
A phone link
Which href makes a link that starts a phone call?
Hint
It is short for telephone.
Solution
Answer: tel:+12025550123
tel: is the scheme for phone numbers, like mailto: for email. On a phone, tapping the link offers to call.
12Practice
A link out and a link in
Make MDN a link to https://developer.mozilla.org/. Give the FAQ heading id="faq" and make the FAQ in the second line a link to it.
- link to MDN
- id="faq" on the heading
- link to #faq
<p>Read the HTML guide on MDN.</p>
<p>Jump to the FAQ.</p>
<h2>Lessons</h2>
<p>Ten short units.</p>
<h2>FAQ</h2>
<p>Is it free? Yes.</p>Hint
The same-page link needs both parts: href="#faq" on the link and id="faq" on the heading.
A solution
<p>Read the HTML guide on <a href="https://developer.mozilla.org/">MDN</a>.</p>
<p>Jump to the <a href="#faq">FAQ</a>.</p>
<h2>Lessons</h2>
<p>Ten short units.</p>
<h2 id="faq">FAQ</h2>
<p>Is it free? Yes.</p>13Practice
An email link
Replace the vague link with one that opens an email to hello@example.com and whose text says Email the bakery.
- mailto link
- clear link text
<p>Questions? <a href="#">Click here</a>.</p>Hint
href starts with mailto: and the link text says what happens.
A solution
<p>Questions? <a href="mailto:hello@example.com">Email the bakery</a>.</p>14Explain
Common mistakes
<p><a href="faq">FAQ</a></p>
<h2 id="faq">FAQ</h2><p><a href="#faq">FAQ</a></p>
<h2 id="faq">FAQ</h2>Without the hash, the browser looks for a file called faq instead of the heading on this page.
15
Summary
- a + href makes a link; the content is the clickable text.
- Absolute URLs for other sites, relative URLs inside your site, #id for the same page.
- Write link text that makes sense on its own.