TetSharing
HTML tutorial · 12 chapters
  1. Get started
  2. Elements and attributes
  3. Headings and paragraphs
  4. Text formatting
  5. Links
  6. Images
  7. Lists
  8. Tables
  9. Forms
  10. Input types and controls
  11. Semantic layout
  12. Head and metadata

Final HTML quiz

Chapter 6 of 12 · By Ethan Cole · Updated November 21, 2026

HTML Images: img, alt Text, Sizes and figure

The <img> element embeds a picture. It is a void element, so it has no closing tag; everything it needs is in its attributes.

The essential attributes

  • src is the image's URL. The example uses data: URLs that contain small SVG drawings inline, so it works without any files.
  • alt is the text alternative. Screen readers read it aloud, and browsers show it if the image fails to load.
  • width and height give the image's dimensions in pixels. Browsers use them to reserve space before the image downloads, so the text below does not jump when it arrives. That jump is called layout shift, and Google's Core Web Vitals measure it.

Writing alt text

Describe what the image communicates in context, not just what it shows. For a chart, the useful alt text is the data: "Bar chart: Python 22 chapters, JavaScript 19, SQL 14", not "chart". For a purely decorative image that adds no information, use an empty alt="", which tells screen readers to skip it. Never leave alt out altogether; some screen readers then read the file name.

HTML · editable

Expected page text

Chapters per track
Images are inline elements, so text can flow next to them.

The page text in the expected output contains only the caption and the paragraph. Alt text is not part of the page's visible text, which is exactly why it must stand on its own for people who cannot see the image.

figure and figcaption

<figure> groups self-contained content, such as an image, chart or code sample, with an optional <figcaption> that describes it. The caption is visible to everyone, while alt replaces the image for those who cannot see it. They often say different things.

Performance

  • loading="lazy" delays offscreen images until the user scrolls near them. Do not lazy-load the main image at the top of the page.
  • srcset and sizes offer several widths of the same image so phones download smaller files. The <picture> element adds modern formats like AVIF and WebP with a fallback.
  • Use SVG for logos and icons; it stays sharp at any size.

Common mistakes

  • Missing alt, or alt text such as "image" or "photo".
  • Leaving out width and height, causing layout shift.
  • Uploading a 4000-pixel photo to display at 400 pixels wide.

Summary

  • <img src alt width height> is the minimum for every image.
  • Alt text conveys the image's purpose; decorative images use alt="".
  • figure + figcaption add a visible caption.
  • Lazy-load offscreen images and serve sizes that match the layout.

Quiz: Images

1. What should a purely decorative image use for alt?

An empty alt tells assistive technology to ignore the image.

2. Why set width and height on images?

Known dimensions let the browser lay out the page before the image arrives.

3. Which element adds a visible caption to an image?

figcaption captions the content of its figure.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.