Sub topic 10
Links and URLs
This chapter assumes you can write a basic HTML document and that you know what an element and an attribute are. It covers the a element, the kinds of URLs you put in href, and the errors that most often break links.
How a link works
A link is an a (anchor) element with an href attribute. The href value is the URL the browser loads when the user activates the link. The content of the element becomes the clickable text.
<p>Read the <a href="https://developer.mozilla.org/" title="MDN Web Docs">documentation</a> today.</p>Without href, an a element is only a placeholder. The browser does not make it focusable or clickable. If the action is not navigation, use a button instead.
Absolute and relative URLs
An absolute URL includes the scheme (such as https:) and the host. A relative URL leaves those out, and the browser resolves it against the address of the current page.
| Kind | Example | Resolves to, from `https://example.com/blog/post.html` |
|---|---|---|
| Absolute | `https://example.org/` | `https://example.org/` |
| Root-relative | `/about.html` | `https://example.com/about.html` |
| Document-relative | `images/a.html` | `https://example.com/blog/images/a.html` |
| Parent folder | `../index.html` | `https://example.com/index.html` |
| Fragment only | `#comments` | `https://example.com/blog/post.html#comments` |
Use absolute URLs for other sites. Use relative URLs for pages on your own site, so the links still work when you move the site to another domain or open it from your disk.
placeholderLinking within a page
A fragment is the part of a URL after #. It names the id of an element on the target page, and the browser scrolls to that element. IDs must be unique in a document.
<a href="#summary">Jump to the summary</a>
<h2 id="summary">Summary</h2>
<p>Links connect pages.</p>You can combine a path and a fragment, as in guide.html#install.
Other link types
The scheme decides what the browser does with the URL.
mailto:[email protected]opens the user's email program.tel:+15551234567offers to start a phone call on devices that support it.- A link to a PDF or other file simply points to that file's URL.
To suggest that the browser download a same-site file instead of opening it, add the download attribute.
Opening links in a new tab
target="_blank" opens the link in a new browsing context. Use it sparingly, because it takes control of navigation away from the user. If you use it, tell the user in the link text. For links to other sites, add rel="noopener noreferrer" to be explicit about not sharing the opener window and referrer. Current browsers already treat _blank links as noopener, but older ones do not.
<a href="https://example.org/" target="_blank" rel="noopener noreferrer">Example site (opens in a new tab)</a>Writing good link text
Screen reader users often browse a list of all links on a page, so each link must make sense out of context. "Click here" fails that test.
<!-- Weak -->
<p>To read the guide, <a href="guide.html">click here</a>.</p>
<!-- Better -->
<p>Read the <a href="guide.html">installation guide</a>.</p>Common errors
- Missing scheme.
href="www.example.com"is treated as a relative path, not a website. Writehttps://www.example.com. - Wrong case or spelling in paths. Many servers treat
Page.htmlandpage.htmlas different files. - Spaces in URLs. Use hyphens in file names. A space must be written as
%20. - Broken relative paths. After you move a file to another folder,
../andimages/paths may point to the wrong place. - Duplicate IDs. The fragment goes to the first match only, which may not be the one you meant.
- Nesting links. An
aelement must not contain anotheraelement or interactive content such as abutton.
Exercise
A page at https://example.com/docs/intro.html contains <a href="../images/logo.png">. What full URL does this link point to, and how would you write a link to the section with id="faq" on the same page?
Show answer
The link points to https://example.com/images/logo.png, because ../ moves up from the docs folder to the site root. The same-page link is <a href="#faq">FAQ</a>.