Morphio
Menu

WebP in HTML: Images, Fallbacks and Delivery Checks

Use WebP in HTML by referencing actual image bytes through an img element or picture source. HTML markup, the image encoding and the URL that hosts it are three separate parts of the workflow.

Add WebP in HTML with a real image path

Replace the example path with an image that your server actually serves. The img element provides the displayed image and alternative text. Width and height describe its intended intrinsic dimensions; they do not re-encode or resize the stored file.

<img src="/images/ceramic-cup.webp"
     width="800" height="600"
     alt="Blue ceramic cup beside a folded linen napkin">

MDN’s image-source documentation explains the relationship between img and picture. A WebP-to-HTML task normally means embedding an image this way. Rendering a whole HTML page into a screenshot is a different operation and is not provided here.

Use an image URL, not merely a page URL

A page URL may return HTML containing several pictures. An image URL must return the intended image representation. A local path on your computer is not automatically accessible to other visitors, and uploading a file requires a hosting service that supplies a usable URL.

For a direct image you are allowed to use, save or obtain its bytes, verify the source, convert if necessary, and publish the resulting file through your chosen hosting workflow. Do not treat an altered URL suffix as encoding.

To publish an existing JPG or PNG as WebP, first encode that supported local image as WebP, then upload the result through your chosen host and use its image URL. Downloading the source, changing its address and adding HTML markup do not perform that encoding. This page supplies neither a URL fetcher nor image hosting.

For actual PNG output, convert WebP to PNG. For an opaque JPEG, convert WebP to JPG. If your destination needs GIF animation, convert WebP to GIF and inspect the palette and timing changes. The static directions use the first animation frame.

WebP in HTML with responsive candidates and fallback

Provide real files for every candidate. The browser evaluates supported source types and chooses an appropriate candidate for the display. Width descriptors belong with an appropriate sizes value; do not mix width and pixel-density descriptors in the same candidate list.

<picture>
  <source type="image/webp"
          srcset="/images/cup-400.webp 400w, /images/cup-800.webp 800w"
          sizes="(max-width: 600px) 100vw, 600px">
  <img src="/images/cup-800.jpg" width="800" height="600"
       alt="Blue ceramic cup beside a folded linen napkin">
</picture>

The img source supplies an alternative for clients that do not use the WebP source. This is not a general network-error recovery chain: a broken selected URL still needs fixing. Check WebP browser support against your audience and deployment environment.

CSS backgrounds use their own image selection

For decoration, CSS image-set can offer format alternatives. Chrome’s guidance also recommends a preceding ordinary declaration for browsers that ignore image-set. Keep meaningful image content in accessible markup rather than depending on a decorative background to convey it.

.decorative-panel {
  background-image: url("/images/pattern.png");
  background-image: image-set(
    url("/images/pattern.webp") type("image/webp"),
    url("/images/pattern.png") type("image/png")
  );
  background-size: cover;
}

Check the actual selected resource and layout. Adding WebP in HTML elsewhere on the page does not automatically provide a fallback for this separate CSS image.

Check the response before blaming the format

Inspect the requested URL, redirects, final status and response body. A successful-looking request can still return an authentication page instead of pixels. Verify the WebP MIME type in the response. HTTP headers describe the transfer; the file header belongs to the encoded bytes.

When one URL varies its image representation according to Accept, content negotiation needs corresponding cache behavior. A Vary: Accept response tells caches that this request header affected the representation. Do not add that header as a substitute for generating the alternate images.

Content-Type: image/webp
Vary: Accept

The Vary example applies to Accept-based negotiation. If each format has its own URL, use a cache policy appropriate to that design. Separate missing URLs, authorization failures and cross-origin pixel-access restrictions from decoder support.

Export canvas pixels and verify the format

Canvas toBlob can fall back to PNG when the requested encoding is unsupported, and the callback can receive null. Check the Blob type before assigning a WebP filename. This example expects an existing canvas containing the intended static pixels:

const blob = await new Promise((resolve, reject) => {
  try {
    canvas.toBlob(value => {
      if (!value) return reject(new Error("No image was encoded"));
      if (value.type !== "image/webp") {
        return reject(new Error(`Unexpected output: ${value.type}`));
      }
      resolve(value);
    }, "image/webp", 0.85);
  } catch (error) {
    reject(error);
  }
});

A canvas containing cross-origin pixels without the required approval can throw a SecurityError. MDN’s canvas CORS guide explains the server and image-loading requirements. This is not a way to bypass access controls, and a static canvas export is not an animation encoder.

Measure delivery and provide useful image context

Google Search’s image guidance supports WebP and AVIF, recommends discoverable img sources and appropriate alternative text, and encourages measuring site performance. It does not promise a ranking increase for changing an extension.

Compare actual bytes, requested dimensions, cache behavior and the user-visible loading result. A smaller source can still be delivered inefficiently. WebP in HTML is one part of image delivery; a format change alone does not prove that every page became faster.

Social previews, email and LaTeX need their own checks

The Open Graph protocol defines an image URL and optional type, size and alt properties. A social platform’s crawler still has its own fetch and format requirements. Verify its generated preview with a reachable image; protocol markup alone is not universal WebP acceptance.

For email production, Mailchimp’s image guidance recommends PNG, JPG or GIF for its content blocks, including PNG for transparency. Treat that as service-specific guidance and inspect a delivered message in the actual target clients; browser support is not an email-client compatibility table.

Overleaf’s compiler guidance lists JPG, PNG and PDF for pdfLaTeX, with a separate EPS path. Use a supported image for the selected compiler instead of expecting a .webp suffix to work everywhere.

Base64, JSON and XML do not define new image pixels

Base64 represents bytes as text. A data URL combines a media-type label with the encoded payload; decoding that payload restores the original bytes, not a differently compressed image. A mismatched label does not turn arbitrary bytes into WebP.

JSON or XML can hold an image URL, a Base64 field or application-specific drawing data. To reverse such a wrapper, you need its schema and the actual image payload. A list of colors, a document tree and an encoded image are different inputs. No arbitrary JSON-to-WebP or XML-to-WebP converter is provided here.

Questions and answers

Practical answers for this task.