In short
- Images are usually the heaviest part of a web page, so optimising them is the most effective speed improvement.
- Resize first: an image should be no larger than the biggest size it is displayed at.
- Use AVIF or WebP for photographs and SVG for logos and icons. Keep JPEG or PNG as a fallback only where needed.
- Responsive images let a phone download a small file and a large monitor a large one.
- Lazy load images below the first screen, always declare the width and height, and write alt text for anything that carries meaning.
A photograph straight from a modern phone or camera is several megabytes. Displayed on a website, the same picture can look identical at a small fraction of that size. The gap between the two is the single largest cause of slow websites, and closing it is mostly a matter of routine.
Image optimisation means delivering each image in the right format, at the right dimensions, with the right amount of compression, at the right moment. This guide takes those four in turn.
Choosing the Right Format
| Format | Best for | Notes |
|---|---|---|
| AVIF | Photographs and complex images | Usually the smallest files at a given quality. Supported by all current browsers. |
| WebP | Photographs and graphics | Smaller than JPEG and PNG, with transparency and animation. Supported everywhere. |
| JPEG | Photographs, as a fallback | The old standard. Larger than the modern formats, but it works in everything. |
| PNG | Screenshots and graphics that need exact detail or transparency | Lossless, so the files are large. Rarely the right choice for photographs. |
| SVG | Logos, icons and illustrations | Drawn from shapes, not pixels, so it stays sharp at any size and is often tiny. |
| GIF | Almost nothing now | Animated GIFs are very large. A short video file is far smaller. |
For most websites the practical rule is short: SVG for logos and icons, and AVIF or WebP for everything else. Newer formats continue to appear, and browser support for JPEG XL is spreading, but AVIF and WebP are the safe choices today.
<picture>
<source srcset="team.avif" type="image/avif">
<source srcset="team.webp" type="image/webp">
<img src="team.jpg" alt="The workshop team beside a finished staircase"
width="1200" height="800" loading="lazy">
</picture>Resize Before Anything Else
The most common mistake is uploading an image far larger than it will ever be shown. A photograph 4,000 pixels wide, placed in a column 800 pixels wide, makes every visitor download many times more data than their screen can use.
Work out the largest size at which the image appears. Allow for high-density screens, which use two device pixels for each CSS pixel, so an image shown 800 pixels wide looks sharpest from a file about 1,600 pixels wide. Beyond double, the extra detail is invisible.
| Use | Typical display width | Sensible file width |
|---|---|---|
| Full-width hero | The whole screen | 1,600 to 2,400 pixels, with smaller versions for phones |
| Image in an article column | 700 to 800 pixels | About 1,600 pixels |
| Card or thumbnail | 300 to 400 pixels | About 800 pixels |
| Logo | Any | SVG, so there is no fixed size |
Compression
Compression removes data the eye will not miss. There are two kinds. Lossless compression keeps every pixel identical and makes modest savings. Lossy compression discards fine detail and can reduce a photograph to a fraction of its size.
- For photographs, use lossy compression. A quality setting somewhere around 70 to 80 for WebP, or lower for AVIF, is usually indistinguishable from the original at normal viewing sizes.
- Compare the result with the original at the size it will be shown, not zoomed in.
- Compress flat graphics and screenshots losslessly, or redraw them as SVG.
- Strip metadata such as camera settings and location, which adds weight and can reveal more than you intend.
- Automate it. Most modern site builds and image services resize, convert and compress on upload.
Responsive Images
A single file cannot suit both a phone and a large monitor. Responsive images let you offer several sizes and let the browser choose, based on the width of the screen and its pixel density. They are a central part of responsive web design.
<img src="kitchen-800.webp"
srcset="kitchen-400.webp 400w,
kitchen-800.webp 800w,
kitchen-1600.webp 1600w"
sizes="(min-width: 60rem) 50vw, 100vw"
alt="A fitted kitchen with oak worktops"
width="1600" height="1067" loading="lazy">sizes attribute says that the image fills half the screen on wide layouts and all of it on narrow ones, and the browser picks the smallest file that will look sharp.When the picture needs to be cropped differently on a phone, and not just scaled, the picture element lets you supply a different image for each layout, as shown in our guide to hero section design.
Lazy Loading
A browser normally downloads every image on a page as soon as it finds it, including those far below the first screen that the visitor may never reach. Lazy loading postpones each image until it is about to scroll into view. It is built into every current browser and needs one attribute.
<!-- At the top of the page: load at once, with priority -->
<img src="hero.avif" alt="A terraced garden at dusk"
width="1600" height="900" fetchpriority="high">
<!-- Further down: load when needed -->
<img src="steps.avif" alt="Stone steps between planted borders"
width="800" height="600" loading="lazy" decoding="async">Tip: Never lazy load the main image at the top of a page. It delays the very thing that is measured for Largest Contentful Paint. Lazy loading is for images below the first screen.
Stop Images Shifting the Page
If the browser does not know an image's proportions, it cannot leave room for it. The text loads, the image arrives a moment later and everything below jumps down. That movement is what Cumulative Layout Shift measures.
The fix is to state the width and height of every image in the HTML. The browser uses them to work out the proportions, and reserves the right space at any screen width.
img {
max-width: 100%;
height: auto;
}
.card-image {
aspect-ratio: 4 / 3;
object-fit: cover;
width: 100%;
}Our guide to Core Web Vitals explains the measurements that images affect.
Alt Text and File Names
Optimisation is not only about weight. Alternative text describes an image to people who cannot see it and to search engines, and it is shown if the image fails to load.
- Describe what the image shows and why it is there, in a sentence: a carpenter fitting an oak door frame.
- Do not begin with the words image of or picture of. Screen readers already announce that it is an image.
- If an image is purely decorative, give it an empty
altattribute so that it is skipped. - If an image is a link or a button, the alt text should describe where it goes.
- Do not stuff it with keywords. Write for the person listening.
- Give files descriptive names, such as
oak-door-frame.avif, not the name the camera gave them.
Alt text is a basic requirement of web accessibility.
Background Images, Icons and Video
- CSS background images cannot use
srcsetorloading. Useimage-set()for formats and media queries for sizes, or use an ordinary image positioned behind the content. - Icons are best as inline SVG, which needs no extra download and can take its colour from the text.
- Animations should be short video files or CSS, not GIFs.
- Video should have a poster image, should not download until it is played and should never play sound without being asked.
- Image delivery services resize, convert and cache images automatically, and are worth considering for sites with many photographs.
An Image Workflow That Works
- Choose the format: SVG for graphics, and AVIF or WebP for photographs.
- Resize to twice the largest display width, and no more.
- Compress, and compare with the original at display size.
- Export two or three widths for responsive images.
- Write the alt text and give the file a descriptive name.
- Add the width and height, and lazy loading for anything below the first screen.
- Test the page and check its weight.
A well-built site does most of this automatically, so that a photograph uploaded by anyone on the team is resized and converted without further thought. That is worth asking about when you choose a website designer, and our guide to website speed optimisation covers what else affects loading time.
Putting It Into Practice
Reading about a technique is the easy part. Applying it to a real business, with real content and a launch date, takes judgement about what to use and what to leave out. DigitalPod is a UK-based design and web development studio that designs every site from a blank page and builds it by hand, so a technique is used because it helps the business and not because a template came with it. If you would like a website designer to handle it for you, startup website design is what we specialise in, websites start from £2,400 and our website redesign service brings an older site up to date.
Frequently Asked Questions
What is the best image format for a website?
For photographs, AVIF or WebP. Both are much smaller than JPEG at the same quality and are supported by every current browser. For logos, icons and simple illustrations, SVG is best, because it stays sharp at any size and the files are very small.
Is WebP or AVIF better?
AVIF usually produces smaller files at the same visual quality, particularly for photographs. WebP is slightly less efficient, but it encodes faster and has been supported for longer. Many sites offer AVIF first with WebP as a fallback, and let the browser choose.
What size should images be for a website?
No wider than about twice the largest width at which they are displayed. That is typically around 1,600 pixels for an image in an article, 800 pixels for a card and up to 2,400 pixels for a full-width hero, with smaller versions offered to phones.
What is lazy loading?
Lazy loading delays the download of an image until it is about to come into view, so that visitors do not fetch pictures they never scroll to. It is added by setting the loading attribute to lazy, and it should be used for images below the first screen, never for the main image at the top.
Does image optimisation help SEO?
It helps in two ways. Smaller images make pages load faster, which improves the Core Web Vitals that Google measures. And descriptive alt text and file names help search engines understand what an image shows, which matters for image search.