HTML Foundations·Beginner foundations

Responsive images and performance

Load the right image size and keep the page from jumping.

Beginner 12 min read Lesson 6 of 480 Challenge included
What you will be able to do
  • Reserve space so the page does not jump
  • Serve the right file size with srcset and sizes
  • Lazy-load images that are below the fold
  • Choose modern formats like WebP and AVIF

In plain English

Images are usually the heaviest part of a page, so how you load them matters. Width and height attributes reserve space before the image arrives, which prevents layout shift, the jump that happens when content moves. A srcset lists the same image at several sizes and sizes tells the browser how wide it will render, so it picks the smallest file that still looks sharp. loading="lazy" waits to fetch an image until it is near the screen, and modern formats like WebP and AVIF are much smaller than older ones. Do not lazy-load the image people see first, because it is needed immediately.

Step by step

  1. 1Decide how much space the image will occupy in your layout.
  2. 2Add width and height so the page reserves that space.
  3. 3Export the image at two or three sizes and list them in srcset.
  4. 4Add sizes so the browser knows how wide the image renders.
  5. 5Add loading="lazy" to images that sit below the first screen.
responsive.htmlHTML
<img
  src="hero-800.jpg"
  srcset="hero-400.jpg 400w, hero-800.jpg 800w, hero-1600.jpg 1600w"
  sizes="(max-width: 600px) 100vw, 800px"
  width="800"
  height="450"
  alt="Sunrise over the hills"
  loading="lazy"
  decoding="async"
>

Common mistakes

  • Leaving off width and height and watching the page jump as images load.
  • Serving one huge image to every device instead of using srcset.
  • Lazy-loading the main image at the top of the page and delaying what people see first.
  • Skipping WebP or AVIF and shipping files several times larger than needed.
Your turn

Make an article image responsive

Medium

This image sits below the first screen. Add width and height, a srcset with two sizes, a sizes value and lazy loading.

HTML · starter
<img src="diagram-800.png" alt="A diagram of the request and response cycle">
Check your understanding before moving on.
Knowledge checkQuestion 1

What is the main benefit of adding width and height to an image?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Icons and inline SVG
Lesson 6 of 480 · 0% of course