Lesson 3 of 5 · 5 min
Images and media
Images with good alt text, figures and captions, responsive images with srcset and picture, lazy loading, video, audio and embeds.
On this page
Images
<img src="/images/desk.jpg" alt="A laptop and a coffee cup on a wooden desk" width="1200" height="800">
srcis the file's address.altdescribes the image for people who can't see it, and is shown if the image fails to load.widthandheightlet the browser reserve space before the image arrives, so the page doesn't jump around while it loads. Set them to the image's real size; CSS can still scale it.
Writing alt text
Describe what the image communicates in this context, in a sentence or less:
- A photo in an article: describe what's in it that matters to the article.
- An image that is a link or button: describe where it goes or what it does, such as
alt="Home"on a logo. - A purely decorative image: use an empty
alt="". Screen readers will skip it. Never leavealtout entirely; then they read the file name. - A chart: summarise the takeaway, and put the data in a table or text nearby.
Figures and captions
<figure>
<img src="/images/box-model.png" alt="Diagram of content, padding, border and margin as nested boxes">
<figcaption>The CSS box model, from the inside out.</figcaption>
</figure>
Image formats
| Format | Use for |
|---|---|
| JPEG | Photos |
| PNG | Screenshots, images that need transparency |
| WebP / AVIF | Photos and graphics, much smaller than JPEG or PNG |
| SVG | Logos, icons and diagrams; sharp at any size |
Lazy loading
<img src="photo.jpg" alt="..." loading="lazy" decoding="async">
loading="lazy" waits to download images until the reader scrolls near them. Don't lazy-load the main image at the top of the page; it should load straight away.
Responsive images
Send small files to small screens:
<img
src="hero-800.jpg"
srcset="hero-400.jpg 400w, hero-800.jpg 800w, hero-1600.jpg 1600w"
sizes="(min-width: 1000px) 1000px, 100vw"
alt="...">
srcset lists the files and their widths. sizes tells the browser how wide the image will be shown. The browser picks the best file for the screen and its pixel density.
<picture> lets you offer modern formats with a fallback:
<picture>
<source srcset="hero.avif" type="image/avif">
<source srcset="hero.webp" type="image/webp">
<img src="hero.jpg" alt="...">
</picture>
Video and audio
<video controls width="800" poster="intro-poster.jpg" preload="metadata">
<source src="intro.webm" type="video/webm">
<source src="intro.mp4" type="video/mp4">
<track kind="captions" src="intro.en.vtt" srclang="en" label="English">
Your browser can't play this video. <a href="intro.mp4">Download it</a>.
</video>
<audio controls src="episode-1.mp3"></audio>
Captions (<track>) help deaf viewers and anyone watching with the sound off.
Embeds
YouTube videos, maps and other widgets come in an iframe:
<iframe
src="https://www.youtube-nocookie.com/embed/VIDEO_ID"
title="Video: Introduction to CSS Grid"
loading="lazy"
allowfullscreen></iframe>
Give every iframe a title; screen readers announce it.
Try it
Add a figure with an image and caption to your page. Give the image width, height and good alt text. Then add loading="lazy" to an image further down and watch the Network panel in the developer tools: it loads only when you scroll to it.
More from HTML Crash Course
- Lesson 5 · 5 minSemantic HTML and accessibilityLandmarks like header, nav, main and footer, articles and sections, tables for data, and a short checklist for accessible, search-friendly pages.
- Lesson 1 · 5 minYour first HTML documentElements, tags and attributes, the boilerplate every page needs, and what each line of it does.