Chapter 4 Beginner 22 min min read Updated 2026-04-11

Links, Anchors, and Navigation

Practice Questions →

In This Chapter

What Is It?

What Are HTML Links?

Links are what make the web a web. Without them, the internet would just be a bunch of disconnected pages floating alone. Links are the threads that connect every page to every other page. Click a link and you teleport to another page, another website, another continent, or even back to the same page at a different section.

Every link in HTML is made with the <a> tag (the 'anchor' tag). The href attribute inside it tells the browser where to go when the user clicks.

<a href="https://www.google.com">Go to Google</a>

This is the simplest kind of link. The text between the opening and closing tags is what the user sees and clicks on.

Why 'Anchor'?

The tag is called 'anchor' because in the very first version of the web, links were used to 'anchor' you to a specific place in a document. Over time, they became the primary way to jump between pages. But the name stuck, so we still call it the anchor tag.

Why Does It Matter?

Why Learn Links Properly?

1. Navigation Is the Backbone of the Web

Every website has a menu, a footer, and buttons that take you somewhere. All of these are links. Without links, a website would be one long scrolling page with no way to get around.

2. Linking to Other Pages Is Powerful

You can send users to any web page in the world with a single link. Share your favourite YouTube video, your project on GitHub, or your photo on Instagram — all with one <a> tag.

3. SEO Loves Good Links

Google uses links to understand the relationship between pages. If many websites link to your page, Google treats your page as important. Internal links between your own pages also help Google crawl your site.

4. Email and Phone Links

You can make email addresses and phone numbers clickable. On a phone, clicking a tel link dials the number. Clicking an email link opens the user's email app with the address pre-filled. Super handy for contact pages.

5. Anchor Links for Long Pages

If your page is long (like a FAQ or a tutorial), you can link to specific sections. Users click a table of contents link and jump directly to that section without scrolling. This is how Wikipedia's 'Contents' box works.

6. Professional Habits

Knowing when to use target="_blank", when to add rel="noopener noreferrer", and how to structure navigation menus are basic skills every web developer needs. Get them right from day one.

Detailed Explanation

Detailed Explanation

The Basic Anchor Tag

A link has two parts: the <a> tag with an href attribute, and the text (or other content) between the opening and closing tags.

<a href="URL_HERE">CLICKABLE_TEXT_HERE</a>

Examples:

  • <a href="https://www.google.com">Google</a>
  • <a href="about.html">About Us</a>
  • <a href="#top">Back to top</a>

Internal Links (Relative Paths)

An internal link goes to another page within the same website. You use a relative path — a path relative to the current HTML file.

PathMeaning
about.htmlA file in the same folder
./about.htmlSame thing (explicit 'same folder')
../index.htmlGo up one folder, then find index.html
pages/contact.htmlGo into 'pages' folder, find contact.html
../../home.htmlGo up two folders, find home.html

External Links (Absolute URLs)

An external link goes to another website. You use a complete URL starting with https:// or http://.

<a href="https://www.wikipedia.org">Wikipedia</a>

Always prefer https:// over http:// — it is the secure version.

target="_blank" — Opening Links in a New Tab

By default, clicking a link replaces the current page. To open the link in a new browser tab (so users can come back to your page), use target="_blank":

<a href="https://www.google.com" target="_blank">Google</a>

However, when you use target="_blank", there is a security and performance issue. The new tab gets access to the opener tab through the window.opener object, and some browsers may tie both tabs to the same process. To fix this, always add rel="noopener noreferrer":

<a href="https://www.google.com" target="_blank" rel="noopener noreferrer">Google</a>

noopener — prevents the new page from using window.opener to control the original page.
noreferrer — does not send the referring URL to the new page (adds extra privacy).

Anchor Links (Same-Page Navigation)

You can link to a specific section within the same page using an ID. First, give the destination element an id:

<h2 id="chapter-3">Chapter 3</h2>

Then link to it with # followed by the ID:

<a href="#chapter-3">Go to Chapter 3</a>

When clicked, the browser scrolls down to that heading. You can also link to an ID on a different page with page.html#chapter-3.

mailto: Links

A mailto: link opens the user's default email app with the address pre-filled:

<a href="mailto:priya@example.com">Email Priya</a>

You can even pre-fill the subject and body:

<a href="mailto:priya@example.com?subject=Hello&body=Hi%20Priya">Email Priya</a>

(Note: spaces in the URL need to be encoded as %20.)

tel: Links

A tel: link opens the phone dialler on mobile devices (on desktop it may not do much):

<a href="tel:+919876543210">Call Us</a>

Always include the country code starting with a plus sign. This makes the link work from any country.

The download Attribute

Adding the download attribute to a link makes the browser download the file instead of opening it:

<a href="resume.pdf" download>Download Resume</a>

You can also rename the downloaded file:

<a href="resume.pdf" download="Priya_Resume.pdf">Download</a>

The title Attribute

The title attribute creates a tooltip that shows when users hover over the link:

<a href="about.html" title="Learn more about us">About</a>

Do not rely on this for important information — mobile users cannot see tooltips.

Building a Navigation Menu

A navigation menu is just a list of links, usually inside a <nav> element:

<nav>
  <a href="index.html">Home</a>
  <a href="about.html">About</a>
  <a href="contact.html">Contact</a>
</nav>

The <nav> tag is a semantic element that tells browsers and screen readers 'this is the main navigation'. CSS (which you will learn later) makes the menu look pretty.

Code Examples

Basic Internal and External Links
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>My Links</title>
</head>
<body>
  <h1>Priya's Favourite Links</h1>
  <h2>Internal Links (Same Site)</h2>
  <p><a href="about.html">About Me</a></p>
  <p><a href="projects.html">My Projects</a></p>
  <p><a href="contact.html">Contact</a></p>
  <h2>External Links (Other Websites)</h2>
  <p><a href="https://www.google.com">Google</a></p>
  <p><a href="https://www.wikipedia.org">Wikipedia</a></p>
  <p><a href="https://learn.modernagecoders.com">Modern Age Coders</a></p>
</body>
</html>
This shows both kinds of links. Internal links use relative paths like about.html — they point to other files in the same folder. External links use full URLs starting with https://. Both types of links use the same <a> tag.
A page with two sections of links. Clicking internal links would open pages in the same folder. Clicking external links opens websites like Google and Wikipedia.
Opening Links in a New Tab
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>External Resources</title>
</head>
<body>
  <h1>Helpful External Resources</h1>
  <p>These open in a new tab so you do not lose this page:</p>
  <p><a href="https://www.wikipedia.org" target="_blank" rel="noopener noreferrer">Wikipedia (new tab)</a></p>
  <p><a href="https://www.github.com" target="_blank" rel="noopener noreferrer">GitHub (new tab)</a></p>
  <p><a href="https://www.stackoverflow.com" target="_blank" rel="noopener noreferrer">Stack Overflow (new tab)</a></p>
</body>
</html>
When you add target="_blank", the link opens in a new browser tab. For security and privacy, always add rel="noopener noreferrer" along with it. This is the standard for external links.
A page with three links that each open in a new browser tab when clicked.
Anchor Links Within the Same Page
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>HTML Tutorial</title>
</head>
<body>
  <h1>HTML Tutorial</h1>

  <h2>Table of Contents</h2>
  <ul>
    <li><a href="#intro">Introduction</a></li>
    <li><a href="#tags">Tags</a></li>
    <li><a href="#attributes">Attributes</a></li>
    <li><a href="#conclusion">Conclusion</a></li>
  </ul>

  <h2 id="intro">Introduction</h2>
  <p>HTML stands for HyperText Markup Language. It is used to build web pages.</p>

  <h2 id="tags">Tags</h2>
  <p>Tags are the building blocks of HTML. Examples include p, h1, and img.</p>

  <h2 id="attributes">Attributes</h2>
  <p>Attributes add extra information to tags, like href on an anchor tag.</p>

  <h2 id="conclusion">Conclusion</h2>
  <p>HTML is easy to learn and the foundation of the web.</p>
  <p><a href="#">Back to Top</a></p>
</body>
</html>
This shows anchor navigation. Each section heading has an id attribute. The table of contents at the top links to those IDs using #. Clicking a link smoothly scrolls to that section. The 'Back to Top' link at the bottom uses just # to go to the top of the page.
A tutorial page with a clickable table of contents. Clicking any entry jumps to that section of the page.
Email, Phone, and Download Links
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Contact Rohan</title>
</head>
<body>
  <h1>Contact Rohan Gupta</h1>
  <p>Choose your preferred way to contact:</p>
  <ul>
    <li>Email: <a href="mailto:rohan@example.com">rohan@example.com</a></li>
    <li>Phone: <a href="tel:+919876543210">+91 98765 43210</a></li>
    <li><a href="https://via.placeholder.com/150" download="Rohan_Avatar.png">Download my avatar</a></li>
  </ul>
  <h2>Email with Subject and Body</h2>
  <p><a href="mailto:rohan@example.com?subject=Hello%20Rohan&body=Hi%20Rohan%2C%20I%20saw%20your%20website.">Email with pre-filled message</a></p>
</body>
</html>
Three special kinds of links: mailto: opens the email app, tel: dials the number on mobile, and download makes a file download instead of opening it. The mailto link can also pre-fill subject and body using query parameters. Note that spaces must be encoded as %20.
A contact page with clickable email and phone links, a download button, and a pre-filled email link.
Navigation Menu Example
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Ananya's Site</title>
</head>
<body>
  <nav>
    <a href="index.html">Home</a> |
    <a href="about.html">About</a> |
    <a href="projects.html">Projects</a> |
    <a href="blog.html">Blog</a> |
    <a href="contact.html">Contact</a>
  </nav>

  <h1>Welcome to Ananya's Site</h1>
  <p>Use the navigation menu above to explore different pages.</p>
</body>
</html>
This is a simple horizontal navigation menu using the <nav> semantic element. The pipe characters (|) are used as simple separators between links. In a real project, CSS would make this look much prettier, but this is a functional navigation menu using only HTML.
A page with a top navigation menu showing 'Home | About | Projects | Blog | Contact', each as a clickable link.
Link With Title Tooltip and Relative Paths
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Paths Example</title>
</head>
<body>
  <h1>Different Relative Paths</h1>
  <h2>Links with Tooltips</h2>
  <p><a href="about.html" title="Learn more about Vikram">About Me</a></p>
  <p><a href="projects.html" title="See my coding projects">Projects</a></p>

  <h2>Links to Other Folders</h2>
  <p><a href="pages/gallery.html">Gallery (in pages folder)</a></p>
  <p><a href="../parent-page.html">Parent Page (go up one folder)</a></p>
  <p><a href="./same-folder.html">Same Folder (explicit)</a></p>
</body>
</html>
The title attribute creates a tooltip that appears when users hover over the link. Different path prefixes point to different locations: ./ is the current folder, ../ goes up one folder, and just a filename means the same folder. Hover over the first two links and you will see a small yellow tooltip.
A page with tooltips when hovering over links, and several examples of relative paths.

Common Mistakes

Forgetting the href Attribute

<a>Click Me</a>
The link is not clickable — it just appears as plain text (or blue underlined text that does nothing).
<a href="https://www.google.com">Click Me</a>
Every <a> tag needs an href attribute to be a functioning link. Without it, the tag may display as styled text, but clicking does nothing.

Using target="_blank" Without rel="noopener noreferrer"

<a href="https://example.com" target="_blank">Example</a>
Security and privacy vulnerability — the new tab can access window.opener and the referring URL is shared.
<a href="https://example.com" target="_blank" rel="noopener noreferrer">Example</a>
Always add rel="noopener noreferrer" when using target="_blank". noopener prevents the new page from accessing the original via window.opener. noreferrer prevents sending the referring URL.

Forgetting https:// for External Links

<a href="www.google.com">Google</a>
The browser treats 'www.google.com' as a relative path inside your site, not as an external URL. Clicking goes to yoursite.com/www.google.com.
<a href="https://www.google.com">Google</a>
External links must start with https:// (or http://). Without it, the browser thinks you mean a file on your own site. Always include the full protocol for external URLs.

Anchor Link With ID That Does Not Exist

<a href="#contact-section">Go to Contact</a>
<!-- But there is no element with id="contact-section" -->
Clicking the link does nothing — there is no target to scroll to.
<a href="#contact-section">Go to Contact</a>
<h2 id="contact-section">Contact</h2>
When using anchor links with #, you must have an element with a matching id attribute somewhere on the page. The ID in the href (after #) must match the id attribute exactly — case-sensitive.

Wrong mailto Syntax

<a href="mail:priya@example.com">Email</a>
The browser does not recognise 'mail:' as an email link, so clicking does nothing meaningful.
<a href="mailto:priya@example.com">Email</a>
The correct protocol for email links is mailto: (with 'to' at the end), not mail:. Similarly, phone links use tel:, not phone:.

Summary

  • The <a> (anchor) tag creates a link. The href attribute specifies where the link goes.
  • Internal links use relative paths like 'about.html' or '../pages/home.html' to point to other files in the same website.
  • External links use full URLs starting with 'https://' like 'https://www.google.com'.
  • target="_blank" opens the link in a new browser tab, keeping your original page open.
  • Always add rel="noopener noreferrer" when using target="_blank" for security and privacy.
  • Anchor links use #id to jump to a specific section on the same page. The target element must have a matching id attribute.
  • mailto: links open the user's email app with the address pre-filled. You can also pre-fill subject and body.
  • tel: links open the phone dialler on mobile devices. Include the country code starting with +.
  • The download attribute makes the browser download the file instead of opening it. You can also rename the file.
  • The title attribute creates a tooltip when users hover over the link — not visible on mobile, so do not rely on it for critical info.
  • Navigation menus are usually wrapped in the <nav> semantic element to tell browsers and screen readers 'this is navigation'.

Ready to Practice?

Test your understanding with 50+ practice questions on this topic.

Go to Practice Questions

Want to learn web development with a live mentor?

Explore our Frontend Masterclass