Skip to main content
Albero Academy

Images in Depth

Serve the right image to every device — formats, responsive images with srcset and picture, lazy loading, and layout-shift prevention.

Chapter 11HTML14 min readBeginner friendly
  • #HTML
  • #Images
  • #Performance

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

FormatBest for
WebP / AVIFModern default — great quality, small files
JPGPhotographs (wide support fallback)
PNGGraphics needing transparency or sharp edges
SVGLogos & icons — vector, scales infinitely
GIFAvoid — 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:

srcset + sizes
<img
src="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
<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 width and height (or CSS aspect-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.
fast, stable images
<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

  1. 01Prefer WebP/AVIF; JPG for photos, PNG for transparency, SVG for logos/icons.
  2. 02srcset offers multiple sizes; sizes tells the browser the display width so it picks well.
  3. 03<picture> serves genuinely different images or modern formats with a fallback.
  4. 04loading="lazy" defers off-screen images; decoding="async" avoids blocking.
  5. 05Always set width/height (or aspect-ratio) to prevent layout shift as images load.