Links are what make the web a web. A link is created with the anchor element, <a>, and its destination is set by the href attribute (hypertext reference).
<a href="https://example.com">Visit an example site</a>The text between the tags is the link text. Browsers show it underlined and coloured by default.
Absolute and relative URLs#
An absolute URL contains the full address, including the protocol and domain. Use it to link to other websites.
A relative URL describes a location compared with the current file. Use it for pages in your own site.
<!-- Absolute: a different website -->
<a href="https://developer.mozilla.org/">MDN Web Docs</a>
<!-- Relative: same folder -->
<a href="menu.html">Our menu</a>
<!-- Relative: inside a sub-folder -->
<a href="events/open-mic.html">Open mic night</a>
<!-- Relative: go up one folder, then down -->
<a href="../index.html">Back to home</a>
<!-- Relative to the site root -->
<a href="/contact.html">Contact</a>Relative links keep working when you move the whole site to a new domain, which is why they are preferred internally.
Links within a page#
Give an element an id, then link to it with a hash. This is how "skip to content" and table-of-contents links work.
<a href="#opening-hours">Jump to opening hours</a>
<!-- ...much later on the page... -->
<h2 id="opening-hours">Opening hours</h2>You can combine both: menu.html#desserts opens the menu page scrolled to the element with id="desserts".
Email and phone links#
<a href="mailto:hello@brewandbyte.example">Email us</a>
<a href="tel:+9779800000000">Call the cafe</a>The mailto and tel schemes open the visitor's email app or dialler.
Opening in a new tab#
<a href="https://example.com/map" target="_blank" rel="noopener noreferrer">
Find us on the map (opens in a new tab)
</a>target="_blank" opens a new tab. Add rel="noopener noreferrer" when you do, so the new page cannot manipulate the page that opened it and does not receive the referring address. Modern browsers imply noopener, but being explicit is harmless. Tell visitors when a link opens a new tab, because it changes how the back button behaves.
Writing good link text#
Link text should make sense on its own, because screen reader users often list all links on a page.
- Weak: "Click here" or "Read more".
- Better: "Read our allergy policy".
You can also link a whole image or card by putting content inside the anchor, but make sure the link still has an accessible name.
Common mistakes#
- Using "click here" as link text.
- Linking with
href="www.example.com". Withouthttps://the browser treats it as a relative path. - Spaces or capitals in file names causing broken relative links.
- Opening every link in a new tab. Reserve it for cases where leaving would lose the visitor's work.
- Forgetting
relwithtarget="_blank".
Try it yourself#
Create index.html and menu.html in one folder. Link them to each other with relative URLs. On the menu page add three headings with ids and a mini table of contents linking to them. Add one external link opening in a new tab with a proper rel, and one email link.
Recap#
The a element with href makes links. Use relative URLs inside your site, absolute URLs for other sites, hashes for in-page jumps, and mailto or tel for contact. Write descriptive link text and pair target="_blank" with rel="noopener noreferrer".
Check your understanding
Lesson quiz
OptionalA short quiz to check what you just learned.
- Questions
- 4
- Time limit
- 5 min
- Pass mark
- 70%
- Best score
- -