Lesson 10: Working with Links

Learning Outcomes

By the end of this lesson, you will understand how to create different types of links using the anchor tag. You will be able to link to external websites, other pages within your own site, and specific sections within a page. You will learn how to write accessible link text, combine images with links, and style links with CSS to create effective navigation. You will also understand link states and how to provide visual feedback to users.

Prerequisites

You should be comfortable with basic HTML structure from Lesson 01 and have some familiarity with CSS from Lesson 03. Understanding file paths from Lesson 09 (Working with Images) will also be helpful, as the same concepts apply to linking between pages.

The Anchor Tag and Basic Syntax

Links are created using the <a> tag, called the anchor tag. The anchor tag requires an href attribute (short for "hypertext reference") that specifies the destination of the link.

Basic Link Structure

A simple link looks like this:

<a href="https://www.example.com">Visit Example Website</a>

This link has three parts:

The href Attribute

The href attribute is required for links to function. It can contain:

What Happens When You Click a Link

When a user clicks a link, the browser navigates to the URL specified in the href attribute. This might:

Understanding how the href attribute works is fundamental to creating effective navigation throughout your website.

Worked Example: Building a Complete Navigation System

This example demonstrates how to create a complete, accessible navigation system that includes a header navigation bar, breadcrumbs, and in-page table of contents. We'll combine semantic HTML with CSS styling to create a professional navigation experience.

Step 1: Create the HTML Structure

Start by building a complete page structure with multiple navigation elements:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Services - My Website</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <!-- Main navigation -->
    <header>
        <nav class="main-nav" aria-label="Main navigation">
            <a href="index.html" class="logo">
                <img src="logo.png" alt="Company Name Homepage">
            </a>
            <ul>
                <li><a href="index.html">Home</a></li>
                <li><a href="about.html">About</a></li>
                <li><a href="services.html" class="current">Services</a></li>
                <li><a href="portfolio.html">Portfolio</a></li>
                <li><a href="contact.html">Contact</a></li>
            </ul>
        </nav>
    </header>

    <!-- Breadcrumb navigation -->
    <nav class="breadcrumb" aria-label="Breadcrumb">
        <ol>
            <li><a href="index.html">Home</a></li>
            <li>Services</li>
        </ol>
    </nav>

    <main>
        <h1>Our Services</h1>

        <!-- Table of contents -->
        <nav class="toc" aria-label="Table of contents">
            <h2>On This Page</h2>
            <ul>
                <li><a href="#web-design">Web Design</a></li>
                <li><a href="#development">Development</a></li>
                <li><a href="#seo">SEO Services</a></li>
                <li><a href="#maintenance">Maintenance</a></li>
            </ul>
        </nav>

        <section id="web-design">
            <h2>Web Design</h2>
            <p>Content about web design services...</p>
        </section>

        <section id="development">
            <h2>Development</h2>
            <p>Content about development services...</p>
        </section>

        <section id="seo">
            <h2>SEO Services</h2>
            <p>Content about SEO services...</p>
        </section>

        <section id="maintenance">
            <h2>Maintenance</h2>
            <p>Content about maintenance services...</p>
        </section>
    </main>
</body>
</html>

Step 2: Style the Main Navigation

Create a professional horizontal navigation bar:

.main-nav {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 2rem;
    background-color: #ffffff;
    border-bottom: 1px solid #e5e7eb;
}

.main-nav .logo img {
    height: 40px;
    display: block;
}

.main-nav ul {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    gap: 2rem;
}

.main-nav a {
    color: #1f2937;
    text-decoration: none;
    font-weight: 600;
    padding: 0.5rem 1rem;
    border-radius: 4px;
    transition: all 0.3s ease;
}

.main-nav a:hover,
.main-nav a:focus {
    background-color: #f3f4f6;
    color: #2563eb;
}

.main-nav a.current {
    background-color: #2563eb;
    color: white;
}

Step 3: Style the Breadcrumb Navigation

.breadcrumb {
    padding: 1rem 2rem;
    background-color: #f9fafb;
}

.breadcrumb ol {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    gap: 0.5rem;
    font-size: 0.875rem;
}

.breadcrumb li {
    color: #6b7280;
}

.breadcrumb a {
    color: #2563eb;
    text-decoration: none;
}

.breadcrumb a:hover {
    text-decoration: underline;
}

.breadcrumb li:not(:last-child)::after {
    content: "›";
    margin-left: 0.5rem;
    color: #9ca3af;
}

Step 4: Style the Table of Contents

.toc {
    background-color: #f3f4f6;
    border-left: 4px solid #2563eb;
    padding: 1.5rem;
    margin: 2rem 0;
}

.toc h2 {
    margin-top: 0;
    font-size: 1.125rem;
    color: #1f2937;
}

.toc ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

.toc li {
    margin: 0.5rem 0;
}

.toc a {
    color: #2563eb;
    text-decoration: none;
    display: block;
    padding: 0.25rem 0;
}

.toc a:hover,
.toc a:focus {
    text-decoration: underline;
    padding-left: 0.5rem;
    transition: padding-left 0.2s ease;
}

Step 5: Add Responsive Behavior

Make the navigation mobile-friendly:

@media (max-width: 768px) {
    .main-nav {
        flex-direction: column;
        align-items: flex-start;
    }

    .main-nav ul {
        flex-direction: column;
        width: 100%;
        gap: 0;
        margin-top: 1rem;
    }

    .main-nav a {
        display: block;
        width: 100%;
        padding: 0.75rem 1rem;
        border-bottom: 1px solid #e5e7eb;
    }
}

What This Example Demonstrates

This navigation system showcases several important concepts:

This pattern can be adapted for most websites, providing users with multiple ways to navigate your content.

Practice Opportunity

Create a multi-page website (at least 3 pages) with a complete navigation system that includes:

Bonus Challenges

Testing Your Work

After creating your navigation:

Summary of Key Concepts

Links are the fundamental technology that connects the web together, allowing users to navigate between pages and discover content. The anchor tag (<a>) with the href attribute creates links to external websites, internal pages, or specific sections within a page.

What You've Learned

Effective link text is descriptive and tells users where the link leads. Avoid generic phrases like "click here" in favor of specific descriptions. When images are used as links, the alt text should describe the link's destination or purpose rather than the image itself.

Links have multiple states (link, visited, hover, active, focus) that can be styled with CSS pseudo-classes. Always provide visible focus indicators for keyboard users and maintain sufficient color contrast to ensure links are distinguishable from regular text. The proper order for styling link states is LVHA: Link, Visited, Hover, Active.

Navigation menus are best marked up using the semantic <nav> element containing lists of links. Different navigation types serve different purposes: main navigation helps users move between pages, breadcrumbs show location in site hierarchy, and tables of contents help users navigate within long pages.

Remember These Key Points

Bringing It All Together

With knowledge of both images (Lesson 09) and links, you can now create rich, interactive web pages with clickable images, image galleries, and complete navigation systems. These two lessons provide the foundation for building websites that are both visually appealing and easy to navigate.

Bibliography

Mozilla Developer Network. "The Anchor element." MDN Web Docs. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a

WebAIM. "Links and Hypertext." WebAIM. https://webaim.org/techniques/hypertext/

W3C Web Accessibility Initiative. "Link Purpose (In Context)." Understanding WCAG 2.1. https://www.w3.org/WAI/WCAG21/Understanding/link-purpose-in-context.html

Mozilla Developer Network. "CSS :link pseudo-class." MDN Web Docs. https://developer.mozilla.org/en-US/docs/Web/CSS/:link