Find a course

Module 2: Writing HTML ContentLesson 4 of 1211 min

Images, Media and Tables

Add images with meaningful alt text and the right file format, embed audio and video, and build accessible tables for data with headers and captions.

Step 1 of 14Learn

Images and alt text

Images are added with the void element img:

<img src="images/storefront.jpg" alt="Our bakery's blue storefront on Main Street" width="800" height="533">
  • src is the path to the image file, relative or absolute, just like a link.
  • alt is a text alternative. Screen readers read it aloud, and browsers show it if the image fails to load.
  • width and height tell the browser the image's proportions before it downloads, so the page doesn't jump around as images appear. CSS can still resize the image.

Writing good alt text is a skill. Describe what the image communicates in context, briefly:

  • A product photo: "Red waterproof hiking boot, side view."
  • A chart: summarize its point, "Sales doubled between 2023 and 2025," and give the full data in text or a table nearby.
  • A purely decorative image (a swirl or background flourish): use an empty alt, alt="", so screen readers skip it. Don't leave alt out entirely, or some screen readers will read the file name instead.

Avoid starting with "Image of..." because screen readers already announce that it's an image. And never put important text only inside an image: it can't be read by screen readers, translated or found by search engines.

Step 2 of 14Practice · What would you do?

What would you do?

A charity's home page has a large photo of volunteers planting trees, with the heading "Join our spring planting day" in normal text below it. What is the best alt text for the photo?

Step 3 of 14Diagram

Writing alt text: a decision tree

Writing alt text: a decision treeHierarchy diagram: An image on your page, Purely decorative?, Carries information?, Contains text, Is a link or button, A photo or illustration.An image on yourpagePurelydecorative?Carriesinformation?Contains textIs a link orbuttonA photo orillustration

Tap a shape with to see more.

An image on your page
Purely decorative?

Then use alt="" so screen readers skip it.

Carries information?
Contains text

Put that text in the alt.

Is a link or button

Describe where it goes or what it does.

A photo or illustration

Describe what matters in this context, in a short sentence.

Step 4 of 14Learn

Choosing image formats

Image files are often the heaviest part of a web page, so the format and size you choose affect how fast your page loads, especially on phones.

FormatBest forNotes
JPEGPhotosLossy compression; small files; no transparency
PNGScreenshots, graphics with sharp edges or transparencyLossless; larger files for photos
WebP / AVIFPhotos and graphicsModern formats; usually much smaller than JPEG or PNG at similar quality; supported by current browsers
SVGLogos, icons, simple illustrationsVector format made of shapes; sharp at any size; tiny files for simple graphics
GIFVery simple animationsOld format; video is usually a better choice for animation

Two practical rules:

  1. Resize before uploading. A photo straight from a phone might be 4,000 pixels wide and 5 MB. If it displays at 800 pixels wide, resize it to about that size (or twice that for sharp high-density screens). The file can shrink to a tenth of its size.
  2. Compress. Free tools reduce file size with little visible change.

For images that sit below the first screen, adding loading="lazy" tells the browser to wait until the user scrolls near them, so the top of the page appears sooner.

Step 5 of 14Practice · Match the pairs

Match each image with the most suitable format.

Tap an answer, then tap the place where it belongs (or drag it there).

  1. A company logo that must look sharp at any size
  2. A product photo for a site with older tools that only accept common formats
  3. A screenshot of software with small text
  4. A photo where you want the smallest file at similar quality in modern browsers

Step 6 of 14Compare

Image formats at a glance

AspectJPEGPNGWebPSVG
Best forPhotosScreenshots, images needing sharp edges or transparencyPhotos and graphics, smaller filesLogos, icons, simple illustrations
TransparencyNoYesYesYes
ScalingPixels: blurs if enlargedPixelsPixelsVector: sharp at any size
File sizeSmall for photosLarge for photosUsually smallestTiny for simple shapes

Step 7 of 14Note

Step 8 of 14Practice · Work it out

A photo is 4,000 × 3,000 pixels but displays at 800 pixels wide on your page. If you resize it to 800 pixels wide keeping its proportions, how many pixels tall will it be?

Step 9 of 14Practice · Chart

Pixels in the photo before and after resizing

Shrinking width and height by 5 each cuts the pixel count by 25. Browsers download every pixel, even ones they then throw away by scaling down.

Tap the version that loads faster on a phone.

Tap the bar that answers the question, then press Check.

Pixels in the photo before and after resizingBar chart with 2 categories: Original 4,000 × 3,000: 12 MP; Resized 800 × 600: 0.48 MP.0 MP2.5 MP5 MP7.5 MP10 MP12.5 MPOriginal 4,000 × 3,000Resized 800 × 60012 MP0.48 MP
Pixels in the photo before and after resizing
ItemMegapixels
Original 4,000 × 3,00012 MP
Resized 800 × 6000.48 MP

Step 10 of 14Note

Step 11 of 14Learn

Tables are for data, not layout

A table shows data in rows and columns: a price comparison, a timetable, sports results. Here's an accessible table:

<table>
  <caption>Membership plans</caption>
  <thead>
    <tr><th scope="col">Plan</th><th scope="col">Monthly price</th><th scope="col">Classes</th></tr>
  </thead>
  <tbody>
    <tr><th scope="row">Basic</th><td>$20</td><td>4</td></tr>
    <tr><th scope="row">Unlimited</th><td>$45</td><td>Unlimited</td></tr>
  </tbody>
</table>

The parts:

  • table wraps everything.
  • caption gives the table a title, which screen readers announce first.
  • tr is a table row.
  • th is a header cell; td is a regular data cell.
  • scope="col" or scope="row" tells assistive technology whether a header labels a column or a row, so a screen reader can say "Unlimited, Monthly price, $45."
  • thead and tbody group header and body rows.

In the early 2000s, developers used tables to lay out entire pages. Don't do that: layout tables confuse screen readers, break on small screens and are hard to maintain. Use tables only for tabular data, and use CSS (Flexbox and Grid, coming later) for layout.

Step 12 of 14Practice · Put in order

Put these table elements in order from the outermost to the innermost.

Drag the items, or use the arrow buttons, to put them in order.

  1. table
  2. tr
  3. tbody
  4. td

Step 13 of 14Diagram

The structure of an accessible data table

The structure of an accessible data tableHierarchy diagram: table, caption, thead, tbody, tr, th scope="col", tr, td.tablecaptiontheadtbodytrth scope="col"trtd

Tap a shape with to see more.

table
caption

A visible title that says what the table shows.

thead
tbody
tr
th scope="col"

Header cells tell screen readers what each column means.

tr
td

Ordinary data cells.

Step 14 of 14Practice · Select all that apply

Which practices make a data table more accessible? Select all that apply.

Select all that apply.