Lesson 4 of 23
In progress
Links that go where they say
Connect pages with relative and absolute links, link within a page, and write link text that makes sense out of context.
The link is what makes a collection of pages a web. It is also the element most often written carelessly: "click here" tells a screen reader user nothing, and a wrong path produces the 404 you spend an hour debugging.
What you will learn
- Write links with
hrefand understand what the browser does with it. - Tell relative, root-relative and absolute paths apart.
- Link within a page and open new tabs responsibly.
The anchor element
Editable. Runs in your browser.
Press Run to see the output.
href is the destination. The text between the tags is the link, and it is what a person reads and what a screen reader announces. Vague text such as "here" or "read more" is a problem because the destination is invisible when links are read out of context.
Write the link text so the sentence works without the surrounding paragraph: "the MDN guide to Flexbox" beats "this guide".
Three kinds of path
A relative path is resolved from the current file’s location. about.html means a file beside this one. ../images/logo.png means up one folder, then into images.
A root-relative path starts with a slash and is resolved from the site root: /tutorials/python/. It works on the server and in local development if the server is rooted at the project folder. It breaks when you open the file directly with file://, because the root is then your hard drive.
An absolute URL includes the protocol and host: https://example.com/about. Use it for other sites. For your own pages, relative paths survive domain changes and work on staging.
Editable. Runs in your browser.
Press Run to see the output.
Changing the site from http://localhost:8000 to a real domain breaks nothing in the first link and everything in a hard-coded localhost absolute URL. That is the practical argument for relative paths.
Link to a section on the page
An id on an element creates a target. href="#setup" scrolls to it.
Editable. Runs in your browser.
<h2>Introduction</h2>
<p>Some introductory text.</p>
<h2 id="setup">Setup</h2>
<p>The part you came for.</p>
Press Run to see the output.
The fragment is handled by the browser, not the server. Ids must be unique on the page; duplicates make the browser pick one and confuse everything else. A "back to top" link usually points at #top with an id on the header or uses href="#", though the latter changes the URL and can behave inconsistently.
New tabs need a reason
target="_blank" opens a new tab. It is useful for a reference the reader will return from, and annoying when every link does it.
If you use it, add rel="noopener" (modern browsers imply it, but being explicit is harmless) and consider noreferrer. Also tell the reader the link opens a new tab, either in the text or with a visually hidden note; unexpected context changes are an accessibility problem.
Editable. Runs in your browser.
Press Run to see the output.
Email, phone and downloads
mailto: opens the mail client, tel: dials on a phone, and the download attribute suggests saving instead of navigating.
Editable. Runs in your browser.
Press Run to see the output.
A mailto: link is not a contact form. It depends on the visitor having a mail client configured, and it exposes the address to scrapers. A form on the server is the reliable option when you need to receive messages.
Writing <a href="page.html"> for a page in a subfolder produces a 404 that looks like the file vanished. Relative paths are resolved from the document, not the site root. Check the Network panel: the request URL tells you exactly where the browser looked.
Write a navigation list with three links: one to index.html, one to about.html in the same folder, and one to an external documentation site that opens in a new tab with the reader warned. Then add a "skip to content" link pointing at a main element with an id.
Show solution
Editable. Runs in your browser.
<nav aria-label="Main">
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="about.html">About</a></li>
<li>
<a href="https://developer.mozilla.org/" target="_blank" rel="noopener">
MDN (opens in a new tab)
</a>
</li>
</ul>
</nav>
<main id="content">
<h1>Page content</h1>
</main>
Press Run to see the output.
The skip link comes first in the source so it is the first thing a keyboard user reaches. The nav has a label because a page can have more than one navigation region.
Recap
hrefsets the destination and the link text describes it; avoid "click here".- Relative paths resolve from the document, root-relative from the site root, absolute from the protocol and host.
#idlinks within a page, andtarget="_blank"needs a warning andrel="noopener".
Next, you will add images with alternative text that actually helps.
Should links be underlined?
Can I link to a PDF?
Progress is saved in this browser only. No account, no email.