Links and Navigation

Introduction

Links are what make the web a web. The <a> element connects pages, files, email addresses, phone numbers, and in-page sections. This chapter covers href, absolute and relative URLs, safe external links, anchor jumps, and building navigation with <nav>.

Prerequisites

The Anchor Element

html
<a href="https://developer.mozilla.org/">MDN Web Docs</a>
PartRole
aAnchor element
hrefHypertext reference — where the link goes
Link textVisible label users click

Good link text describes the destination:

html
<a href="/docs/html/01_what_is_html">Read the HTML introduction</a>

Weak link text:

html
<a href="/docs/html/01_what_is_html">click here</a>

Tip

Link Text Stands Alone

Screen reader users often jump between links. “Click here” repeated ten times is useless out of context.

Absolute URLs

An absolute URL includes the full address:

html
<a href="https://www.example.com/pricing">Pricing</a>
<a href="mailto:support@example.com">Email support</a>
<a href="tel:+15551234567">Call us</a>

Use absolute URLs for:

  • external sites
  • mailto: and tel: links
  • canonical links to a known domain

Relative URLs

A relative URL is resolved against the current page location.

If your site looks like:

text
site/
├── index.html
├── about.html
└── docs/
    └── guide.html

From index.html:

html
<a href="about.html">About</a>
<a href="docs/guide.html">Guide</a>

From docs/guide.html back to home:

html
<a href="../index.html">Home</a>
PathMeaning
about.htmlSame folder
docs/guide.htmlChild folder
../index.htmlParent folder
/contact.htmlSite root path (depends on server)

Relative links make local development and deployment portable. Keep folder names consistent and avoid spaces in paths.

Linking to Files

Download or open non-HTML files:

html
<a href="files/report-2026.pdf">Download 2026 report (PDF)</a>
<a href="images/photo.webp">View photo</a>

Optional download hint:

html
<a href="files/report-2026.pdf" download>Download report</a>

The download attribute suggests downloading instead of navigating, but browser behavior varies. Always use clear link text that mentions the file type.

html
<a
  href="https://example.com"
  target="_blank"
  rel="noopener noreferrer"
>
  Visit Example (opens in new tab)
</a>
AttributePurpose
target="_blank"Open in new browsing context (tab/window)
rel="noopener"Prevents the new page from accessing window.opener
rel="noreferrer"May omit referrer header (browser-dependent)

Warning

Use New Tabs Sparingly

Opening every link in a new tab disorients users and breaks the back button habit. Reserve it for external references where staying on your site still matters.

In-Page Anchors

Jump to a section on the same page with a fragment:

Rules:

  • Put id on the target element (usually a heading or section)
  • Use href="#id" on the link
  • id values must be unique on the page
  • Use short, descriptive ids: setup, not section-2-final-v3

Link from another page to a specific section:

html
<a href="guide.html#setup">Jump to Setup in the guide</a>

The browser loads guide.html and scrolls to #setup.

Building Navigation with <nav>

Wrap major navigation links in <nav>:

aria-label helps when a page has more than one nav (main, footer, sidebar). aria-current="page" marks the active link for assistive technology.

Navigation can be a list:

html
<nav aria-label="Main">
  <ul>
    <li><a href="index.html">Home</a></li>
    <li><a href="articles.html">Articles</a></li>
    <li><a href="about.html">About</a></li>
  </ul>
</nav>

Lists announce the number of items to screen readers—a nice improvement for menus.

Show hierarchy on documentation or e-commerce sites:

html
<nav aria-label="Breadcrumb">
  <ol>
    <li><a href="/">Home</a></li>
    <li><a href="/docs/">Docs</a></li>
    <li><a href="/docs/html/">HTML</a></li>
    <li aria-current="page">Links</li>
  </ol>
</nav>

The last item is the current page and often is not a link.

Browsers style links with pseudo-classes (CSS topic):

  • unvisited
  • visited
  • hover
  • active (while clicking)
  • focus (keyboard)

HTML defines the link; CSS styles the states. Keep focus visible for keyboard users when you add styles later.

Mini Project: Three-Page Site

Create:

text
mini-site/
├── index.html
├── about.html
└── contact.html

Each page should share:

  • the same <nav> with relative links
  • one h1 describing the page
  • aria-current="page" on the correct nav link
  • one in-page anchor on index.html (for example #features)

Test every link locally with Live Server.

Common Mistakes

Empty or missing href

html
<a>Broken link</a>

Always provide href unless you have a deliberate JavaScript pattern (advanced).

Wrong relative path after moving files

If about.html moves into pages/about.html, update every link that pointed to it.

Duplicate id values

Two elements with id="intro" break anchor targets.

Use <a href="..."> for navigation and <button type="button"> for actions on the same page. Mixing them confuses semantics and keyboard behavior.

FAQ

What is the difference between href and src?

href is for links (user navigates). src is for embedded resources (browser loads into the page), such as images and scripts.

Browsers do not jump reliably without an id (or legacy name on anchors). Add id to the target heading or section.

No. Default same-tab navigation is usually better. Use new tabs when leaving your docs would interrupt a task the user is completing on your site.

href="#" jumps to the top of the page. Empty javascript placeholders are an old pattern—avoid them in modern accessible pages.

What comes next?

Images and responsive imagesimg, alt, srcset, and <picture>.