# Optimize images for the web

Resize and compress images the right way: suitable sizes, WebP, AVIF or JPEG, quality, responsive images and lazy loading.

Web page: https://skriptbox.com/en/guides/optimize-images-for-the-web

A photo straight from a camera or smartphone is often 4,000 pixels wide and several megabytes in size. On a website it is rarely displayed wider than 1,200 pixels. Everything beyond that is dead weight that every visitor has to download – including on mobile, with a limited data plan.

This guide explains which dimensions and formats suit which purpose, how to choose a sensible quality setting and how to embed images so the browser can display them quickly and without layout jumps.

## Why image size matters

On many pages, images make up most of the transferred data. The most important one is the largest element visible while the page loads, usually a header image: how quickly it appears is measured by **Largest Contentful Paint (LCP)**, one of the three Core Web Vitals. Google recommends an LCP of 2.5 seconds or less, and an oversized hero image is one of the most common reasons a page misses that target.

Data usage matters too: anyone browsing on a throttled mobile plan notices every unnecessary megabyte. Smaller images also save storage and bandwidth on your server.

## Choosing the right pixel dimensions

Base the image width on how wide it is actually displayed on the page. For high-resolution displays (smartphones, Retina screens), twice that width – **2×** – is worthwhile. Going beyond 2× hardly makes a visible difference but costs considerably more data.

| Use | Typical display width | Sensible file width |
| --- | --- | --- |
| Full-width hero image | up to approx. 1,400 px | 1,600–2,400 px, plus smaller variants for mobile devices |
| Image within body text | 600–800 px | 1,200–1,600 px |
| Thumbnail | 150–300 px | 300–600 px |

These are guidelines – if in doubt, use your browser’s developer tools to check how wide the image really is in your layout. With the [Resize & Convert Images](https://skriptbox.com/en/tools/image-resizer) tool you set a maximum width and height; the aspect ratio is preserved, and you can process several images in one go.

## Formats compared

| Format | Suitable for | Transparency | Browser support |
| --- | --- | --- | --- |
| JPEG | Photos where maximum compatibility is needed | no | everywhere |
| PNG | Screenshots, graphics with sharp edges, lossless images | yes | everywhere |
| WebP | Photos and graphics, usually noticeably smaller than JPEG and PNG | yes | all current browsers |
| AVIF | Photos with very good compression, even at small file sizes | yes | all current browsers (since 2024) |
| SVG | Logos, icons, diagrams – vector graphics that scale to any size | yes | everywhere |

For most photos, **WebP** is a safe choice today: it is supported everywhere and is usually smaller than JPEG at the same quality. **AVIF** often compresses even better, but encoding takes longer and not every tool can do it. The tool on this website produces WebP, JPEG and PNG; to create AVIF files, use an image editor or your build process. Logos and icons should be embedded as SVG wherever possible.

### Setting the quality

Lossy formats such as JPEG, WebP and AVIF have a quality setting. For photos on websites, a value of around **75 to 85 %** is a good starting point; differences from the original are usually invisible. Below that, depending on the subject, block artifacts and smeared areas start to show, especially in fine gradients such as skies or skin tones. Above 90 %, the file grows considerably without any visible improvement.

Always check the result at its actual display size. The [Resize & Convert Images](https://skriptbox.com/en/tools/image-resizer) tool shows the file size before and after for each image, so you can quickly compare different settings. PNG is lossless – there is no quality setting, and only the pixel dimensions have a significant effect on file size.

## Embedding responsive images

With `srcset` and `sizes` you offer the browser several sizes, and it picks the appropriate file based on screen width and pixel density. With `<picture>` you can additionally offer modern formats with a fallback:

```
<picture>
  <source type="image/avif"
          srcset="team-800.avif 800w, team-1600.avif 1600w"
          sizes="(min-width: 900px) 800px, 100vw">
  <source type="image/webp"
          srcset="team-800.webp 800w, team-1600.webp 1600w"
          sizes="(min-width: 900px) 800px, 100vw">
  <img src="team-800.jpg"
       srcset="team-800.jpg 800w, team-1600.jpg 1600w"
       sizes="(min-width: 900px) 800px, 100vw"
       width="1600" height="1067"
       alt="Our team outside the office"
       loading="lazy" decoding="async">
</picture>
```

The browser uses the first `<source>` whose format it understands; the `<img>` is the fallback and carries the attributes for size, alternative text and loading behavior. MDN covers the details in [Responsive images](https://developer.mozilla.org/en-US/docs/Web/HTML/Guides/Responsive_images).

### width and height against layout shifts

Specify `width` and `height` on every image. The browser uses them to work out the aspect ratio and reserves the space before the image has loaded. Without them, the text jumps down as the image arrives – which is what **Cumulative Layout Shift (CLS)** measures. The values only need to match the aspect ratio; your CSS still determines the actual display size, for example with `max-width: 100%; height: auto;`.

### Lazy loading and priority

With `loading="lazy"`, the browser only loads images once they get close to the visible area. That is ideal for images further down the page. Do **not** use it on the **LCP image** or on any other image that is visible straight away – otherwise the browser waits unnecessarily. Instead, mark the hero image with `fetchpriority="high"` so it is loaded ahead of less important resources:

```
<img src="hero-1600.webp" width="1600" height="900"
     alt="View across the harbor" fetchpriority="high">
```

For background, see the web.dev article [Optimize Largest Contentful Paint](https://web.dev/articles/optimize-lcp).

## Removing metadata

Photos contain EXIF metadata: camera model, time of capture and, on smartphones, often the **GPS location**. If you publish an unedited photo taken in your garden, it may well contain your home address. Remove metadata before uploading unless you deliberately want to show it. As a side effect, the file gets slightly smaller.

When you convert an image with [Resize & Convert Images](https://skriptbox.com/en/tools/image-resizer), only the pixels are carried over – EXIF data, including the GPS position, is dropped. The orientation of phone photos is applied first, so the image still appears the right way up. The images are processed in your browser only and are not uploaded.

## Base64 and data URIs: only for very small graphics

An image can be written directly into HTML or CSS as a data URI, for example `background-image: url("data:image/svg+xml;base64,…")`. That saves a separate request but has drawbacks:

- Base64 makes the data about a third larger.
- The image is not cached separately; it is downloaded again whenever the surrounding file changes.
- Large data URIs bloat HTML or CSS and therefore delay rendering of the entire page.

This only makes sense for very small graphics of a few hundred bytes up to roughly 1–2 KB, such as a small icon or a placeholder that has to be visible immediately. With SVG you can often skip Base64 altogether by inserting the URL-encoded SVG code. Photos, on the other hand, always belong in separate files. If you want to convert a small graphic, the [Base64 encoder](https://skriptbox.com/en/tools/base64-encoder) generates the right code from a file.

## Checklist

- Resize images to their actual display width, at most 2× for high-resolution displays.
- Photos as WebP or AVIF with JPEG as a fallback, logos and icons as SVG.
- Quality around 75–85 %, and check the result at full size.
- Use `srcset` and `sizes` to provide several sizes.
- Specify `width` and `height` on every image.
- `loading="lazy"` for images further down, `fetchpriority="high"` for the LCP image.
- Remove EXIF data, especially GPS, before publishing.
- Data URIs only for very small graphics.
- Don’t forget meaningful `alt` text.


## Tools for this topic

- [Resize & Convert Images](https://skriptbox.com/en/tools/image-resizer.md) – Resize photos for free and convert them to WebP, JPG or PNG – no upload, right in your browser.
- [Base64 Encoder & Decoder](https://skriptbox.com/en/tools/base64-encoder.md) – Encode and decode text and files as Base64 – UTF-8, base64url and data URIs for CSS and HTML, with validation and preview. No upload.
