How do you optimise images for the web?

IntermediatePerformance interview questionBy , updated 17 November 2026

Short answer

To optimise images for the web, serve each image in a modern format (AVIF or WebP, with a fallback), at the size it is displayed (using srcset and sizes), compressed to a sensible quality. Set width and height so the layout does not shift, lazy-load images below the fold, and give the LCP image high priority instead. An image CDN or framework component can do most of this automatically.

1. Choose the right format

Format Best for Notes
AVIF Photos and complex images Usually the smallest file at the same visual quality; slower to encode; supports transparency
WebP Photos, and a fallback for AVIF Smaller than JPEG in most cases; supports transparency and animation
JPEG Photos where old clients matter Universal; use progressive encoding and a modern encoder such as MozJPEG
PNG Lossless graphics, screenshots with sharp text Large for photos; WebP or AVIF lossless are often smaller
SVG Logos, icons, simple illustrations Vector, sharp at any size; minify with SVGO; never wrap a photo in SVG
MP4 / WebM video Animations A short video is usually far smaller than an animated GIF

AVIF and WebP work in all current major browsers. If you still need a fallback, <picture> lets the browser take the first format it supports:

<picture>
  <source type="image/avif" srcset="/img/team-800.avif 800w, /img/team-1600.avif 1600w" sizes="(min-width: 60rem) 50vw, 100vw" />
  <source type="image/webp" srcset="/img/team-800.webp 800w, /img/team-1600.webp 1600w" sizes="(min-width: 60rem) 50vw, 100vw" />
  <img src="/img/team-800.jpg" alt="The team at the 2026 offsite" width="1600" height="900" loading="lazy" decoding="async" />
</picture>

The <img> inside <picture> is required; it carries alt, width, height and loading behaviour for whichever source wins.

2. Serve the right size

A 3000-pixel photo shown in a 400-pixel card wastes most of its bytes. Export several widths and list them in srcset with sizes so the browser downloads the smallest file that looks sharp on that screen and pixel density. How the browser makes that choice is covered in srcset and sizes. As a rule, two or three widths per layout slot cover most screens; you do not need ten.

3. Compress sensibly

Most photos look the same to users at a lower quality setting than the export default. Compare by eye at the display size, not zoomed in, and pick the lowest quality you cannot tell apart. Strip metadata (EXIF, colour profiles you do not need, embedded thumbnails); it can be a large share of a small image. Automate this in the build or the CDN, because hand-optimised images drift as people upload new ones.

4. Stop layout shift

An image without dimensions takes no space until it loads, then pushes content down, which hurts CLS. Set width and height attributes to the image's intrinsic size; the browser uses them to compute an aspect ratio and reserve the space before the file arrives. Keep it responsive in CSS:

img {
  max-width: 100%;
  height: auto; /* keeps the aspect ratio from width and height */
}

For images in containers of fixed shape, aspect-ratio with object-fit: cover does the same. See Core Web Vitals for how CLS is scored.

5. Load in the right order

Below the fold: loading="lazy" lets the browser fetch an image only as it nears the viewport, which saves bandwidth for images many users never scroll to. The trade-offs are in lazy loading for images and routes.

The LCP image: never lazy-load it. Lazy loading delays the request until layout runs, which is exactly the wrong thing for the largest element on the screen. Instead:

<img src="/hero-1200.avif" srcset="/hero-800.avif 800w, /hero-1200.avif 1200w, /hero-2000.avif 2000w"
     sizes="100vw" width="2000" height="1000" fetchpriority="high" alt="Spring collection" />

fetchpriority="high" moves it ahead of other images. If the hero is a CSS background or is inserted by JavaScript, the browser finds it late; a <link rel="preload" as="image"> fixes that, as explained in preload vs prefetch vs preconnect. Better still, make it a real <img> in the server-rendered HTML.

decoding="async" tells the browser it may decode the image off the critical path, which can help large images that are not the LCP element.

6. Use an image CDN or a framework component

Producing every format and width by hand does not scale. An image CDN or image service resizes, converts and compresses on request from one original, using URL parameters such as ?w=800&q=70, and picks AVIF or WebP from the browser's Accept header. It also caches the results at the edge near users. In Next.js, next/image does the same through its built-in optimiser or a configured loader, and adds lazy loading and reserved space by default.

7. Placeholders

A small blurred preview or a dominant-colour background makes slow images feel faster and gives the space a shape. Keep the placeholder tiny (inline as a data URI only if it is a few hundred bytes), and do not let it replace setting dimensions.

Practise it

Common follow-up questions

Should I always use AVIF?

Use it as the first choice for photos, with WebP or JPEG as a fallback if you support older clients. For tiny images and simple graphics the gain may be small, and SVG or PNG can be better. Encoding AVIF is slower, so do it once in the build or CDN, not on every request without caching.

Is a CSS background image worse than an img tag?

For content images, usually yes. The browser finds it only after the CSS is downloaded and parsed, it cannot use srcset and sizes (only image-set()), it cannot be lazy-loaded natively, and it has no alt text. Keep backgrounds for decoration.

How do I find which images to fix first?

Run Lighthouse or open the Network panel filtered to images and sort by size. Look at the LCP element in the Performance panel. Fix the LCP image first, then the largest files, then images served much larger than displayed.

Does lazy loading hurt SEO?

Native loading="lazy" does not stop search engines from finding the image, because the <img> and its src are in the HTML. Custom lazy loaders that keep the URL only in a data-src attribute can, so prefer the native attribute.

Mistakes to avoid

  • Lazy-loading the hero or LCP image

  • Leaving out width and height, so the page jumps as images load

  • Serving one large file to every screen size

  • Using PNG for photos or animated GIFs for animations

  • Putting important images in CSS backgrounds

  • Optimising by hand once and never again; automate it

  • Missing alt text, or describing decorative images that should have alt=""

Practise it in code

Interviewers rarely stop at the definition. These questions open in an editor with tests.

  1. Design image delivery for an e-commerce site: responsive images, AVIF and WebP, CDN transforms, the LCP image and placeholdersHard55 min
  2. Diagnose and fix a poor LCP on a product pageHard45 min
  3. Simulate how the browser picks an image from srcset and sizes: width and density descriptors and DPRMedium35 min
  4. Fix layout shifts from ads, web fonts, images and late bannersHard40 min
  5. Build a lazy image loader using IntersectionObserverMedium35 min