A photo that looks crisp on a large desktop monitor might be 2,000 pixels wide and weigh several hundred kilobytes. Send that same file to a phone that displays it 375 pixels wide, and the visitor downloads far more data than they can see. Send a small file to a high-resolution laptop instead, and the photo looks soft and blurry. Responsive images solve this by giving the browser a menu of image files and letting it choose the best one for each screen.
This guide walks through how srcset and sizes work, how to decide which image widths to create, when to use the picture element, and how to keep your hosted image versions organized.
Why One Image Size Doesn’t Fit Every Screen
Two things decide how many pixels a device actually needs:
- Display width: how wide the image appears on the page, measured in CSS pixels. A blog photo might fill the whole width of a phone but sit in a 700-pixel column on a desktop.
- Pixel density: many phones and laptops pack two or three physical pixels into each CSS pixel. A 375-pixel-wide slot on a phone with 3x density needs roughly a 1,125-pixel-wide image to look sharp.
A single image file can’t be the right answer for both a budget phone on a slow connection and a high-density desktop display. Oversized files slow down page loads and use up mobile data; undersized files look soft. With responsive images, you create several versions and let the browser do the math.
How srcset and sizes Work Together
The standard img tag gets two extra attributes. Here’s a typical example for a blog photo:
<img src=”lake-800.jpg” srcset=”lake-480.jpg 480w, lake-800.jpg 800w, lake-1200.jpg 1200w, lake-1600.jpg 1600w” sizes=”(max-width: 700px) 100vw, 700px” width=”1600″ height=”1067″ alt=”Mountain lake at sunset”>
Width Descriptors in srcset
The srcset attribute lists each image file followed by its actual pixel width, written with a w. So lake-1200.jpg 1200w tells the browser that file is 1,200 pixels wide. You’re describing the files, not giving instructions; the browser decides which one to download.
The sizes Attribute
The browser needs to know how wide the image will appear before it has finished laying out the page, which is why you tell it with sizes. In the example above, the rule reads: if the viewport is 700 pixels wide or less, the image fills 100% of the viewport width (100vw); otherwise, it displays at 700 pixels.
The browser combines that slot width with the screen’s pixel density and picks a suitable file:
- A phone with a 375-pixel viewport at 3x density needs about 1,125 pixels, so it will typically pick the 1200w file.
- A desktop with a 700-pixel slot at 1x density needs about 700 pixels, so it picks the 800w file.
- The same desktop layout on a 2x display needs about 1,400 pixels, so it picks the 1600w file.
If you leave out sizes while using width descriptors, browsers assume the image is the full viewport width. That often leads to downloading files much larger than needed, so always include it.
Density Descriptors for Fixed-Size Images
Some images always display at the same size, such as a 200-pixel-wide author photo or a logo. For those, you can skip sizes and use density descriptors instead:
<img src=”author-200.jpg” srcset=”author-200.jpg 1x, author-400.jpg 2x, author-600.jpg 3x” width=”200″ height=”200″ alt=”Author headshot”>
Don’t mix w and x descriptors in the same srcset. Pick one approach per image.
How to Choose Which Image Widths to Create
You don’t need a file for every possible screen. A handful of well-spaced sizes covers most situations. A practical approach:
- Find the largest display width. Check how wide the image appears in your layout on a large screen. For a content column, that might be 700 to 900 pixels; for a full-width hero banner, it might be the full screen.
- Double it for high-density screens. Your largest file should be about twice the maximum display width. Going to 3x for large images rarely produces a visible difference and adds a lot of weight.
- Fill in steps below that. Four to six sizes spaced a few hundred pixels apart work well, for example 480, 800, 1200, 1600 and 2000 pixels for a full-width image.
- Never upscale. If your original photo is only 1,400 pixels wide, don’t create a 2,000-pixel version. Enlarging adds file size without adding detail.
Keep every version at the same aspect ratio and crop. The browser treats srcset candidates as interchangeable copies of the same image, so a different crop in one file can produce unexpected results.
Using the picture Element for Formats and Art Direction
The picture element wraps an img tag and adds source elements the browser checks first. It’s useful in two situations.
Serving Modern Formats with a Fallback
Formats like AVIF and WebP often produce smaller files than JPEG at similar visual quality. With picture, browsers that support them use the modern file, and others fall back to JPEG:
<picture> <source type=”image/avif” srcset=”lake-800.avif 800w, lake-1600.avif 1600w” sizes=”(max-width: 700px) 100vw, 700px”> <source type=”image/webp” srcset=”lake-800.webp 800w, lake-1600.webp 1600w” sizes=”(max-width: 700px) 100vw, 700px”> <img src=”lake-800.jpg” srcset=”lake-800.jpg 800w, lake-1600.jpg 1600w” sizes=”(max-width: 700px) 100vw, 700px” width=”1600″ height=”1067″ alt=”Mountain lake at sunset”> </picture>
Art Direction for Different Screens
Sometimes a wide landscape shot loses its subject when shrunk onto a phone. With a media attribute on a source element, you can serve a tighter, differently cropped version to narrow screens. Unlike srcset alone, a media condition is an instruction the browser must follow, so it’s the right tool when you want a different crop rather than just a different size.
Hosting and Organizing Multiple Image Sizes
Responsive images mean more files, so a consistent system saves time later:
- Use predictable file names. Include the width in each name, such as lake-480.jpg, lake-800.jpg and lake-1200.jpg. You’ll be able to tell versions apart at a glance and build srcset lists quickly.
- Export from the original. Resize each version from your full-resolution master file rather than from a smaller copy, which keeps every version as sharp as possible.
- Keep versions together. Store all sizes of one image in the same folder or album so nothing goes missing when you update a post.
- Use stable direct links. Each version needs its own URL that won’t change. If you host images with a service like ImageCoast, upload each size and copy the direct link for each into your srcset.
To confirm it’s working, open your browser’s developer tools, load the page, and check the Network panel to see which file downloaded. Then switch to the device emulation mode, pick a phone, and reload. Browsers may keep using a larger file that’s already cached, so test in a private window or with the cache disabled.
Common Mistakes to Avoid
- A sizes value that doesn’t match your CSS. If sizes says 100vw but the image actually sits in a 600-pixel column, phones and desktops will download larger files than needed. Update sizes whenever you change your layout.
- Forgetting the src attribute. Always include a sensible default in src for older browsers and tools that don’t read srcset.
- Leaving out width and height. These attributes let the browser reserve space before the image loads, preventing content from jumping around. Pair them with CSS such as max-width: 100%; height: auto; so the image still scales.
- Lazy-loading the main image. Adding loading=”lazy” is great for photos further down the page, but your top hero image should load right away.
- Expecting swaps on resize. Once a large image has loaded, browsers generally won’t download a smaller one if the window shrinks. That’s normal behavior, not a bug.
Set up once, responsive images keep working quietly in the background: faster pages for mobile visitors and sharper photos for high-resolution screens. Store your resized versions in one place, such as an ImageCoast account, and adding them to new posts becomes routine.
Frequently Asked Questions
Do I still need the src attribute if I use srcset?
Yes. The src attribute acts as a fallback for browsers or tools that don’t support srcset, and it’s required for valid HTML. Point it at a mid-sized version that looks acceptable on most screens.
How many image sizes should I create?
Four to six sizes are enough for most images. Start with a small mobile size around 400 to 500 pixels wide and go up to roughly twice the largest width the image displays at in your layout.
Can I tell the browser exactly which file to use?
Not with srcset alone. Srcset gives the browser options, and it may factor in screen density, cached files or other conditions. If you need a specific file for specific screens, such as a different crop, use the picture element with media conditions.
Does srcset work with images hosted on another site?
Yes. Each entry in srcset is just a URL, so you can point to direct links from any image host, as long as each version has its own stable link and the files share the same aspect ratio.
Is responsive image markup good for SEO?
It can help indirectly. Smaller downloads make pages load faster, especially on mobile, and page speed is part of a good user experience. Keep writing descriptive alt text, since srcset doesn’t change how search engines read it.