Images are usually the heaviest part of a web page. Sending a 2,000-pixel photo to a phone with a 400-pixel screen wastes data and slows the page, which matters a lot on mobile networks. Responsive images let the browser choose the right file for each device. This lesson covers srcset and sizes, the <picture> element, modern formats such as WebP and AVIF, and the attributes that stop layouts from jumping.
In this lesson you will learn
- Offering several sizes with
srcsetand width descriptors - Telling the browser the display size with
sizes - Pixel-density descriptors for fixed-size images
- Format switching and art direction with
<picture> - Lazy loading and preventing layout shift
You should know the img element and responsive design.
srcset and sizes
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>srcset and sizes</title>
<style>
img { max-width: 100%; height: auto; display: block; }
</style>
</head>
<body>
<!-- The browser picks the smallest file that is big enough -->
<img
src="hills-800.jpg"
srcset="hills-400.jpg 400w,
hills-800.jpg 800w,
hills-1600.jpg 1600w"
sizes="(max-width: 700px) 100vw, 700px"
width="800" height="400"
alt="Green hills under a yellow sun"
loading="lazy"
decoding="async">
<!-- For a fixed-size image, describe pixel density instead -->
<img src="logo.png" srcset="logo.png 1x, logo@2x.png 2x"
width="120" height="40" alt="Core Tech ED logo">
</body>
</html>
srcsetlists the available files with their real widths:hills-400.jpg 400wmeans that file is 400 pixels wide.sizestells the browser how wide the image will be shown, before the CSS has loaded. Here: the full viewport width on screens up to 700px, otherwise 700px.- The browser combines the two with the screen’s pixel density. On a 360px phone with a 2× screen, it needs about 720 pixels, so it picks
hills-800.jpg. On a laptop it may pick 800 or 1600. You describe the options; the browser makes the choice. srcis the fallback for very old browsers and must still be present.- For an image that is always the same size, such as a logo, density descriptors are simpler:
logo.png 1x, logo@2x.png 2x. widthandheightlet the browser reserve the right space before the file arrives, so text does not jump down when it loads. CSSheight: autokeeps the image’s shape when it is resized.loading="lazy"waits until the image is near the screen;decoding="async"lets the page carry on while it is decoded.
The picture Element
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>picture element</title>
</head>
<body>
<!-- 1. Format switching: the first supported type wins -->
<picture>
<source type="image/avif" srcset="market.avif">
<source type="image/webp" srcset="market.webp">
<img src="market.jpg" alt="Vegetable stalls at a busy market" width="900" height="600">
</picture>
<!-- 2. Art direction: a different crop for narrow screens -->
<picture>
<source media="(max-width: 600px)" srcset="bridge-tall.jpg">
<img src="bridge-wide.jpg" alt="The Padma Bridge at sunset" width="1200" height="500">
</picture>
</body>
</html>
- Inside
<picture>, the browser reads the<source>elements in order and uses the first one that matches, then shows it through the<img>. - Format switching:
type="image/avif"andtype="image/webp"offer smaller modern formats; a browser that understands neither falls back to the JPEG. - Art direction:
media="(max-width: 600px)"serves a different, taller crop on narrow screens, so the bridge does not become a thin strip. - The
<img>is required. It supplies thealttext, the size and the fallback, and CSS styles it, not the picture element.
Image Formats
| Format | Best for | Notes |
|---|---|---|
| JPEG | Photos | Supported everywhere |
| PNG | Graphics with sharp edges or transparency | Large for photos |
| WebP | Photos and graphics | Usually 25–35% smaller than JPEG at similar quality |
| AVIF | Photos | Often smaller still; slower to create |
| SVG | Logos, icons, diagrams | Vector: sharp at any size |
A Practical Checklist
- Resize photos to the largest size they will be shown at; never upload straight from the camera.
- Offer two or three widths in
srcsetand write an honestsizes. - Serve WebP or AVIF through
<picture>where you can. - Always set
width,heightand meaningfulalttext. - Lazy-load images below the fold, but not the main image at the top of the page.
Common Mistakes
- Leaving out
sizeswith width descriptors. The browser then assumes the image fills the whole screen and may download a file far too big. - Lazy-loading the hero image, which delays the most important picture on the page.
- Styling
<picture>instead of the<img>inside it. - Empty or missing
alton images that carry information.
Practice Exercises
Exercise 1. An image is shown at half the screen width on screens wider than 900px and full width otherwise. Write its sizes attribute.
Show answer
sizes="(min-width: 901px) 50vw, 100vw"
Exercise 2. Write a <picture> that offers team.webp and falls back to team.jpg.
Show answer
<picture>
<source type="image/webp" srcset="team.webp">
<img src="team.jpg" alt="Our school debate team" width="800" height="500">
</picture>
Frequently Asked Questions
How do I make the different sizes of an image?
Image editors can export several sizes, and command-line tools or online converters can do it in bulk. Many content management systems, including WordPress, create several sizes automatically and write the srcset for you.
What is Cumulative Layout Shift?
A measure of how much a page’s content jumps around while loading. Images without width and height are a common cause. It is one of Google’s Core Web Vitals, which affect search ranking.
Summary
srcsetoffers files;sizessays how wide the image will be shown.<picture>switches formats and crops; it always needs an<img>.- WebP and AVIF are much smaller than JPEG.
- Set width, height and alt; lazy-load only images below the fold.
