IHOR.DEVKNOWLEDGE_BASE
MENU

~/blog / Web Quality / image-optimization-for-modern-websites.md

// guide · 4 min read

Image Optimization for Modern Websites

Choose responsive image sizes, reserve layout geometry, prioritize the actual hero, and verify Next.js image requests without damaging screenshots or accessibility.

Quick answer

Optimize images for their displayed job: a hero needs early discovery, a gallery needs responsive candidates, and a UI screenshot needs legible text. Reserve the image's layout space, describe its actual rendered width with sizes, and inspect which file the browser downloads at representative viewport widths. Reducing JPEG quality alone cannot fix an oversized candidate or a hero discovered late through JavaScript.

Measure the current request and visible quality before changing formats. A photographic preview and a screenshot with small labels tolerate compression differently. Keep the original asset available for regeneration; repeated lossy recompression can accumulate damage. Avoid presenting an image change as a measured performance improvement until the relevant trace supports it.

Practical implementation steps

Match dimensions to layout

Determine the actual CSS slot at mobile, tablet, and desktop widths. A card occupying half the desktop content area should not advertise a full-viewport slot. Width and height provide an intrinsic ratio; fill requires a positioned parent with defined geometry. Keep object-fit behavior intentional so cropping does not cut off the very UI the screenshot is meant to explain.

Prioritize one confirmed critical image

Identify the LCP element in the affected route before adding preload or high fetch priority. Keep below-the-fold gallery images lazy when appropriate. Preloading many images competes with the real hero and critical styles. Next.js 16 provides preload and current loading controls; check the installed version's guidance instead of copying older priority-only examples.

Keep semantics separate from performance

Write alt text for the visible information, not file names or keyword variants. Decorative images need an empty alternative when adjacent content supplies their meaning. A screenshot caption can explain context that does not fit into concise alt text. Performance optimization does not justify removing meaningful screenshots or replacing readable text with an uninformative blur.

Verify the downloaded candidate

Use the network panel and image currentSrc at representative viewports and device pixel ratios. Compare displayed width, fetched pixel dimensions, response size, and cache behavior. Check the production image optimizer and remotePatterns if using external assets. Restrict remote sources to intended hosts and paths rather than allowing arbitrary user-supplied URLs.

Implementation example

This illustrative Next.js gallery image uses explicit dimensions to reserve its ratio and sizes to describe a capped content column. The asset and dimensions must be replaced with real values. The portfolio's extension gallery uses this same kind of dimension-and-slot model, while project visuals use fill with sized containers.

import Image from "next/image";

export function ProductScreenshot() {
  return (
    <Image
      src="/screenshots/product-panel.png"
      alt="Product panel showing saved items and export controls"
      width={1280}
      height={800}
      sizes="(max-width: 820px) calc(100vw - 48px), 760px"
    />
  );
}

This is a below-the-fold screenshot, so it does not request preload. If the same asset becomes the hero, verify its LCP role and choose the loading behavior for that context. Reusing an asset does not mean every occurrence should share the same priority.

Tradeoffs and alternatives

Modern formats can reduce transfer size, but screenshot readability and transparency may favor a different encoding from photos. Automated optimization simplifies delivery but introduces an optimizer and caching path to verify. Static variants can be appropriate for controlled assets and hosting constraints. Choose based on measured bytes, visible quality, and operational fit.

Common mistakes

  • Advertising 100vw for a narrow desktop card.
  • Using fill without a stable parent size.
  • Lazy loading the confirmed LCP hero.
  • Preloading every gallery item.
  • Compressing interface screenshots until labels are unreadable.

Production checklist

  • Slot sizes match the CSS layout.
  • Geometry remains stable before loading.
  • Critical loading choices follow an LCP trace.
  • Mobile and high-DPR candidates were inspected.
  • Alt text and captions describe the visible content.
  • Screenshots remain legible at their displayed size.

FAQ

Are width and height the displayed size?

They establish intrinsic dimensions and ratio; CSS controls the rendered slot. The sizes hint should describe that rendered slot so the browser can choose an appropriate candidate.

Should all images use the smallest file possible?

Use the smallest file that preserves the image's purpose at the required dimensions. A screenshot that hides its controls is no longer doing its job, even if its transfer size is impressive.

Conclusion

Fix layout hints and discovery before chasing compression ratios. Verify the selected resource in a production-like browser, then assess both transfer cost and legibility. The useful result is a stable, readable image delivered at the size the layout needs.

Primary sources

Ihor Kharchyshyn

Independent full-stack product engineer writing source-backed implementation guides for browser extensions and modern web products.

Need this implemented in a real product?

One connected implementation across the interface, application logic, APIs, and data layer.