🎓 A1Learner
🔍

Home / Blog / Web Development / Performance

Image Optimization for the Web: Formats, Sizes, and Loading

Web Development · Performance · 7 min read

Learn how to resize, format, and load images so pages look sharp without making visitors wait.

A phone photo is not a web picture yet

A photo from a phone is built for printing and zooming, not for a web page. It can be far wider than the column it fills on screen. Uploading it unchanged means every visitor downloads pixels their screen will never show. The picture still looks the same. The wait is longer.

Image optimisation is the work of serving each picture in the smallest file that still looks right on the visitor's screen, at the right moment. That work has four parts. Resize the picture. Choose a fitting format. Offer several sizes. Load the visible pictures first. Each part is simple on its own. Together they decide whether pictures help a page or weigh it down.

Start with the display size. Measure how wide the picture appears on a large screen, not how wide the original file is. A picture shown at about half a page column needs a far smaller file than a full-width banner. Sharp phone screens show more detail, so allow a little extra width beyond the display size. A little extra is enough. Four times the display width is not.

Pick a format for the picture in front of you

An image format is the method used to store a picture file. Different formats suit different kinds of pictures. JPEG suits photographs and busy scenes with many colours. It stores a photo in a relatively small file by discarding fine detail the eye barely notices. That discarding is called lossy compression. Lossy means some data is removed for good, so keep the original file apart from the web copy.

PNG suits logos, screenshots, and pictures with sharp edges or transparent areas. Transparency means parts of the picture let the background show through. PNG stores those edges cleanly, but it is a poor choice for photographs. A photograph saved as PNG can weigh several times more than the same photograph saved as JPEG.

WebP is a newer general format. It handles photographs and sharp graphics, and it usually stores either in fewer bytes than JPEG or PNG at similar quality. AVIF is newer still and often smaller again for photographs, though it takes longer to encode. SVG is different from all of these. It is a vector format, which means it describes shapes with code instead of storing dots of colour. Use SVG for icons, simple logos, and diagrams. It stays sharp at any size and stays tiny. Never use it for a photograph.

Offer several sizes and let the browser choose

One file cannot serve every screen well. A phone on mobile data does not need the file a large desktop monitor needs. Sending the desktop file to the phone wastes data and time. Sending the phone file to the desktop looks soft. Responsive images solve this by listing several widths of the same picture and letting the browser pick.

The srcset attribute is the list of widths. Each entry pairs a file with its pixel width. The sizes attribute tells the browser how wide the picture will display at different screen widths. The browser then combines that display width with the sharpness of the screen and picks the smallest file that still covers it. Three to five widths cover most layouts. A thumbnail in a grid and a full-width banner need different sets, so build the list around the layout, not around one fixed set of numbers.

Getting sizes right matters as much as listing files. If sizes is missing, the browser assumes the picture fills the whole screen width. A small thumbnail then downloads a full-width file for no reason. Test the result on a phone and on a desktop. The phone should download a smaller file than the desktop for the same picture. That check takes a minute in a browser's network panel, which lists every file a page downloads and its size.

Load the top picture first and the rest later

Loading order matters as much as file size. A page has a first screen, the part visible before any scrolling. The main picture there is often the largest visible element. Largest Contentful Paint is the measurement of how long that main content takes to appear. It is one of Google's Core Web Vitals, a small set of measurements for loading, responsiveness, and visual stability. A slow main picture delays that measurement directly.

Give the main picture high priority. Do not lazy-load it. Lazy loading means delaying a picture until the visitor scrolls near it. That is the right move for pictures further down the page, because many visitors never reach them. Applied to the main picture, it adds a needless delay before the page looks complete. A single attribute marks a below-the-fold picture for lazy loading, and no script is needed for it in current browsers.

Pictures that sit in a carousel need extra care. A carousel is a rotating set of pictures where only one shows at a time. Loading every slide at once punishes the visitor for pictures they may never view. Load the first slide at once and the rest lazily, or replace the carousel with one strong picture when the extra slides add little. The same rule applies to long galleries. Show the first pictures, and let the rest wait for the scroll.

<img src="garden-800.jpg"
  srcset="garden-400.jpg 400w, garden-800.jpg 800w, garden-1600.jpg 1600w"
  sizes="(max-width: 700px) 100vw, 800px"
  width="1600" height="900" alt="A garden path in morning light">
One picture in three widths, with its display width described for the browser.

Hold the space so the page does not jump

A page jump is what happens when text moves down the moment a picture arrives. The visitor was reading one line and suddenly reads another. Worse, a button can shift just as a finger taps it. This jumping is measured as Cumulative Layout Shift, another Core Web Vital. It is a stability problem, not a speed problem, but it feels like slowness because the page cannot be trusted until it settles.

The fix is to reserve the picture's space before it downloads. The width and height attributes on an image tag give the browser the picture's proportions. The browser then holds a box of the right shape while the file travels. Styles can still scale the picture to fit its column. The proportions are what matter, not the raw pixel counts.

An aspect ratio is the same idea in style code. It states the width-to-height shape, such as sixteen units wide to nine tall. Use it for pictures whose size is set by layout rather than by fixed attributes. The rule stays the same either way. Every picture that loads late needs a reserved shape. A picture with no reserved shape is a jump waiting to happen.

A short routine for every new picture

A routine beats a rescue. Resize the picture to the width it will display, with a little extra for sharp screens. Choose the format by the picture in front of you: JPEG or a newer format for a photograph, PNG for sharp edges and transparency, SVG for an icon or diagram. Compress the web copy and keep the original somewhere safe. Compression cannot be undone, so the original is the only way back.

Then build the responsive set and add the dimensions. List a few widths with srcset and describe the display width with sizes. Add width and height so the layout holds still. Mark below-the-fold pictures for lazy loading and leave the main picture eager, which means it loads at once. Finally, check the page by file size, not by eye. The network panel shows which picture still weighs too much. Fix that one before publishing, and the next upload starts from the same routine.

Questions readers ask

Will compressing my images make them look worse?

Not at sensible settings. Shrinking a picture to the width it displays at rarely changes how it looks on screen. Damage comes from the opposite mistake: stretching a small picture larger than its real size. Keep the original file somewhere safe, and compress a copy for the web.

Should I lazy-load every image on the page?

No. Lazy-load only the pictures below the first screen. The main picture at the top should load at once, with high priority. Delaying it delays the moment the page looks complete, which is the exact delay visitors notice first.

Should I use WebP or AVIF?

WebP is the safer everyday choice. It is smaller than JPEG or PNG at similar quality and works in current browsers. AVIF is smaller again for many photographs, but it takes longer to create. Offer AVIF first with WebP as a fallback when your tooling makes that easy.

Do I need a CDN for my images?

A CDN helps when visitors live far from your main server, because it serves files from a nearer copy. It does not shrink a heavy picture. Resize and compress first. A CDN delivers a heavy file faster, but the file is still heavy.