Skip to main content

🔗 Lesson 6: Links & Images

Links and images are what make the web a web. Links connect pages to each other (and to the rest of the internet), while images bring your content to life visually. Together, they transform a plain text document into a real website.

🎯 Learning Objectives

By the end of this lesson, you will be able to:

  • Create hyperlinks to other pages and external websites with <a>
  • Understand the difference between absolute and relative URLs
  • Open links in new tabs with the target attribute
  • Display images with <img> and write meaningful alt text
  • Link between pages in your own site

Estimated Time: 45 minutes

Hands-on: You'll add navigation links and images to your website project.

📑 In This Lesson

Absolute vs. Relative URLs

There are two ways to write a URL in the href attribute, and understanding the difference is critical:

Absolute URLs

An absolute URL includes the full address — protocol, domain, and path. It points to the same place no matter where your page is located:

<a href="https://www.wikipedia.org">Wikipedia</a>
<a href="https://developer.mozilla.org/en-US/docs/Web/HTML">MDN HTML Docs</a>

Use absolute URLs when linking to external websites — sites that aren't yours.

Relative URLs

A relative URL describes the path relative to the current page. It doesn't include a domain name:

<!-- Links to about.html in the same folder -->
<a href="about.html">About Me</a>

<!-- Links to a file inside a subfolder -->
<a href="pages/contact.html">Contact</a>

<!-- Goes up one folder, then into images -->
<a href="../images/photo.jpg">View Photo</a>

Use relative URLs when linking between pages within your own site. They're shorter, easier to manage, and they keep working even if you move your entire site to a different domain.

graph TD A["my-website/"] --> B["index.html"] A --> C["about.html"] A --> D["pages/"] D --> E["contact.html"] A --> F["images/"] F --> G["photo.jpg"] B -- "href='about.html'" --> C B -- "href='pages/contact.html'" --> E E -- "href='../about.html'" --> C style A fill:#eff6ff,stroke:#3b82f6,stroke-width:2px,color:#1e293b style B fill:#f0fdf4,stroke:#22c55e,stroke-width:1px,color:#1e293b style C fill:#f0fdf4,stroke:#22c55e,stroke-width:1px,color:#1e293b style D fill:#fdf4ff,stroke:#a855f7,stroke-width:1px,color:#1e293b style E fill:#f0fdf4,stroke:#22c55e,stroke-width:1px,color:#1e293b style F fill:#fdf4ff,stroke:#a855f7,stroke-width:1px,color:#1e293b style G fill:#fffbeb,stroke:#f59e0b,stroke-width:1px,color:#1e293b

💡 The .. Shortcut

In a file path, .. means "go up one directory." If you're in pages/contact.html and want to link to about.html (which is in the parent folder), you'd write href="../about.html". A single dot (.) means "this current directory."

Displaying Images with <img>

The <img> element embeds an image on the page. It's a void element (no closing tag), and it requires two attributes:

<img src="images/sunset.jpg" alt="A golden sunset over the ocean">
  • src — the source (path or URL to the image file)
  • altalternative text (describes the image for screen readers and for when the image fails to load)

Image Paths

Just like links, image sources can be absolute or relative:

<!-- Relative: image in an "images" subfolder -->
<img src="images/photo.jpg" alt="My photo">

<!-- Relative: image in the same folder as the HTML file -->
<img src="logo.png" alt="Site logo">

<!-- Absolute: image hosted on another site -->
<img src="https://via.placeholder.com/300x200" alt="Placeholder image">

For your own projects, keep images in a dedicated images/ folder to stay organized.

Setting Image Dimensions

You can specify width and height using attributes (in pixels):

<img src="images/sunset.jpg"
     alt="A golden sunset over the ocean"
     width="600"
     height="400">

💡 Why Set Width and Height?

When you include width and height attributes, the browser reserves that space on the page before the image loads. This prevents the annoying "layout shift" where content jumps around as images pop in. You'll refine sizing with CSS later, but the HTML attributes help the browser plan the layout.

Common Image Formats

Format Extension Best For
JPEG .jpg / .jpeg Photographs, complex images with many colors
PNG .png Graphics with transparency, screenshots, logos
GIF .gif Simple animations, very small graphics
SVG .svg Icons, logos, illustrations (scales without losing quality)
WebP .webp Modern format with smaller file sizes (photos and graphics)

Writing Good Alt Text

The alt attribute is not optional. It serves three critical purposes:

  1. Accessibility: Screen readers read the alt text aloud so visually impaired users know what the image shows
  2. Fallback: If the image fails to load (broken link, slow connection), the alt text appears in its place
  3. SEO: Search engines can't "see" images — they rely on alt text to understand what's in them

Alt Text Guidelines

<!-- ❌ Bad: empty or unhelpful -->
<img src="photo.jpg" alt="">
<img src="photo.jpg" alt="image">
<img src="photo.jpg" alt="photo.jpg">

<!-- ❌ Bad: too long or redundant -->
<img src="photo.jpg" alt="Image of a photo showing a picture of a sunset">

<!-- ✅ Good: concise and descriptive -->
<img src="photo.jpg" alt="Golden sunset over the Pacific Ocean">

<!-- ✅ Good: describes the function for UI images -->
<img src="search-icon.png" alt="Search">

⚠️ When Alt Should Be Empty

If an image is purely decorative and adds no information — like a decorative border or background flourish — use an empty alt attribute: alt="". This tells screen readers to skip it entirely. But never omit the alt attribute altogether; that causes screen readers to read the filename, which is worse.

Hands-on Exercise

🏋️ Exercise: Connect Your Pages and Add an Image

Objective: Add navigation links between your pages and display an image.

Instructions:

  1. Create an images folder inside your my-website folder (if it doesn't exist yet)
  2. Find or download any image and save it in the images folder (you can use Unsplash for free photos)
  3. Open index.html and add a navigation section at the top of the <body>:
    • A link to about.html with the text "About Me"
    • A link to an external site (your favorite website) that opens in a new tab
  4. Below the navigation, add an image using <img> with descriptive alt text
  5. Open about.html and add a link back to index.html with the text "Back to Home"
  6. Save both files and test the links with Live Server
💡 Hint

Since index.html and about.html are in the same folder, the relative URL is just the filename: href="about.html". For images in the images subfolder, use src="images/filename.jpg".

✅ Example Solution — index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My First Website</title>
</head>
<body>
    <a href="about.html">About Me</a> |
    <a href="https://www.wikipedia.org"
       target="_blank"
       rel="noopener noreferrer">Wikipedia</a>

    <h1>Welcome to My Website</h1>

    <img src="images/sunset.jpg"
         alt="A golden sunset over the ocean"
         width="600" height="400">

    <p>This is my first properly structured website.
       I'm learning HTML and CSS from scratch!</p>

    <h2>What I'm Learning</h2>
    <ul>
        <li>HTML structure and tags</li>
        <li>Links and images</li>
        <li>How to connect pages together</li>
    </ul>
</body>
</html>
✅ Example Solution — about.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>About Me</title>
</head>
<body>
    <a href="index.html">Back to Home</a>

    <h1>About Alex Johnson</h1>
    <p>I'm a <strong>web development student</strong>
       learning to build websites from scratch.</p>

    <h2>My Interests</h2>
    <p>I enjoy drawing, board games, and
       <em>sourdough baking</em>.</p>

    <hr>

    <h2>Contact</h2>
    <p>
        <a href="mailto:alex@example.com">alex@example.com</a>
    </p>
</body>
</html>

🎯 Quick Quiz

Question 1: Which attribute specifies where a link goes?

Question 2: When should you use a relative URL instead of an absolute URL?

Question 3: What are the two required attributes for <img>?

Question 4: How do you open a link in a new tab?

Summary

🎉 Key Takeaways

  • The <a> element creates hyperlinks using the href attribute
  • Use absolute URLs (full address) for external sites and relative URLs (just the path) for your own pages
  • Use target="_blank" with rel="noopener noreferrer" to open external links in new tabs safely
  • Write descriptive link text — avoid "click here"
  • The <img> element displays images with src (source) and alt (alternative text)
  • Always include meaningful alt text for accessibility, SEO, and fallback display
  • Wrap <img> inside <a> to make clickable image links
  • Keep images organized in a dedicated images/ folder

📁 Your Project So Far

my-website/
├── images/
│   └── sunset.jpg     ← your first image
├── index.html         ← homepage with links and image
└── about.html         ← profile page with link back to home

🚀 What's Next?

Your pages have text, headings, links, and images — but content often needs to be organized into lists and tables. In the next lesson, you'll learn how to create ordered lists, unordered lists, definition lists, and structured data tables.