Images and Multimedia
Learn images, responsive images, audio, video, captions, and embedded content.
Images with img
The <img> element embeds an image resource. The src attribute identifies the image, while alt provides alternative text. Decorative images can use an empty alt="" so assistive technologies can ignore them.
Ask whether the image communicates information. If it does, describe that information; if it is purely decorative, an empty alt attribute is often appropriate.
<img src="developer.jpg" alt="Developer working at a laptop" width="800" height="500">- Meaningful images should have useful alternative text.
- Decorative images can use alt="".
Which attribute provides alternative text for an image?
Responsive Images
The srcset and sizes attributes allow browsers to choose an appropriate image resource based on viewport and device characteristics. The <picture> element allows art direction and format selection.
<img\n src="photo-800.jpg"\n srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1200.jpg 1200w"\n sizes="(max-width: 600px) 100vw, 800px"\n alt="Mountain landscape">- srcset can provide multiple image candidates.
- Responsive images can reduce unnecessary bandwidth.
Which attribute provides multiple image candidates?
Audio and Video
HTML provides native <audio> and <video> elements. The controls attribute exposes browser-provided playback controls. Multiple <source> elements can provide alternative media formats.
<video controls width="800" poster="preview.jpg">\n <source src="lesson.mp4" type="video/mp4">\n <source src="lesson.webm" type="video/webm">\n Your browser does not support HTML video.\n</video>- HTML supports native audio and video playback.
- Multiple source formats can improve compatibility.
Which attribute displays native video controls?
Figure and Figcaption
<figure> groups self-contained content such as an image, diagram, chart, or code example. <figcaption> provides a caption for the figure.
<figure>\n <img src="architecture.png" alt="Three-tier web architecture">\n <figcaption>Figure 1: A typical three-tier web application.</figcaption>\n</figure>- Use figure for self-contained illustrative content.
- Use figcaption for its associated caption.
Which element provides a caption for a figure?