← Back to HTML & CSS, page 5

html-css-0086 · Easy fresher

Which image format would you pick for a photo, a logo and a screenshot, and how do you offer a fallback?

Use JPEG, WebP or AVIF for photos, PNG or WebP for sharp images that need transparency, and SVG for logos and icons. Offer newer formats with picture and source type, and keep a JPEG or PNG img as the fallback.

Full answer

Each format suits a different kind of picture.

  • JPEG is good for photos. It is small, but it loses detail, and it has no transparency.
  • PNG is good for screenshots, text and images with sharp edges, and it supports transparency. Photos in PNG are large.
  • SVG is a drawing described in text. It stays sharp at any size, which suits logos, icons and simple illustrations. It is a poor choice for photos.
  • WebP and AVIF are newer formats. They are often smaller than JPEG or PNG at similar quality, and they support transparency. AVIF is often the smallest but can be slower to create. Check the support table for your visitors.
  • GIF is old and large. Use a short video or an animated WebP for animations.

To offer a newer format with a safe fallback, use picture with several source elements. The browser takes the first source whose type it understands. The img at the end is the fallback, and it also carries the alt text and the size.

A browser that understands AVIF loads the AVIF file. One that knows only WebP loads the WebP file. All others load the JPEG. The page keeps one alt text and one reserved size for all cases.

Always give width and height so the page does not jump when the image loads. Size the image to the largest size it is shown at, and compress it before upload.

html
<picture>
  <source srcset="/img/campus.avif" type="image/avif">
  <source srcset="/img/campus.webp" type="image/webp">
  <img id="photo" src="/img/campus.jpg" alt="Students at the campus gate" width="400" height="300">
</picture>

The image files are placeholders served by a local test server, so the check read the markup and not the chosen file. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • JPEG suits photos; PNG suits sharp images; SVG suits logos and icons.
  • WebP and AVIF are often smaller, but check support.
  • picture with source type offers formats in order.
  • The img inside picture is the fallback and holds alt and size.
  • Set width and height to avoid layout shift.

Common mistakes

  • Using PNG for large photos, which makes pages heavy.
  • Using a JPEG for a logo, which gets blurry edges and cannot be transparent.
  • Adding a new format with no fallback img.

Follow-ups

  • Why should width and height be set on the img?
  • When is SVG a bad choice?

Tips

  • Match format to content: photo, drawing, sharp screenshot.
  • Say that picture picks the first source the browser supports, and img is the fallback.