Module 4 of 12

📖 The Anchor Tag <a>

The <a> tag creates hyperlinks — the foundation of the World Wide Web. The href attribute specifies the destination.

Basic Link Syntax

<a href="https://ethioweb.net">Visit Ethioweb.net</a>

Essential Attributes

AttributePurposeExample
href The URL or destination (required) href="about.html"
target Where to open the link target="_blank" (new tab)
title Tooltip text on hover title="Learn more"
download Prompts file download download="file.pdf"

Opening Links in a New Tab

<a href="https://example.com" target="_blank" rel="noopener noreferrer"> Open in New Tab (Safe) </a>
⚠️ Security Best Practice: Always add rel="noopener noreferrer" when using target="_blank". This prevents the new page from accessing your page via JavaScript (tabnabbing protection).

🔗 Absolute vs. Relative URLs

Understanding URLs is crucial for building multi-page websites.

Absolute URLs

Complete addresses that work from anywhere. Use these for external websites.

<a href="https://ethioweb.net/courses/html">Full URL</a> <a href="https://ethioweb.net">Protocol + Domain</a>

Relative URLs

Paths relative to the current file's location. Use these for internal pages.

<!-- Same folder --> <a href="about.html">About Page</a> <!-- Subfolder --> <a href="pages/contact.html">Contact Page</a> <!-- Parent folder --> <a href="../index.html">Go Up One Level</a> <!-- Root-relative (starts from website root) --> <a href="/images/logo.png">From Root</a>

URL Path Cheat Sheet

PathMeaning
page.htmlFile in the same folder
folder/page.htmlFile inside a subfolder
../page.htmlFile in the parent folder
../../page.htmlFile two levels up
/page.htmlFile at the website root

📍 Internal Page Navigation (Anchor Links)

You can link to specific sections within the same page using fragment identifiers (IDs).

How It Works

  1. Add an id attribute to the target element
  2. Link to it using #id-name
<!-- The navigation links --> <nav> <a href="#about">About</a> <a href="#services">Services</a> <a href="#contact">Contact</a> </nav> <!-- The target sections --> <h2 id="about">About Us</h2> <p>Content about the company...</p> <h2 id="services">Our Services</h2> <p>Content about services...</p> <h2 id="contact">Contact Us</h2> <p>Contact information...</p>
💡 Smooth Scrolling: You can add smooth scrolling with CSS: html { scroll-behavior: smooth; }

Linking to Page Sections from Other Pages

<!-- Link to the #services section of about.html --> <a href="about.html#services">See Our Services</a>

✏️ Exercise 4.1: Build a Multi-Page Website

Your Task

Create a simple 3-page personal website with navigation:

  1. Create a folder called my-website
  2. Create index.html (Homepage) with:
    • Your name as <h1>
    • A welcome paragraph
    • Navigation links to all three pages
  3. Create about.html with:
    • "About Me" as <h1>
    • 2-3 paragraphs about yourself
    • Navigation links to all three pages
  4. Create contact.html with:
    • "Contact" as <h1>
    • Your email as a clickable mailto: link
    • A link to Ethioweb.net that opens in a new tab (with security attributes)
    • Navigation links to all three pages

Email Links

<a href="mailto:yourname@example.com">Send me an email</a>

📝 Module 4 Quiz

Test Your Knowledge
1. Which attribute specifies the link destination?
2. How do you open a link in a new tab?
3. What does ../ mean in a relative path?
4. How do you link to a section with id="contact" on the same page?
5. Which rel attribute should you use with target="_blank"?

🎯 Module Summary

← Previous Module Next: Module 5 →