Images are usually the heaviest thing on a page, so serving them well is the biggest performance win you can make in HTML. This chapter is about choosing the right format and shipping an appropriately-sized image to each device instead of one giant file for everyone.
Image formats
| Format | Best for |
|---|---|
WebP / AVIF | Modern default — great quality, small files |
JPG | Photographs (wide support fallback) |
PNG | Graphics needing transparency or sharp edges |
SVG | Logos & icons — vector, scales infinitely |
GIF | Avoid — use video for animation |
Responsive images: srcset
srcset offers the browser several sizes of the same image and lets it pick the best for the screen and resolution — a phone downloads a small file, a retina desktop a large one:
<imgsrc="hero-800.jpg"srcset="hero-400.jpg 400w, hero-800.jpg 800w, hero-1600.jpg 1600w"sizes="(max-width: 600px) 100vw, 800px"alt="Students at demo day"width="800" height="500">
Art direction: picture
Use <picture> when you need different images (not just sizes) per situation — a cropped square on mobile, a wide banner on desktop, or a modern format with a fallback:
<picture><source srcset="hero.avif" type="image/avif"><source srcset="hero.webp" type="image/webp"><img src="hero.jpg" alt="…"> <!-- fallback for old browsers --></picture>
Loading & layout shift
loading="lazy"defers off-screen images until the user scrolls near them.- Always set
widthandheight(or CSSaspect-ratio) so the browser reserves space and the page doesn't "jump" as images load — a real ranking factor. - Add
decoding="async"to avoid blocking rendering while the image decodes.
<img src="card.jpg" alt="…" width="400" height="300"loading="lazy" decoding="async">
Practice
Take a hero image and provide three sizes via srcset with a sizes hint, wrap it in a <picture> with a WebP source and a JPG fallback, and add width/height plus loading="lazy". Compare the bytes downloaded on a narrow vs wide window.
Key takeaways
- 01Prefer WebP/AVIF; JPG for photos, PNG for transparency, SVG for logos/icons.
- 02srcset offers multiple sizes; sizes tells the browser the display width so it picks well.
- 03<picture> serves genuinely different images or modern formats with a fallback.
- 04loading="lazy" defers off-screen images; decoding="async" avoids blocking.
- 05Always set width/height (or aspect-ratio) to prevent layout shift as images load.
