Learning goals: By the end, you'll know how to create links to other pages, other websites, sections of the same page, email addresses and files.
What is a link?
Links (hyperlinks) are what make the web a web. They are created with the anchor element, <a>, and its href attribute.
htmlCopy<a href="https://example.com">Visit Example</a>
Try:Agent|Desktop-
<a>is the anchor tag. -
href(hypertext reference) is the destination. - The content between the tags is the link text people click.
Absolute vs. relative URLs
| Type | Example | Use it for |
|---|---|---|
| Absolute | https://example.com/about.html | Pages on other websites |
| Relative | about.html | Pages in your own project |
Relative paths depend on where your files sit:
htmlCopy<a href="about.html">Same folder</a>
<a href="pages/contact.html">Inside a subfolder</a>
<a href="../index.html">One folder up</a>
Try:Agent|DesktopUse relative links for your own pages. They keep working when you move your site to a new domain.
Opening links in a new tab
htmlCopy<a href="https://example.com" target="_blank" rel="noopener noreferrer">Open in new tab</a>
Try:Agent|Desktop-
target="_blank"opens the link in a new tab. -
rel="noopener noreferrer"is a security best practice for external links. - Use new tabs sparingly. Many users prefer to choose for themselves.
Linking within the same page (anchors)
Give an element an id, then link to it with #.
htmlCopy<a href="#contact">Jump to Contact</a>
<!-- ...lots of content... -->
<h2 id="contact">Contact</h2>
Try:Agent|DesktopYou can also link to a section on another page: <a href="about.html#team">Meet the team</a>.
Email, phone and file links
htmlCopy<a href="mailto:hello@example.com">Email us</a>
<a href="tel:+441234567890">Call us</a>
<a href="guide.pdf" download>Download the guide</a>
Try:Agent|DesktopLinking an image
Put an <img> inside the anchor to make it clickable (images are covered in Lesson 5).
htmlCopy<a href="index.html">
<img src="logo.png" alt="Home">
</a>
Try:Agent|DesktopWriting good link text
| Avoid | Prefer |
|---|---|
Click here | Read our beginner's guide |
Link | Download the price list (PDF) |
Good link text makes sense out of context. Screen reader users often scan a list of links alone.
Useful attributes
| Attribute | Purpose |
|---|---|
href | Destination URL |
target | Where to open the link |
rel | Relationship to the linked page (security, SEO) |
title | Tooltip on hover |
download | Downloads the file instead of opening it |
Common mistakes
-
Forgetting
https://on external links, which makes the browser treat them as relative -
Wrong file paths or mismatched capitalisation (
About.htmlvsabout.html) -
Missing quotes around the
hrefvalue - Vague link text like "click here"
-
Using spaces in file names (use
my-page.html)
Key takeaways
-
<a href="...">creates a link. - Use absolute URLs for other sites and relative URLs for your own pages.
-
#idlinks jump to a section on a page. -
mailto:andtel:open email and phone apps. - Descriptive link text improves accessibility.
Exercises
-
Identify. In
<a href="contact.html" target="_blank">Contact us</a>, is the URL absolute or relative? What doestargetdo?
Answer: relative.target="_blank"opens the link in a new tab. -
Fix the errors.
htmlCopy<a href=example.com>Visit<a> <a href="mailto hello@example.com">Email me</a> Try:Agent|Desktop
Hints: quote thehrefand addhttps://, close with</a>, and usemailto:with a colon. -
Build it. Create two files,
index.htmlandabout.html. Add:-
a link from
index.htmltoabout.html -
a link back from
about.htmltoindex.html - an external link that opens in a new tab
-
a
mailto:link
-
a link from
-
Challenge. On a long page, create a "table of contents" at the top with
#idlinks to three sections. Add a "Back to top" link at the bottom of each section.
Lesson 4 checkpoint
-
I can create a link with
<a href>. - I know when to use absolute and relative URLs.
-
I can link to a section of a page using
#id. - I can write clear, descriptive link text.