Step 1 Pick What You Need
Different sizes is the usual choice for photos. Pixel density is for images that are always the same size, like a logo. Modern formats adds AVIF and WebP versions, and Art direction shows a different crop on some screens.
The frame is as wide as the screen you picked. Placeholders stand in for your images, and the line above the picture says which file your own browser picked.
<style>
img {
max-width: 100%;
height: auto;
}
</style>
<img
src="https://example.com/images/mountain-1600.jpg"
srcset="https://example.com/images/mountain-480.jpg 480w,
https://example.com/images/mountain-800.jpg 800w,
https://example.com/images/mountain-1200.jpg 1200w,
https://example.com/images/mountain-1600.jpg 1600w"
sizes="(max-width: 600px) 100vw, (max-width: 1200px) 50vw, 800px"
alt="A snowy mountain peak at sunrise, with pink clouds behind it"
width="1600"
height="1067"
loading="lazy"
decoding="async">
Which file loads? mountain-800.jpg
Browsers can pick differently: one that already has a bigger copy cached may keep using it, Chrome may take a slightly smaller file when it's close enough, and data-saver modes can go smaller.
Checks
A responsive image is an image that comes in more than one file, so each visitor's browser can download the one that fits their screen. A phone gets a small file, a big monitor gets a big one, and a high-density screen (what Apple calls Retina) gets one with enough pixels to look sharp.
Here's the plain way to add an image:
<img src="/images/photo-1600.jpg" alt="A snowy mountain at sunrise">
Everyone downloads the same 1600-pixel file. On a phone that shows the picture about 375 pixels wide, that's several times more pixels than the screen
can use, and the extra download costs time and mobile data. The fix is to list a few sizes with srcset and let the browser choose.
This generator writes that code for you: srcset with widths or 1x/2x files, the sizes attribute, the
<picture> element for AVIF and WebP, and different crops for phones. It shows which file a browser would load at any screen width, and
checks for the usual mistakes. It runs in your browser, and your last settings are remembered for next time.
Different sizes is the usual choice for photos. Pixel density is for images that are always the same size, like a logo. Modern formats adds AVIF and WebP versions, and Art direction shows a different crop on some screens.
Type a file name pattern where {w} stands for the width, like /images/photo-{w}.jpg, then pick the
widths you have (or use a preset). Image services that resize on the fly work too: https://example.com/img/photo.jpg?w={w}. If your
files don't follow a pattern, switch to List of files and type one per line, like /images/small.jpg 400w.
In sizes, add a row for each screen width where the image changes size, and an Otherwise size for the rest. The Quick start buttons fill in common layouts. The plain-English lines under the rows say what your sizes means.
Describe the picture in Alt text (or mark it Decorative). Keep width and height on and set the aspect ratio so the page doesn't jump. Leave loading="lazy" on for images further down the page, and switch it off for the big image at the top.
Move the Screen slider and pick a Pixel ratio. Which file loads? explains the browser's choice step
by step, and the preview shows the file your own browser picked. Read the Checks, then press Copy. Switch off
CSS if your stylesheet already has img { max-width: 100%; height: auto; }.
💡 The preview never loads your real images. Each file is drawn as a colored placeholder with its width written on it, so you can see which one was picked.
srcset is a list of files, separated by commas. After each file comes a descriptor that tells the browser something about
it, and there are two kinds:
| Descriptor | It means | Use it for |
|---|---|---|
800w | This file is 800 pixels wide. | Photos and pictures that change size with the page. Needs sizes. |
2x | Use this file on screens with a pixel ratio of 2 or more. | Logos, icons and avatars that are always the same size on the page. |
With widths, you tell the browser how big each file is and let it do the math:
<img
src="/images/photo-800.jpg"
srcset="/images/photo-400.jpg 400w,
/images/photo-800.jpg 800w,
/images/photo-1600.jpg 1600w"
sizes="(max-width: 600px) 100vw, 800px"
alt="A snowy mountain at sunrise"
width="1600" height="1067"> With densities, you tell it which file is for which kind of screen:
<img
src="/images/logo-200.png"
srcset="/images/logo-200.png 1x,
/images/logo-400.png 2x"
alt="Example Bakery"
width="200" height="60">
Don't mix the two in one srcset. Keep src as well: it's the file for very old browsers, and the browser treats it as the 1x
file when srcset uses densities.
The browser starts downloading images before it has read your CSS, so it doesn't know yet how wide the image will be on the page. sizes
tells it. It's a list of "on screens like this, the image is this wide" pairs, and the last value has no condition: it's the size for every other
screen. The browser reads from the left and stops at the first condition that matches.
| sizes value | What it says |
|---|---|
100vw | The image is as wide as the screen. This is what the browser assumes when there is no sizes. |
50vw | Half the screen width, like one of two columns. |
800px | Always 800 CSS pixels wide, whatever the screen. |
calc(100vw - 32px) | The screen width minus 16px of padding on each side. |
(max-width: 600px) 100vw, 50vw | Full width on screens up to 600px wide, half the width on bigger ones. |
min(100vw, 800px) | As wide as the screen, but never more than 800px. |
vw means a percent of the screen width: 50vw is half the screen. A plain % doesn't work in sizes. Inside
calc(), put spaces around + and -: calc(100vw - 32px) works, calc(100vw-32px) doesn't.
Take the first example above on a phone that's 375 pixels wide with a pixel ratio of 2 (most phones today have 2 or 3). The screen is under 600px, so
sizes says 100vw, which is 375px. Twice 375 is 750, so the browser looks for a file at least 750 pixels wide. The smallest one is
800w:
On a laptop 1366px wide with a pixel ratio of 1, sizes says 800px, so the 800w file is enough there too. That's the point: two
very different screens, and nobody downloads the 1600-pixel file unless they need it. Browsers treat this as a hint, though. One that already has a
bigger copy in its cache may keep using it, and Chrome may take a slightly smaller file when it's close enough.
AVIF and WebP are newer image formats that usually make smaller files than JPG or PNG at the same quality. Current Chrome, Edge, Firefox and Safari can
show both, but some older browsers can't, so you offer them inside <picture> with a normal <img> as the fallback:
<picture>
<source type="image/avif" srcset="/images/photo-800.avif">
<source type="image/webp" srcset="/images/photo-800.webp">
<img src="/images/photo-800.jpg" alt="A snowy mountain at sunrise"
width="800" height="533">
</picture>
The browser goes down the <source> list and uses the first type it understands, so put AVIF first, then WebP. The
<img> is still the image that shows on the page: the sources only change which file it loads. That's why the alt text, width, height
and loading go on the <img>, and why you style the <img>, not the picture.
To combine formats with sizes, give every source its own srcset and sizes:
<picture>
<source type="image/avif"
srcset="/images/photo-400.avif 400w, /images/photo-800.avif 800w"
sizes="(max-width: 600px) 100vw, 800px">
<source type="image/webp"
srcset="/images/photo-400.webp 400w, /images/photo-800.webp 800w"
sizes="(max-width: 600px) 100vw, 800px">
<img src="/images/photo-800.jpg"
srcset="/images/photo-400.jpg 400w, /images/photo-800.jpg 800w"
sizes="(max-width: 600px) 100vw, 800px"
alt="A snowy mountain at sunrise" width="800" height="533">
</picture>
Sometimes a smaller copy of the same picture isn't enough. A wide group photo turns into a strip of tiny faces on a phone, and a square close-up works
better. That's called art direction, and it's the one job srcset can't do, because the browser treats every file in a srcset as the same
picture. Use <source media="..."> instead:
<picture>
<!-- Phones: a square close-up -->
<source media="(max-width: 600px)"
srcset="/images/team-square-600.jpg"
width="600" height="600">
<!-- Everyone else: the wide group photo -->
<img src="/images/team-wide-1200.jpg" alt="Our five bakers outside the shop"
width="1200" height="500">
</picture>
The browser takes the first source whose media matches, so order matters: with max-width, put the smallest screens first. The
width and height on each source give each crop its own shape, so the page saves the right space for it. Current browsers
support this; older ones fall back to the size on the <img>.
Without width and height, the browser doesn't know how tall an image will be until the file arrives. The text below it jumps
down when it does, which is annoying (you tap the wrong link) and counts against the page in Google's Core Web Vitals as layout shift.
The numbers don't fix the size on screen. With this small piece of CSS, the image still shrinks to fit, and the browser only uses the two numbers for the shape:
<style>
img {
max-width: 100%;
height: auto;
}
</style>
<img src="/images/photo-800.jpg" alt="A snowy mountain at sunrise"
width="800" height="533">
So you can use the real size of your biggest file, or any pair with the same aspect ratio. A 3:2 photo can be width="1200" height="800" or
width="3" height="2". The generator works out the height from the ratio you type.
loading="lazy" tells the browser to wait until the image is close to the screen, which saves data for images people may never scroll to.
fetchpriority="high" does the opposite: it moves an image to the front of the queue. decoding="async" lets the browser unpack
the image without holding up the rest of the page.
<!-- The big image at the top: load it early -->
<img src="/images/hero-1600.jpg" alt="Fresh bread on the counter"
width="1600" height="900" fetchpriority="high">
<!-- Images further down: wait until they're near the screen -->
<img src="/images/oven-800.jpg" alt="The wood oven"
width="800" height="600" loading="lazy" decoding="async">
The rule of thumb: the big image at the top of the page (the hero) gets fetchpriority="high" and no lazy loading. Everything further down
gets loading="lazy". Never use both on one image.
max-width before a smaller one hides the
smaller one:
<!-- Never uses the second row: 400px is also "up to 1200px" -->
sizes="(max-width: 1200px) 50vw, (max-width: 600px) 100vw, 800px"
<!-- Fixed: smaller max-widths first -->
sizes="(max-width: 600px) 100vw, (max-width: 1200px) 50vw, 800px" <img srcset sizes> is
shorter and lets the browser choose more freely. Use <picture> for formats and different crops.
height: auto in your CSS.alt="" only for decoration, so screen readers skip it.
800w has to be a file that's really 800 pixels wide, or the browser's math is off and
images look blurry or download more than needed.
Want to check the rest of the page? The HTML Validator finds missing alt text, unclosed tags and typos and explains them in plain English. You can look up <img>, <picture> and <source> in the HTML Tags list.
vh, because those depend on the screen height.
Starting a new page? The HTML Starter Template gives you the doctype, head and layout to put your images in.
srcset is an attribute on <img> (and <source>) that lists several files of the same picture, separated by commas. Each file has a descriptor after it: a width like 800w, or a pixel density like 2x. The browser looks at the screen and picks the file that fits best, so phones download small files and big or high-density screens get sharper ones. Keep the src attribute too, as the fallback for very old browsers.
It tells the browser how wide the image will be on the page, before the CSS has loaded. For example, sizes="(max-width: 600px) 100vw, 50vw" means "as wide as the screen on screens up to 600px, half the screen on bigger ones". The browser multiplies that width by the screen's pixel ratio and picks the smallest file in srcset that is at least that wide. Without sizes, it assumes the image fills the whole screen (100vw).
Use widths (400w, 800w) when the image changes size with the page, like most photos in articles, cards and banners. Use densities (1x, 2x) when the image is always shown at the same size, like a 200px logo or an avatar: the 2x file is twice as wide and keeps it sharp on high-density screens. Don't mix both kinds in one srcset.
Use <picture> for two jobs: offering newer formats like AVIF and WebP with a JPG or PNG fallback, and art direction, where some screens get a different crop (a square close-up on phones, a wide shot on desktops). For the same picture in several sizes and one format, a plain <img> with srcset and sizes is simpler and lets the browser choose more freely.
Wrap the image in <picture>, add a <source type="image/avif" srcset="photo.avif">, then a <source type="image/webp" srcset="photo.webp">, and end with a normal <img src="photo.jpg" alt="...">. The browser uses the first source type it understands, and browsers that know neither format load the JPG. Put the alt text, width, height and loading attributes on the <img>, because the <img> is what shows on the page.
Yes. They let the browser save the right amount of space before the file arrives, so the page doesn't jump. Add img { max-width: 100%; height: auto; } to your CSS and the image still shrinks to fit: the browser only uses the two numbers for the shape. Use the size of your biggest file, or any pair with the same aspect ratio.
No. loading="lazy" is great for images further down the page, because it waits until they are near the screen. The big image at the top of the page (the hero) should load right away, so leave lazy loading off there and consider fetchpriority="high" instead. Lazy-loading the hero makes it start later, which is one of the most common slowdowns.
The srcset choice is a hint, not an order. A browser that already has a bigger copy in its cache may keep using it, Chrome may take a slightly smaller file when it is close enough, and data-saver modes can go smaller. Also check that sizes matches your real layout, that the rows are in the right order (the first match wins), and that each w number is the real width of that file.