Lesson 5 of 23
In progress
Images, alt text and layout stability
Add images with useful alternative text, reserve their space to avoid layout shift, and choose formats that stay small.
An image is content, not decoration you sprinkle on a page. The alt attribute decides whether a blind visitor gets the information, and the width and height attributes decide whether the page jumps while it loads.
What you will learn
- Add an image with a useful text alternative.
- Reserve space so the layout does not shift.
- Choose between formats and load large images responsibly.
The image element
Editable. Runs in your browser.
Press Run to see the output.
src is the file, alt is the text alternative, and width/height are the intrinsic pixel dimensions. The browser uses the last two to reserve the right aspect ratio before the bytes arrive, which prevents the content below from jumping.
figure and figcaption group an image with a caption. The caption is visible; the alt text is not. They do different jobs: the caption might say "Figure 1: the Kali river", while the alt describes what the image shows.
Alt text is a sentence, not a filename
Write what the image conveys in context. A photo of a bridge on a travel page might need "The Howrah bridge at dawn, crowded with cyclists". The same photo as decoration beside a heading needs alt="".
An empty alt is not missing alt. It tells a screen reader to skip the image. Missing alt makes some readers announce the filename, which is noise. Decorative images get alt=""; every informative image gets a description.
Do not start with "Image of" or "Picture of": the screen reader already announces an image. Do not stuff keywords into alt either; it is read aloud and it is not a ranking trick.
If an image is a link, the alt becomes the link text. alt="Read the Flexbox guide" is useful; alt="icon" is not.
Size and format
A photograph should be WebP or AVIF at the size it will be displayed. Serving a 4000-pixel photo in a 640-pixel slot wastes bandwidth and slows the page, especially on mobile data.
- JPEG for photos when compatibility is the priority.
- WebP for most photos and graphics today: smaller at similar quality.
- AVIF for even smaller files, with slightly less tool support.
- SVG for logos and icons, because it scales without blurring.
- PNG for screenshots and images that need transparency.
The srcset and sizes attributes let the browser choose a smaller file for a small screen:
<img src="photo-640.webp"
srcset="photo-320.webp 320w, photo-640.webp 640w, photo-1280.webp 1280w"
sizes="(max-width: 700px) 100vw, 640px"
alt="A market stall lit by string lights"
width="640" height="360">
The browser reads the layout width from sizes and picks the smallest candidate that will look sharp. This is the single biggest image performance win on most sites.
Loading behaviour
Images below the fold should be loading="lazy" so they are fetched only when needed. The main image at the top of the page should not be lazy; it is the largest contentful paint, and delaying it delays the whole page.
Editable. Runs in your browser.
Press Run to see the output.
decoding="async" lets the browser decode off the main thread. Do not add both loading="lazy" and fetchpriority="high" to the same image; they pull in opposite directions.
For the hero image, fetchpriority="high" tells the browser it matters more than other resources.
Stretching a small image with CSS to fill a large area makes it blurry, and serving a huge image for a small slot wastes bandwidth. Resize the file to roughly twice the display size for high-density screens, and use CSS only for fine adjustments.
Add a figure with an informative image, a caption, explicit width and height, and alt text that describes what matters. Then add a purely decorative icon with an empty alt and explain in a comment why it is empty.
Show solution
Editable. Runs in your browser.
<!– Decorative divider: adds no information, so alt is empty. –>
<img src="https://picsum.photos/id/1069/600/20" alt="" width="600" height="20">
Press Run to see the output.
The informative image describes the scene rather than naming the file. The decorative one is skipped by screen readers because the empty alt says so deliberately.
Recap
altdescribes the image’s purpose; usealt=""for decoration, never omit it.- Always set
widthandheightto reserve space and prevent layout shift. - Serve WebP or AVIF at display size, use
srcsetfor responsive images, and lazy-load only below the fold.
Next, you will group content with lists and present data in tables.
Is alt text required?
Should I use an image for text?
Progress is saved in this browser only. No account, no email.