Responsive Images: srcset, picture and Fast Loading
Big images are the main reason websites load slowly. Let us send the right size image to every device.
The basic rule
img {
max-width: 100%;
height: auto;
}This stops images from being wider than the screen. Put it in every stylesheet.
Different sizes with srcset
A phone does not need a 2000px-wide photo. Make the same image in a few sizes and let the browser pick.
srcset a phone gets the small file and a laptop gets the bigger one.<img
src="lake-800.jpg"
srcset="lake-400.jpg 400w,
lake-800.jpg 800w,
lake-1600.jpg 1600w"
sizes="(min-width: 900px) 800px, 100vw"
alt="Blue lake with mountains behind"
width="800" height="533">srcsetlists the files and their real width (w).sizestells the browser how wide the image will show: "800px on big screens, full screen width on small screens".- The browser then downloads only the best file. This saves mobile data.
Modern formats with picture
<picture>
<source srcset="lake.avif" type="image/avif">
<source srcset="lake.webp" type="image/webp">
<img src="lake.jpg" alt="Blue lake with mountains behind" width="800" height="533">
</picture>The browser uses the first format it understands. Old browsers fall back to the JPG. AVIF and WebP files are often half the size of JPG.
<picture> lets the browser pick the best format it understands.A different crop for phones
A wide landscape photo can look tiny on a narrow phone. With <picture> and media, you can send a closer, taller crop to small screens:
<picture>
<source media="(max-width: 600px)" srcset="team-square.jpg">
<img src="team-wide.jpg" alt="Our team of five in the office" width="1200" height="500">
</picture>This is called art direction. Use it for big banner images where the important part, like a face, would become too small.
Lazy loading
<img src="photo.jpg" alt="..." loading="lazy" width="600" height="400">loading="lazy" loads the image only when the user scrolls near it. Use it for images lower on the page. Do not use it for the big image at the top, because that one should load at once.
Crop images neatly with object-fit
cover is usually what you want for cards and avatars..avatar {
width: 120px;
height: 120px;
object-fit: cover; /* fill the box, crop extra */
border-radius: 50%; /* round */
}Keep the shape with aspect-ratio
.thumb {
width: 100%;
aspect-ratio: 16 / 9; /* always a video shape */
object-fit: cover;
}aspect-ratio keeps a box the same shape at any width. Photos of different sizes now line up neatly in a grid. It also reserves the space before the image loads, so the page does not jump.
Background images
.banner {
background-image: url("banner.webp");
background-size: cover;
background-position: center;
min-height: 300px;
}Use background images only for decoration. Google and screen readers cannot see them, and you cannot add alt text. If the picture means something, use a real <img>.
How to make the different sizes
You need the same photo in a few widths, for example 400px, 800px and 1600px. Free ways to make them:
- Squoosh (squoosh.app): open the photo, set the width, choose WebP, download. Repeat for each size.
- Most image editors have Export or Save for web with a size option.
- Some hosting services and image CDNs can make sizes automatically from one upload.
Name the files clearly with the width: lake-400.webp, lake-800.webp.
Mini project: a fast product image
<picture>
<source srcset="shoe-400.webp 400w, shoe-800.webp 800w" type="image/webp">
<img
src="shoe-800.jpg"
srcset="shoe-400.jpg 400w, shoe-800.jpg 800w"
sizes="(min-width: 700px) 400px, 100vw"
alt="Blue running shoe, side view"
width="800" height="600"
loading="lazy">
</picture>This one image uses everything from the chapter: modern format, the right size for each screen, real width and height, lazy loading and clear alt text. Copy this pattern for every important image.
Image checklist
- ☐ The file is resized and compressed (most photos under 200 KB)
- ☐ WebP or AVIF is offered, with JPG as backup
- ☐
widthandheightare set - ☐
alttext describes the image (or is empty for pure decoration) - ☐
loading="lazy"on images below the first screen, not on the top image - ☐ The file name describes the picture:
blue-running-shoe.jpg
Practice time
- Add
max-width: 100%for all images. - Make 3 sizes of one photo and use
srcset. - Add
loading="lazy"to images below the first screen. - Make a 400px and 800px version of one photo and use them with srcset.
- Use
aspect-ratioto make a row of photos all the same shape.
Quick summary
max-width: 100%keeps images inside the screen.srcset+sizessend the right size file.<picture>serves modern formats;loading="lazy"saves data.