Links
Links are what make the web a web. The "HyperText" in HTML means text that links to other text. With the <a> (anchor) element you can link to other websites, to other pages in your own site, to a spot on the same page, to an email address or to a phone number.
The a element and href
<a href="https://www.example.com">Visit the example site</a>
The href attribute (hypertext reference) holds the destination. The content between the tags is the clickable link text. Browsers show unvisited links blue and underlined and visited links purple, until you change that with CSS.
Absolute and relative URLs
An absolute URL includes the protocol and domain, and is used for links to other websites: https://www.example.com/news.
A relative URL is a path from the current page to another file in the same site. Imagine this folder structure:
my-site/
index.html
about.html
projects/
weather-app.html
images/
logo.png
| From | To | href |
|---|---|---|
index.html | about.html | about.html (same folder) |
index.html | weather-app.html | projects/weather-app.html (go into a folder) |
weather-app.html | index.html | ../index.html (.. means "up one folder") |
| any page | the site's root | / (starts from the top of the site) |
Relative links keep working when you move the whole site to a new domain, so prefer them for your own pages.
Writing href="www.example.com" without https://. The browser treats it as a relative path and looks for a file called www.example.com in your own folder, which gives a 404. External links always need the protocol.
Opening in a new tab
The target="_blank" attribute opens the link in a new tab:
<a href="https://www.example.com" target="_blank" rel="noopener">Example (opens in a new tab)</a>
Use it sparingly. Unexpected new tabs confuse many users, especially on phones and with screen readers, and they break the Back button. If you do use it, say so in the link text. The rel="noopener" stops the new page from controlling your page through JavaScript. Modern browsers already apply this behaviour to target="_blank" automatically, but adding it is harmless and protects users on older browsers.
Linking to part of a page
Give any element an id, then link to it with # followed by that id. This is how "Back to top" links and tables of contents work.
<a href="#fees">Jump to fees</a> … <h2 id="fees">Fees</h2>
You can combine a page and a fragment: about.html#team opens about.html and scrolls to the element with id="team". A link to just #top scrolls to the top of the page, even if no element has that id.
Email, phone and download links
<a href="mailto:hello@example.com">Email us</a> <a href="tel:+254700000000">Call +254 700 000 000</a> <a href="files/brochure.pdf" download>Download our brochure (PDF, 1.2 MB)</a>
mailto:opens the visitor's email app with a new message to that address.tel:starts a call on a phone, or offers a calling app on a computer. Use the international format with the country code, such as +254 for Kenya, +256 for Uganda or +255 for Tanzania.- The
downloadattribute asks the browser to save the file instead of opening it. It only works for files on the same site. Mention the file type and size so people on limited data bundles can decide.
Writing good link text
Many screen-reader users pull up a list of every link on a page. A list of "click here, click here, read more" is useless. Link text should make sense on its own.
<!-- Vague --> <p>To see our fees, <a href="fees.html">click here</a>.</p> <!-- Clear --> <p>See the <a href="fees.html">2027 course fees</a>.</p>
Use a link (<a>) when something takes the user somewhere. Use a button (<button>) when it does something on the page, like submitting a form or opening a menu. Mixing them up confuses keyboard and screen-reader users.
Try it
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Links</title>
</head>
<body>
<h1 id="top">Nyota Coding Club</h1>
<p>Jump to: <a href="#meetings">Meetings</a> | <a href="#contact">Contact</a></p>
<h2 id="meetings">Meetings</h2>
<p>We meet every Saturday at 2 pm. Bring a laptop if you have one.</p>
<p>Read the <a href="https://developer.mozilla.org/en-US/docs/Web/HTML">MDN guide to HTML</a> before your first session.</p>
<h2 id="contact">Contact</h2>
<p><a href="mailto:club@example.com">Email the club</a> or
<a href="tel:+254700000000">call +254 700 000 000</a>.</p>
<p><a href="#top">Back to top</a></p>
</body>
</html>
The "Meetings", "Contact" and "Back to top" links scroll within the preview (make the preview short or add more text to see the jump). The external, email and phone links may be blocked inside the preview because it runs in a protected frame; save the code as a file and open it in your browser to test them fully.
Key points
<a href="…">creates a link; the content is the clickable text.- Use absolute URLs (with
https://) for other sites and relative paths for your own pages;..goes up a folder. #idlinks jump within a page;mailto:andtel:open email and phone apps.- Write link text that makes sense out of context, and avoid unnecessary new tabs.
Tip: use the ← and → keys to move between pages.
