Responsive Images in HTML: srcset, sizes and the picture Element
Serve a small image to a phone and a large one to a 4K monitor, without writing any JavaScript.
The problem responsive images solve
A single large image wastes bandwidth on small screens, while a single small one looks soft on large, high-density displays. Responsive images give the browser a menu of options and enough information to choose. The browser knows the viewport width, the pixel density and often the network conditions, so it picks better than you can in advance.
srcset with width descriptors
List the available files and their widths in pixels, plus a sizes attribute describing how wide the image will be displayed:
<img
src="/img/photo-800.jpg"
srcset="/img/photo-400.jpg 400w,
/img/photo-800.jpg 800w,
/img/photo-1600.jpg 1600w"
sizes="(min-width: 1000px) 800px, 100vw"
width="800" height="533"
alt="A red kayak on a calm lake"
loading="lazy">
Here, 400w means the file is 400 pixels wide. sizes says the image fills the full viewport width on small screens and 800 CSS pixels when the viewport is at least 1000 pixels wide. The browser multiplies by device pixel density and picks the closest suitable file. Older browsers ignore srcset and use src.
Density descriptors (1x, 2x)
For images with a fixed display size, such as a logo, you can use density descriptors instead: srcset="logo.png 1x, logo@2x.png 2x". Do not mix width and density descriptors in one srcset.
The picture element: formats and art direction
Use picture when you want to offer alternative formats or different crops. The browser uses the first source it supports:
<picture>
<source type="image/avif" srcset="/img/hero.avif">
<source type="image/webp" srcset="/img/hero.webp">
<img src="/img/hero.jpg" width="1200" height="630" alt="Mountain village at dawn">
</picture>The inner img is required: it holds the alt text, dimensions and fallback. For art direction, add media conditions, for example a tighter crop for portrait phones. Create those crops with the crop tool.
Creating the image files
- Start from the original full-size image.
- Resize to each width you want to offer (for example 400, 800, 1600) with the image resizer, which can produce several sizes at once.
- Compress each (quality 75–85) and, if desired, convert to WebP.
- Name files clearly, such as
photo-800.webp.
Common mistakes
- Missing
sizes: with width descriptors, the browser assumes the image is 100vw wide, so it may download a bigger file than needed. - No width and height: causes layout shift. Always set them.
- Lazy-loading the hero image: delays Largest Contentful Paint. See image optimisation for Core Web Vitals.
- Too many variants: three to five widths are usually enough.
- Empty or keyword-stuffed alt text: write what the image shows.
A complete, copy-paste hero image
<picture>
<source type="image/avif"
srcset="/img/hero-800.avif 800w, /img/hero-1600.avif 1600w"
sizes="(min-width: 1000px) 960px, 100vw">
<source type="image/webp"
srcset="/img/hero-800.webp 800w, /img/hero-1600.webp 1600w"
sizes="(min-width: 1000px) 960px, 100vw">
<img src="/img/hero-1600.jpg"
srcset="/img/hero-800.jpg 800w, /img/hero-1600.jpg 1600w"
sizes="(min-width: 1000px) 960px, 100vw"
width="1600" height="900" alt="Describe the image"
fetchpriority="high">
</picture>This offers three formats at two widths. The browser picks the first format it supports and then the best width for the screen. Because this is the hero image, it is not lazy-loaded and has a high fetch priority. For images further down the page, add loading="lazy" and decoding="async" and remove fetchpriority.
Do you really need all this?
For a small site, the biggest wins are simple: resize images to the displayed width, compress them, and set dimensions. Add srcset and picture when you have large hero images or heavy traffic from mobile. CMS platforms such as WordPress generate srcset automatically, as covered in the WordPress guide.
Try it now: Resize Image
Free, private and runs in your browser. Nothing is uploaded.
Open Resize ImageFrequently asked questions
Sources and further reading
- MDN: The img element
- MDN: The picture element
- Google Search Central: Image SEO best practices
- web.dev: Optimize Largest Contentful Paint
External links open the original source. We cite primary or official documentation for factual claims and re-check them when we update an article. See our editorial policy.
Keep reading
- Image Optimization for Core Web Vitals: Fix LCP and CLSHow images affect LCP and CLS, with fixes: right size and format, fetchpriority, no lazy-loading the hero, and set dimensions on every image.
- JPEG vs PNG vs WebP vs AVIF: Which Image Format Should You Use?Compare JPEG, PNG, WebP and AVIF on file size, quality, transparency and browser support, with a simple rule for choosing the right image format.
- Image Compression for WordPress: A Practical GuideHow to optimise images for WordPress: sizes, formats, quality, lazy loading and the 2560px threshold, plus a pre-upload workflow that keeps your site fast.
- Image Compression Explained: Formats, Quality Settings and ToolsHow image compression works, which format to use, what quality setting to pick and how to shrink photos without visible loss. A practical 2026 guide.