Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Optimize website images by matching their pixel dimensions to their rendered size, choosing an encoding suited to the image, serving responsive candidates, reserving layout space, and loading each image according to its importance. Keep the likely Largest Contentful Paint (LCP) image discoverable and eager; lazy-load images farther down the page. Then measure the actual page to verify the trade-offs.

1. Size image files for how they appear on the page

Start with the image’s rendered role, not the largest dimensions you can export. If a page displays an image in a 500-by-500-pixel box, a 500-by-500 source is appropriately sized when other variables are held constant; a 1000-by-1000 source has twice the width and height needed for that display. Larger sources may be justified by high-density displays or different layouts, but oversized files can waste download bytes.

For an image whose display width changes by viewport, provide a useful set of candidates and tell the browser the expected rendered width. The browser can select a candidate using the layout information and device needs. A desktop page may show the image in a narrow column while a phone shows it full-width, so the widest screen does not automatically need the largest candidate.

<img
  src="photo-800.jpg"
  srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1200.jpg 1200w"
  sizes="(min-width: 66em) 33vw, (min-width: 44em) 50vw, 100vw"
  width="1200"
  height="800"
  alt="Describe the meaningful image content"
>

The w descriptors state each candidate’s intrinsic width. With width descriptors, sizes describes the expected rendered width; it does not replace the CSS that defines the layout. Adjust the candidate widths and sizes conditions to match the actual page, and keep the declared aspect ratio consistent with the image files.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option

When a single source is enough

If an image always renders at approximately one size and the source is already close to the needed dimensions, a responsive candidate set may add complexity without meaningful benefit. Revisit that decision when the layout changes substantially across screen sizes or the same asset serves very different display roles.

2. Choose a format and compression setting by image content

MDN’s general guidance is to prefer WebP or AVIF for raster images because they generally compress better than PNG, JPEG, and GIF. That is a useful starting point, not a guarantee that every conversion produces a smaller file or acceptable appearance. Compare the encoded result at the size visitors will see.

Photographs and continuous tones

Lossy compression discards image information to reduce file size. Photographs and other continuous-tone images can often tolerate some loss, but inspect gradients, fine detail, and faces at the intended display size. The appropriate setting depends on the image and acceptable visual quality; no single quality value is established for every image.

Screenshots, diagrams, logos, and line art

Sharp boundaries and text make artifacts conspicuous. MDN advises lossless encoding for screenshots, diagrams, logos, and line art, where blur or colored fringes around edges can be visible. WebP and AVIF can be encoded using lossless or lossy approaches, so selecting one of those formats does not by itself determine the compression behavior.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Fallbacks where browser support requires them

Use <picture> when you want to offer a modern format and retain a fallback. Put the preferred supported source first, then the fallback image. The browser uses the first source it can render.

<picture>
  <source srcset="diagram.avif" type="image/avif">
  <source srcset="diagram.webp" type="image/webp">
  <img src="diagram.png" width="1200" height="800" alt="A labeled system diagram">
</picture>

Whether a fallback is needed depends on the browser support your audience requires. Check current browser support and inspect the result rather than assuming every visitor can use the same format.

3. Prevent layout movement while images load

Give an image element its intrinsic width and height, even when CSS makes it responsive. The browser can use those values to reserve the image’s aspect ratio before its bytes arrive, reducing image-driven layout shifts.

img {
  max-width: 100%;
  height: auto;
}

Use dimensions that correspond to the image’s real aspect ratio. If an image is cropped to a different ratio in the layout, reserve the space for that displayed shape using an appropriate layout or aspect-ratio rule; otherwise, the browser may reserve the wrong amount of space.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

4. Load images according to position and importance

Below-the-fold images

For images well below the initial viewport, add loading="lazy". This lets the browser defer fetching them until they approach the viewport.

<img src="article-detail.webp" width="900" height="600" loading="eager" alt="Close-up detail of the finished project">

Above-the-fold and likely LCP images

Do not lazy-load the image likely to be the page’s LCP element. Lazy loading can delay discovery and harm LCP. For a prominent image in the initial viewport, leave loading at the default eager behavior unless measurement gives you a reason to do otherwise.

You may use fetchpriority="high" for a genuinely important image, such as the likely LCP image. Use it selectively: giving high priority to many images can make prioritization ineffective or compete with scripts and fonts. First check whether the key image is in the initial HTML and starts loading promptly.

Images discovered late

A preload can help when a crucial image is otherwise discovered late, for example when it is referenced in CSS. Use preloads sparingly. A format-specific preload can cause a duplicate download if the browser later selects another source, so confirm which resource the browser actually fetches.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #4
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers

5. Measure the page, not just the image file

Compare the page under the layouts and device conditions that matter to your audience. A smaller image file is not automatically a better result if it looks visibly worse, arrives too late, or forces a poor responsive choice. For a likely LCP image, check whether it appears in the initial HTML, whether it begins loading promptly, whether it was accidentally marked lazy, and whether a priority hint helps.

web.dev states a good LCP target as 2.5 seconds or less for at least 75% of page visits. That is a user-experience target, not a promise that image optimization alone will achieve it. Image delivery is one part of page performance; compare actual results rather than inferring LCP from a format conversion.

Balance variants against operational cost

More widths and formats can help different layouts and browsers download a better-fit asset, but they also add storage, generation, markup, and cache complexity. A full-width hero and a small product-list thumbnail may need different candidate sets. An image CDN can automate device- and browser-aware delivery, but it is optional, not a requirement for every site. Choose a manageable set and refine it using measurements.

6. Troubleshoot common image optimization problems

The browser downloads an unexpectedly large candidate

  • Likely cause: The sizes value overstates the rendered width, the candidate set lacks a suitable intermediate size, or the page’s CSS differs from the assumed layout.
  • Fix: Check the actual rendered width at relevant breakpoints, update sizes, and add a candidate only if the measured layouts need it. Remember that sizes describes expected display width; it does not set CSS width.

The image looks soft or has halos around text

  • Likely cause: The selected candidate is too small for its display size or lossy compression damaged sharp edges.
  • Fix: Verify the chosen source and rendered dimensions. For text, diagrams, screenshots, logos, or line art, compare a lossless version and inspect it at display size.

The page shifts when the image appears

  • Likely cause: The browser cannot infer the intended aspect ratio before the image loads, or the declared dimensions do not match the displayed crop.
  • Fix: Supply accurate intrinsic dimensions or reserve the correct aspect ratio in the layout. Keep responsive CSS from distorting that ratio.

The LCP image appears late

  • Likely cause: It is marked lazy, is discovered only after CSS or script execution, or competes with too many high-priority resources.
  • Fix: Keep the likely LCP image eager and discoverable in initial HTML. Consider a selective priority hint; use preload only when the image is otherwise discovered late, then check for duplicate requests.

A modern format conversion does not improve the page

  • Likely cause: The particular file did not compress better at the needed quality, the image is not the page’s main bottleneck, or delivery and discovery remain poor.
  • Fix: Compare file size and appearance for the actual asset, then assess page loading and LCP. Do not treat a format name as proof of a speed improvement.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

If your task is capturing a web page as an image or PDF rather than optimizing the images inside your own site, ScreenshotNeo is a website screenshot API and MCP server. One GET request can return a PNG, JPEG, WebP, or PDF. Cookie banners are accepted and more than 60 known consent platforms, newsletter popups, and chat widgets are removed before capture; each cleanup step can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits cost nothing, with response headers identifying the page verdict and billing status. Its MCP server gives AI agents tools for screenshots, page information, and PDF capture. The free plan includes 1,000 screenshots a month without a card; paid plans start at $5 for 3,000.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

cURL example, with the endpoint and parameters documented at ScreenshotNeo’s API documentation:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

Replace YOUR_API_KEY with your API key and set the target URL to the page you want to capture. For a webpage screenshot, that is a direct capture workflow; it does not replace optimizing the original image assets served by your own site. Sign up for 1,000 free screenshots a month with no card.

Frequently Asked Questions

Does converting every image to WebP or AVIF guarantee faster loading?

No. Compare the encoded size and visual quality for each asset, and measure the page; the result depends on content, settings, browser support, and delivery.

Should I lazy-load the page’s hero image?

Usually not if it is the likely LCP image. Keep it discoverable and eager, then measure whether a selective priority hint is warranted.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Do I need an image CDN to optimize website images?

No. A CDN can automate transformations and device- or browser-aware delivery, but responsive markup and appropriately encoded assets can be implemented without one.

Quick Recap

SaleBestseller No. 1
HTML and CSS: Design and Build Websites
HTML and CSS: Design and Build Websites
HTML CSS Design and Build Web Sites; Comes with secure packaging; It can be a gift option
$15.74
SaleBestseller No. 3
SaleBestseller No. 4
Web Design with HTML, CSS, JavaScript and jQuery Set
Web Design with HTML, CSS, JavaScript and jQuery Set
Brand: Wiley; Set of 2 Volumes
$35.05

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.