The images lesson had one trap: a 5 MB photo is still downloaded in full, even when it is shown small on a phone. The solution is responsive images. This means the browser downloads the version of an image that fits the visitor's screen: a small file for phones, a big file for laptops. Phone users don't waste mobile data on a giant image, and laptop users still get a sharp one. HTML has two ways to make responsive images: the <picture> tag and the srcset attribute.
<picture>: different images for different screens
<picture>
<source media="(min-width: 1024px)" srcset="large.jpg">
<source media="(min-width: 600px)" srcset="medium.jpg">
<img src="small.jpg" alt="A beach at sunset">
</picture>
Part by part:
<picture>wraps several versions of one image.<source>is one version.media="(min-width: 1024px)"is its condition. It means "a screen at least 1024px wide". The brackets are part of it.srcset="large.jpg"is the file used when the condition matches.<source>has no closing tag.- The browser reads the
<source>tags from top to bottom and uses the first one whose condition matches the screen. - The
<img>at the bottom is the fallback: the browser uses it when no<source>matches. This<img>is required, because it is the tag that shows the image. A<source>only changes which file the<img>uses. Thealtalso goes here.
The result: a laptop with a 1280px wide screen uses large.jpg, a 768px tablet uses medium.jpg, and a 360px phone uses small.jpg. The browser downloads only one file, not all three. Each version can be cropped differently, for example a wide photo on laptops and a tall one on phones. With <picture>, you decide which image goes with which screen. But media does not look at how sharp the screen is, that is, how closely packed the screen's dots are. A phone with a sharp screen still gets small.jpg, which looks blurry on that screen. So when the versions are the same photo in different sizes, use srcset from the next part.
srcset and sizes: one image, several file sizes
<img src="large.jpg" alt="Photo"
srcset="small.jpg 480w, large.jpg 1024w"
sizes="(max-width: 600px) 480px, 1024px">
srcsetlists the available files with their real widths.small.jpg 480wmeans the file small.jpg is 480 pixels wide (wfor width). Each pair (a file and its width) is separated by a comma.sizestells the browser how wide the image will be shown: "on a screen up to 600px wide, the image is shown 480px wide. On a wider screen, 1024px". CSS sets how wide the image is shown, but the browser picks a file before it has read the CSS. So withoutsizes, the browser does not know that width. It assumes the image is as wide as the screen, and often downloads a file that is too big.srcis still written as a fallback for old browsers, andaltis still there.
The difference from <picture>: with srcset and sizes, the image is the same, and only the file size is different. The browser itself picks the file that is big enough for that screen.
In short, choose by what you need:
| What you need | Write |
|---|---|
| A fixed display size, such as a logo | <img src="logo.png" srcset="[email protected] 2x" alt="Logo">. A sharp screen (2x: its dots are twice as close) takes [email protected] |
| The image width follows the layout | srcset with w, and sizes, as in the example above |
| A different crop of the photo per screen | <picture> with <source media="..."> |
| A smaller file format, with a fallback | <picture> with <source type="image/avif" srcset="photo.avif">, then <img src="photo.jpg" alt="...">. A browser that does not know AVIF uses the JPG |
⚠️ Images that never show, or never get picked
- A
<picture>without an<img>. No image is shown at all. The<img>is always the last thing inside<picture>. - A
<picture>without a<source>. It is not broken, but it has no use: the result is the same as a plain<img>. alton the<source>.<source>has noalt. The alternative text always goes on the<img>.- A missing
w. The browser can't readsmall.jpg 480without thew, so it skips that file. mediawithout brackets.media="min-width: 768px"never matches. Writemedia="(min-width: 768px)".
TL;DR: Responsive images: the browser downloads the image version that fits the screen. A <picture> contains several <source media="..." srcset="..."> tags and one fallback <img> at the bottom. The browser uses the first <source> whose media matches the screen. When none match, it uses the <img>. For the same image in several file sizes, one <img> with srcset (the files and their widths, with w) and sizes (how wide the image is shown) is enough. src and alt always go on the <img>.
⚡ Different images for different screens
Write:
<picture>
<source media="(min-width: 1024px)" srcset="large.jpg">
<source media="(min-width: 600px)" srcset="medium.jpg">
<img src="small.jpg" alt="A beach at sunset">
</picture>
Guess first: how many files does a phone with a 360px wide screen download?
Just one: small.jpg. A 360px screen is narrower than 600px and 1024px, so no <source> matches and the browser uses the <img>.
Meaning: media="(min-width: 1024px)" is a condition: a screen at least 1024px wide. The browser reads the <source> tags from top to bottom and uses the first one that matches. The <img> at the bottom is the required fallback, and the alt goes on that <img>.
⚡ A <picture> with no <source>
Guess first: a <picture> that contains only an <img>, is it broken?
Not broken, but it has no use. The result is the same as a plain <img>.
Meaning: The <source> tags are what make a <picture> useful. But without the <img>, no image is shown at all.
⚡ One image, several file sizes
Write:
<img src="large.jpg" alt="Photo"
srcset="small.jpg 480w, large.jpg 1024w"
sizes="(max-width: 600px) 480px, 1024px">
Meaning: srcset lists the files with their real widths in pixels, written with w. sizes tells the browser how wide the image will be shown. The browser itself picks the file that is big enough.