On this page12 sections
Lecture 010: Links
| Category | 3: Links, Images, Tables & Lists |
| Lecture | 010 of 048 |
| Estimated video length | 12 min |
| Target | HTML5 · any modern browser |
| Level | Beginner |
Links are what make the web a web. One element,
<a>, takes you to another site, another page of your own site, a spot further down the page, an email or a phone call.
Learning Objectives
- Create links with
<a href="..."> - Tell absolute URLs and relative paths apart
- Open a link in a new tab safely
- Link to a section of the same page, to email and to a phone number
1. The Anchor Element
Example
<a href="https://www.wikipedia.org">Open Wikipedia</a><a>stands for anchor.hrefis the destination.- The text between the tags is what people click, so make it describe where it goes.
2. Absolute vs Relative Links
An absolute URL is a full address, used for other websites:
https://www.example.com/blog/post.htmlA relative path points to a file on your own site, starting from the current page:
| Link | Goes to |
|---|---|
href="about.html" | about.html in the same folder |
href="blog/post.html" | post.html inside the blog folder |
href="../index.html" | index.html one folder up |
href="/contact.html" | contact.html at the root of the site |
Relative links keep working if you move your whole site to a new domain.
3. Opening in a New Tab
target="_blank" opens the link in a new tab. Add rel="noopener" so the new page can't
control yours.
Example
<a href="https://developer.mozilla.org" target="_blank" rel="noopener">
MDN Web Docs (opens in a new tab)
</a>Use new tabs sparingly, mainly for other websites. Telling people in the link text that a new tab will open is a kind touch.
4. Jumping to Part of a Page
Give the target element an id, then link to #that-id.
Example
<p><a href="#prices">Jump to prices</a></p>
<h2>About us</h2>
<p>We bake fresh bread every morning.</p>
<p>Our ovens start at 5am.</p>
<h2 id="prices">Prices</h2>
<p>Bread: Rs 150</p>5. Email and Phone Links
Example
<p><a href="mailto:hello@example.com">Email us</a></p>
<p><a href="tel:+923001234567">Call 0300-1234567</a></p>On a phone, tapping the tel: link starts a call. mailto: opens the email app.
6. A Picture as a Link
Anything inside <a> becomes clickable, including an image.
Example
<a href="https://www.wikipedia.org">
<img src="https://picsum.photos/id/24/240/140" alt="Open book: visit Wikipedia">
</a>7. Link Colours
By default, browsers show unvisited links in blue, visited links in purple, and the link you're clicking in red. CSS can change each state:
Example
<style>
a:link { color: #1971c2; } /* not visited yet */
a:visited { color: #7048e8; } /* already visited */
a:hover { color: #e8590c; } /* mouse over it */
a:active { color: #c92a2a; } /* being clicked */
</style>
<p><a href="https://www.wikipedia.org" target="_blank" rel="noopener">Hover over and click this link</a></p>Keep them in this order (:link, :visited, :hover, :active), or later rules hide earlier ones.
Links That Look Like Buttons
<style>
a.button { background: #2f9e44; color: white; padding: 10px 18px; border-radius: 6px; text-decoration: none; display: inline-block; }
a.button:hover { background: #2b8a3e; }
</style>
<a class="button" href="#">Sign up free</a>Common Beginner Mistakes
1. "Click here" links. Screen-reader users often jump from link to link, and "click here" says nothing about where it goes. Write "Download the timetable" instead.
2. Forgetting https://. href="www.google.com" is treated as a relative file name
on your own site. Full addresses need https://.
3. Spaces in file names. my page.html causes problems in links. Use my-page.html.
4. target="_blank" everywhere. Opening every link in a new tab clutters the browser
and breaks the Back button.
Practice Questions
- Which attribute holds a link's destination?
- Write a link to
https://www.bbc.comwith the text "BBC News". - Your page is in
/blog/. Write a link toindex.htmlin the folder above. - How do you make a link open in a new tab safely?
- Write a link that jumps to an element with
id="faq". - Write a link that starts an email to
info@school.edu.pk.
Practice Answers
href<a href="https://www.bbc.com">BBC News</a><a href="../index.html">Home</a>- Add
target="_blank" rel="noopener". <a href="#faq">Questions</a><a href="mailto:info@school.edu.pk">Email the school</a>
Key Takeaways
<a href="...">text</a>makes a link; make the text describe the destination.- Full URLs (with
https://) for other sites; relative paths for your own pages. #idjumps within a page;mailto:andtel:open email and phone.- New tabs:
target="_blank" rel="noopener", used sparingly.