Module 5 of 12

📖 The <img> Tag

The <img> tag embeds images into your web page. Unlike most HTML tags, it is a self-closing tag (it has no closing tag).

Basic Syntax

<img src="photo.jpg" alt="A beautiful sunset over the mountains">

Essential Attributes

AttributeRequired?Purpose
src Yes The image file path or URL
alt Yes (for accessibility) Text description for screen readers and when image fails to load
width No Image width in pixels
height No Image height in pixels
loading No lazy delays loading until image is near viewport
⚠️ Accessibility Law: The alt attribute is not optional for accessible websites. In many countries, missing alt text violates accessibility laws (like ADA in the US). Always describe the image's purpose, not just its appearance.

Good vs. Bad Alt Text

ImageBad Alt TextGood Alt Text
Company logo "Logo" "Ethioweb.net - Web Development Solutions"
Chart showing sales growth "Graph" "Bar chart showing 50% sales growth from 2024 to 2025"
Decorative border "Blue line" "" (empty alt for decorative images)
💡 Lazy Loading: Add loading="lazy" to images below the fold (not visible when page first loads). This makes your page load faster: <img src="photo.jpg" alt="..." loading="lazy">

🖼️ Image Formats Explained

Choosing the right image format affects page speed and quality.

FormatBest ForTransparency?Animation?
JPEG / JPG Photographs, complex images No No
PNG Graphics, logos, screenshots Yes No
SVG Logos, icons, illustrations Yes Yes (via CSS/JS)
WebP Modern replacement for JPEG/PNG Yes Yes
GIF Simple animations (avoid for photos) Yes (1-bit) Yes
💡 Modern Best Practice: Use WebP when possible (30% smaller than JPEG without quality loss). Provide JPEG/PNG fallback for older browsers using the <picture> element.

🎨 Figure and Figcaption

HTML5 introduced <figure> and <figcaption> for self-contained content with captions.

<figure> <img src="ethiopian-coffee.jpg" alt="Traditional Ethiopian coffee ceremony with jebena pot" width="600"> <figcaption> Traditional Ethiopian coffee ceremony, an essential part of Ethiopian culture. </figcaption> </figure>

When to Use <figure>

🎬 Video and Audio

HTML5 allows native video and audio playback without plugins like Flash.

The <video> Tag

<video width="640" height="360" controls poster="thumbnail.jpg"> <source src="video.mp4" type="video/mp4"> <source src="video.webm" type="video/webm"> <p>Your browser does not support HTML5 video. <a href="video.mp4">Download the video</a> instead. </p> </video>

Video Attributes

AttributeEffect
controlsShows play, pause, volume buttons
autoplayStarts playing automatically (often blocked by browsers)
loopRepeats when finished
mutedStarts with sound off
posterImage shown before video plays

The <audio> Tag

<audio controls> <source src="music.mp3" type="audio/mpeg"> <source src="music.ogg" type="audio/ogg"> <p>Your browser does not support audio playback.</p> </audio>
⚠️ Accessibility: Always provide transcripts or captions for video content. Deaf and hard-of-hearing users cannot access audio information otherwise.

✏️ Exercise 5.1: Create a Photo Gallery

Your Task

Create gallery.html with the following:

  1. A page title "My Photo Gallery"
  2. At least 4 images using <figure> and <figcaption>
  3. Each image must have descriptive alt text
  4. Use placeholder images from picsum.photos:
    • https://picsum.photos/400/300?random=1
    • https://picsum.photos/400/300?random=2
    • Change the number for different images
  5. Add loading="lazy" to images below the first two
  6. Include a link back to your homepage

📝 Module 5 Quiz

Test Your Knowledge
1. Which attribute is required for accessibility in <img>?
2. What does loading="lazy" do?
3. Which format is best for photographs?
4. What tag wraps an image with its caption?
5. Which attribute shows video controls?

🎯 Module Summary

← Previous Module Next: Module 6 →