Sub topic 10
Links and URLs
This chapter assumes you can write a complete HTML document, you know the parts of an element (start tag, content, end tag, attributes), and you can open a local file in a browser. It covers the a element, how URLs are structured, and the errors that most often break links.
What a link is
A link, also called a hyperlink, connects the current document to another resource. The resource can be another page, a spot on the same page, a file, or an email address. You create one with the a (anchor) element. Its href attribute (hypertext reference) holds the destination.
<p>Read the <a href="https://developer.mozilla.org/">MDN Web Docs</a> for details.</p>The content of the element is the link text. Browsers show it underlined and colored by default, and users can click it, tab to it, or activate it with the Enter key. An a element with no href is a placeholder. It is not interactive and is not keyboard focusable.
a is an inline element, so it sits inside a paragraph or other text. HTML5 also allows a to wrap block-level content such as a whole card, as long as the content inside contains no other interactive elements (no nested links or buttons).
Anatomy of a URL
A URL (Uniform Resource Locator) is the address of a resource. A full URL has several parts, and each one answers a different question.
The table below summarizes each part.
| Part | Example | Notes |
|---|---|---|
| Scheme | `https:` | Also `http:`, `mailto:`, `tel:`, `file:` |
| Host | `example.com` | A domain name or IP address |
| Port | `:8080` | Optional. Defaults are 443 for https and 80 for http |
| Path | `/docs/intro.html` | Maps to a resource on the server |
| Query | `?lang=en` | Key=value pairs joined with `&` |
| Fragment | `#install` | Handled by the browser and never sent to the server |
Absolute and relative URLs
An absolute URL includes the scheme and host. It works from anywhere. A relative URL leaves those parts out and is resolved against the URL of the current document, called the base URL.
Suppose the current page is https://example.com/blog/2026/post.html. The table shows how relative href values resolve.
| href value | Resolves to |
|---|---|
| `other.html` | `https://example.com/blog/2026/other.html` |
| `images/a.png` | `https://example.com/blog/2026/images/a.png` |
| `../index.html` | `https://example.com/blog/index.html` |
| `/about.html` | `https://example.com/about.html` |
| `//cdn.example.org/x.js` | `https://cdn.example.org/x.js` |
A leading / means "from the root of the site". ../ means "go up one folder". A bare name means "in the same folder as this page". Use relative URLs for pages within your own site, so the site still works when you move it to another domain or test it locally. Use absolute URLs for other sites.
<nav>
<a href="index.html">Home</a>
<a href="guides/setup.html">Setup guide</a>
<a href="../contact.html">Contact</a>
<a href="https://www.w3.org/">W3C</a>
</nav>When you open a file directly from disk, the browser uses a file: base URL. Relative links to other files in the same folder still work, but root-relative links such as /about.html point to the root of your drive, not your project folder. This is a common surprise when you test locally.
Linking to a spot on a page
A fragment identifier jumps to the element whose id matches it. Each id must be unique in the document.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Fragment Demo</title>
</head>
<body>
<p><a href="#install">Skip to installation</a></p>
<h2 id="overview">Overview</h2>
<p>Some text about the project.</p>
<h2 id="install">Installation</h2>
<p>Run the installer.</p>
<p><a href="#top">Back to top</a></p>
</body>
</html>The link #top is a special case. If no element has id="top", the browser scrolls to the top of the document. You can combine a path and a fragment, as in guides/setup.html#install. If the id does not exist, the page loads and stays at the top. The browser shows no error.
Other link types
The scheme decides what the browser does with the link.
<p><a href="mailto:[email protected]?subject=Question">Email support</a></p>
<p><a href="tel:+15555550123">Call us</a></p>
<p><a href="files/guide.pdf" download>Download the guide</a></p>mailto:opens the user's email program. Spaces and special characters in the subject must be percent-encoded, for examplesubject=Hello%20there.tel:lets phones start a call. Use the international format with the country code.downloadsuggests that the browser save the file instead of opening it. It only works for same-origin URLs.
Opening links in a new tab
The target attribute chooses where the destination loads. The default, _self, replaces the current page. The value _blank opens a new browsing context, usually a tab.
<a href="https://example.com/" target="_blank" rel="noopener noreferrer">Example (opens in a new tab)</a>The rel attribute describes the relationship between the two documents. noopener stops the new page from controlling the original through window.opener. Modern browsers apply noopener automatically for target="_blank", but writing it explicitly is harmless and helps older browsers. noreferrer additionally hides the page address from the destination.
Writing good link text
Link text should make sense on its own. Screen reader users often pull up a list of all links on a page, so "click here" and "read more" are meaningless in that list.
<!-- Weak -->
<p>To learn about forms, <a href="forms.html">click here</a>.</p>
<!-- Better -->
<p>Learn more in the <a href="forms.html">guide to HTML forms</a>.</p>You can also mark an image as a link by putting the img inside the a. In that case the image's alt text becomes the link text, so it must describe the destination, not the picture.
<a href="/"><img src="logo.png" alt="Garden Notes home" width="120" height="40"></a>Special characters in URLs
URLs may contain only a limited set of characters. Spaces, non-ASCII letters and reserved characters must be percent-encoded: a % followed by two hexadecimal digits. A space becomes %20. Inside an HTML attribute, an ampersand separating query parameters should be written as & so the parser does not read it as the start of a character reference.
<a href="https://example.com/search?q=html%20links&page=2">Search results</a>Avoid spaces and capital letters in your own file names. Many servers treat Page.html and page.html as different files, and a link that works on your laptop can break after you upload the site.
Common errors
- Missing scheme on an external link.
href="www.example.com"is treated as a relative path, so the browser looks for a file namedwww.example.comin the current folder. Writehttps://www.example.com. - Wrong relative path. A link to
images/a.pngfrom a page in a subfolder looks in that subfolder. Check the folder of the page, not the folder of the site. - Duplicate or misspelled
id. Fragment links go to the first match, or nowhere. - Nested links. An
ainside anotherais invalid, and parsers will close the first one early. - Using a link as a button. A link navigates. If the action changes data on the page, use a
button. - Quote problems. An unclosed quote in
hrefcan swallow the rest of the tag.
Exercise
Exercise
A page at https://example.com/shop/books/list.html contains the link <a href="../cart.html#total">Cart</a>. What full URL does the link resolve to, and which part of it does the browser not send to the server?
Show answer
It resolves to https://example.com/shop/cart.html#total. The ../ moves up from /shop/books/ to /shop/, then cart.html is added. The fragment #total is handled only by the browser, which scrolls to the element with id="total", and is not sent to the server.
Exercise
You want a link to https://example.org/ that opens in a new tab. Write the element, with link text that tells the user what will happen.
Show answer
<a href="https://example.org/" target="_blank" rel="noopener noreferrer">Example.org (opens in a new tab)</a>