Media

Pictures, video, sound and embedded pages, each in a figure with its caption beneath. It is the same unit as a code block: centred, no wider than the code, with rounded corners. No class is needed, and every piece keeps its own shape as the page narrows.

An image

An img in a figure fills the figure's width and keeps its shape. Give it width and height so the browser saves the right space before it loads, and the page does not jump. Add loading="lazy" to an image far down a page so it loads only as the reader nears it.

The Stylesheet.css banner: the sun logo and the name on yellow
The Stylesheet.css banner
<figure>
  <img src="og-image.png" alt="The Stylesheet.css banner: the sun logo and the name on yellow"
       width="1200" height="630" loading="lazy">
  <figcaption>The Stylesheet.css banner</figcaption>
</figure>
An image with its size, loaded when needed

A picture for each screen

picture lets the browser choose between versions of an image. Each source offers one, and the img inside is the fallback that holds the alt text and the size. Here a narrow screen gets a different picture: make the window narrower than 600 pixels to see it swap.

A sample picture that changes with the width of the screen
The banner on a wide screen, a test pattern on a narrow one
<figure>
  <picture>
    <source media="(max-width: 600px)" srcset="poster.jpg" width="640" height="360">
    <img src="og-image.png" alt="A sample picture that changes with the width of the screen"
         width="1200" height="630">
  </picture>
  <figcaption>The banner on a wide screen, a test pattern on a narrow one</figcaption>
</figure>
A different picture below 600 pixels

A source can also offer a newer, smaller format, such as type="image/avif", for browsers that read it, with the img as the version every browser can show.

Video

video with controls gives the browser's own player, which works with the keyboard and a screen reader. poster is the still shown before it plays, and preload="metadata" fetches only the length and size until the reader presses play. A track adds captions.

A six-second test pattern with a tone, and captions
<figure>
  <video controls preload="metadata" poster="poster.jpg" width="640" height="360">
    <source src="clip.mp4" type="video/mp4">
    <track kind="captions" src="captions.vtt" srclang="en" label="English">
    <a href="clip.mp4">Download the clip</a>
  </video>
  <figcaption>A six-second test pattern with a tone, and captions</figcaption>
</figure>
A video with a poster, captions and a fallback link

The captions are a small text file of times and lines, in the WebVTT format.

WEBVTT

00:00:00.000 --> 00:00:03.000
A test pattern: colour bars and a moving stripe.

00:00:03.000 --> 00:00:06.000
[a steady beeping tone]
captions.vtt

Audio

audio with controls is the same player without a picture. It fills the figure's width.

A four-second beeping tone
<figure>
  <audio controls preload="metadata" src="tone.mp3">
    <a href="tone.mp3">Download the tone</a>
  </audio>
  <figcaption>A four-second beeping tone</figcaption>
</figure>
An audio player

A drawing

An svg written straight into the page scales to the figure's width and stays sharp at any size. role="img" and an aria-label make it one picture with one description, rather than a heap of shapes.

A simple bar drawing
<figure>
  <svg viewBox="0 0 640 200" role="img" aria-label="Three bars rising from left to right">
    <rect x="80" y="120" width="120" height="60" rx="8" fill="#ffe066"/>
    <rect x="260" y="80" width="120" height="100" rx="8" fill="#ffd21a"/>
    <rect x="440" y="30" width="120" height="150" rx="8" fill="#c25e0d"/>
  </svg>
  <figcaption>A simple bar drawing</figcaption>
</figure>
An inline drawing named for a screen reader

Another page

An iframe shows another page inside this one. It has no shape of its own, so in a figure it is given 16:9; set a different aspect-ratio inline when the page needs it. Every iframe needs a title, which is how a screen reader names it.

The Divider page, in a frame
<figure>
  <iframe src="/components/divider/" title="The Divider page" loading="lazy"
          style="aspect-ratio: 4 / 3"></iframe>
  <figcaption>The Divider page, in a frame</figcaption>
</figure>
A page in a frame, with a title

For a player from another site, such as YouTube, see Video embed.

Accessibility

alt
Every image says what it shows. An image that only decorates takes an empty alt="", so a screen reader skips it.
The caption
A figcaption is read with its figure, so it can add what the alt text leaves out without repeating it.
Captions
A video with speech or important sound needs a captions track.
No autoplay
Let the reader start the sound and the movement. Nothing on this page plays until it is pressed.
title
Every iframe has a title that says what is inside it.