On this page12 sections
  1. Learning Objectives
  2. 1. The Anchor Element
  3. 2. Absolute vs Relative Links
  4. 3. Opening in a New Tab
  5. 4. Jumping to Part of a Page
  6. 5. Email and Phone Links
  7. 6. A Picture as a Link
  8. 7. Link Colours
  9. Common Beginner Mistakes
  10. Practice Questions
  11. Practice Answers
  12. Key Takeaways

Lecture 010: Links

Category3: Links, Images, Tables & Lists
Lecture010 of 048
Estimated video length12 min
TargetHTML5 · any modern browser
LevelBeginner

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

html
<a href="https://www.wikipedia.org">Open Wikipedia</a>
  • <a> stands for anchor.
  • href is the destination.
  • The text between the tags is what people click, so make it describe where it goes.

An absolute URL is a full address, used for other websites:

output
https://www.example.com/blog/post.html

A relative path points to a file on your own site, starting from the current page:

LinkGoes 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

html
<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

html
<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>

Example

html
<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.


Anything inside <a> becomes clickable, including an image.

Example

html
<a href="https://www.wikipedia.org">
  <img src="https://picsum.photos/id/24/240/140" alt="Open book: visit Wikipedia">
</a>

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

html
<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.

html
<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

  1. Which attribute holds a link's destination?
  2. Write a link to https://www.bbc.com with the text "BBC News".
  3. Your page is in /blog/. Write a link to index.html in the folder above.
  4. How do you make a link open in a new tab safely?
  5. Write a link that jumps to an element with id="faq".
  6. Write a link that starts an email to info@school.edu.pk.

Practice Answers

  1. href
  2. <a href="https://www.bbc.com">BBC News</a>
  3. <a href="../index.html">Home</a>
  4. Add target="_blank" rel="noopener".
  5. <a href="#faq">Questions</a>
  6. <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.
  • #id jumps within a page; mailto: and tel: open email and phone.
  • New tabs: target="_blank" rel="noopener", used sparingly.