Choosing and Preparing Images for the Web
Pick the right format, resize and compress sensibly, and add the markup that keeps your pages fast, sharp and accessible on every screen.
Key takeaways
- Use JPEG, WebP or AVIF for photographs, PNG or lossless WebP for screenshots and sharp edges, and SVG for logos and simple drawings.
- Resize images to the largest size they will be displayed, allowing extra pixels for high-density screens.
- Set
widthandheighton every image so the page doesn't jump while it loads. - Offer several sizes with
srcset, and lazy-load only the images that start below the visible area. - Keep your original files, check your rights to each picture and remove location metadata before publishing.
A web-ready image is in a format suited to its content, no larger in pixels than the page can use, compressed until just before the damage shows, and described in text for people who can't see it. Do those four things and your pages load faster and look better, with no special software.
The usual mistake is uploading a photo straight from a camera or phone. It may be several thousand pixels wide and many megabytes in size, then displayed in a column a fraction of that width. Your visitor downloads all of it anyway.
Decide whether the image earns its place
Every image costs download time and attention. It earns that cost when it shows the product, explains a process, proves something happened or helps people recognize a place. A generic stock photo of smiling people at a laptop usually adds weight without information.
Original pictures tend to be more useful than polished generic ones. A clear photo of your actual storefront helps someone find it. A screenshot of the exact settings page answers a question that a paragraph can't.
Check that you may use the picture. Finding an image through a search engine gives you no license to publish it. Use your own photos, images you've licensed, or works whose license permits your use, and follow any attribution terms. Rules vary by country and license, so read the terms where you got the file. If you need to trace where a picture came from, reverse image search can help.
Choose a format
Formats differ in whether they throw away detail (lossy) or keep every pixel (lossless), whether they support transparency, and whether they describe pixels or shapes.
| Format | Compression | Transparency | Good for |
|---|---|---|---|
| JPEG | Lossy | No | Photographs, the safe fallback |
| PNG | Lossless | Yes | Screenshots, text, flat graphics |
| WebP | Lossy or lossless | Yes | Replacing JPEG and PNG in current browsers |
| AVIF | Lossy or lossless | Yes | Photos where small size matters most |
| SVG | Vector shapes | Yes | Logos, icons, diagrams |
| GIF | Lossless, 256 colors | Basic | Very simple animations |
SVG is a text file that describes lines and shapes, so it stays sharp at any size. It is a poor choice for photographs. Animated GIFs are often far larger than the same clip as a short video, so consider an MP4 or WebM file for motion.
For a closer comparison of the pixel formats, read image formats for speed.
Resize before you upload
Find the widest space the image will fill on your site. If your content column is 800 CSS pixels wide, an image 1600 pixels wide gives a high-density screen two device pixels for each CSS pixel. Going far beyond that adds bytes few people can see.
Crop first. Removing empty sky or cluttered edges makes the subject clearer and shrinks the file. Then resize, using the image resizer and cropper for single files or the bulk image resizer for a folder of product shots. The aspect ratio calculator helps you keep a set of pictures at consistent proportions.
Avoid scaling small images up. Enlarging adds pixels but no detail, and the result looks soft.
Compress until just before it shows
Lossy formats have a quality setting. Lowering it shrinks the file, slowly at first in terms of visible change, then with obvious blotches, color banding and halos around edges. The right setting depends on the picture, so judge by eye at normal viewing size.
A practical method:
- Start from your resized original, never from an already compressed copy.
- Export at a fairly high quality and note the size.
- Lower the setting in steps and compare side by side.
- Stop one step above where you notice damage in faces, text or smooth gradients.
The image compressor and WebP converter let you try this quickly. For vector files, the SVG optimizer strips editor clutter that design programs leave behind.
Quality numbers aren't comparable across formats or programs. A setting of 70 in one encoder may look quite different from 70 in another.
Mark images up properly
Good files still need good HTML. Here is a solid pattern for a content photo:
<picture>
<source type="image/avif" srcset="bakery-800.avif 800w, bakery-1600.avif 1600w" sizes="(max-width: 800px) 100vw, 800px">
<source type="image/webp" srcset="bakery-800.webp 800w, bakery-1600.webp 1600w" sizes="(max-width: 800px) 100vw, 800px">
<img src="bakery-800.jpg" width="800" height="533" alt="Bakery storefront with a blue awning" loading="lazy">
</picture> The browser takes the first source whose format it supports. srcset lists available widths, and sizes tells the browser how wide the image will be laid out, so it can choose a file before it has finished working out the page. The final img is the fallback and carries the attributes.
width and height give the browser the proportions in advance, so it reserves space and the text doesn't leap downward when the picture arrives. Your CSS can still make the image flexible with max-width: 100% and height: auto, a staple of responsive design.
loading="lazy" defers images until the visitor scrolls near them. Leave it off the main image at the top of the page, because delaying the most visible thing makes the page feel slower. You can add fetchpriority="high" to that one image to ask the browser to fetch it early.
Write alt text and file names
The alt attribute replaces the image for screen reader users and appears when an image fails to load. Describe what matters in context. For a product photo, name the product and the visible feature. For a chart, state the conclusion and put the numbers in nearby text or a table. For a purely decorative flourish, use an empty alt="" so assistive technology skips it. More examples are in writing useful alt text.
File names help you and, modestly, search engines. blue-ceramic-mug-front.jpg beats IMG_4821.jpg. Use lowercase letters and hyphens, with no spaces, which avoids encoding oddities in addresses.
Remove metadata you don't mean to share
Photos can carry EXIF metadata: the camera model, capture time and sometimes GPS coordinates. A picture taken at home may reveal where home is. Check files with the EXIF reader and clean them with the photo metadata remover. Some content systems strip this automatically and others don't, so test with one upload.
Watch the visible content too. License plates, documents on a desk, name badges and screens in the background can expose more than you intended. Cover sensitive areas with a solid block, since light blurring of text can sometimes be reversed.
Serve images efficiently
Once files are lean, delivery matters. Long cache lifetimes let returning visitors reuse images, as long as you give a changed picture a new file name or version string. See browser caching and cache headers. A content delivery network serves copies from locations nearer your visitors, and some will resize and convert formats for you.
Pictures shared on social networks and chat apps use a separate preview image declared in your page's metadata. Plan it deliberately, as described in favicons and social preview images.
Set up a repeatable workflow
Image quality across a site drifts when every upload is handled differently. Write down a few rules that anyone adding content can follow in a minute or two:
- The standard widths and proportions for each spot, such as article headers, product shots and thumbnails.
- The export format and a starting quality setting for photos and for screenshots.
- A file naming pattern, and where untouched originals are stored.
- Who writes the alt text, and when. The person choosing the picture usually knows why it is there.
- A rough size ceiling per image that prompts a second look when exceeded.
Many content systems generate smaller sizes automatically on upload and add srcset for you. Check the page source to see what yours actually produces before adding a plugin that duplicates it. Automation can also be set too aggressively, so inspect a few results, especially product photos where texture and color matter to buyers.
Screenshots need their own care. Capture only the window or panel you're discussing, hide personal details and keep the browser zoom consistent. A screenshot shrunk until its labels are unreadable helps nobody, so crop tightly instead, and repeat the important setting names in the text. That also keeps the instructions useful after the interface changes.
What can go wrong
Blurry images usually mean the file is smaller than its display size or has been compressed repeatedly. Stretched images mean the declared proportions don't match the file. Sideways photos can happen when a tool drops the orientation tag without rotating the pixels, so rotate first and then strip metadata. Colors that shift after export may come from a wide-gamut color profile being discarded; converting to sRGB before export is the safe choice.
If a page is still slow, open your browser's developer tools, reload with the network panel showing, and sort by size. The largest offenders are usually easy to spot, and fixing three or four often does more than polishing a hundred small icons.