HTML publishing guides

Build a Picture Element With a Reliable Fallback

·

A picture element lets the browser choose a compatible image format before it chooses a candidate width. Check that all three format sets describe the same image and match assets your host can actually serve.

AVIF/WebP Fallback Markup Generator

Compare the three format sets for a hero image

Select Load the harbor example, then Generate picture markup. The base URL is https://assets.example/photo/harbor.jpg?fit=crop&w=9&format=old and the requested widths are 640, 320 and 960. The generator sorts each source set to 320w, 640w and 960w, keeps fit=crop and replaces format and w.

The two source elements appear in AVIF and WebP order. The img element is the JPG fallback, with 640 × 360 intrinsic dimensions, alt text Harbor & boats, and the same responsive sizes value. Its src is https://assets.example/photo/harbor.jpg?fit=crop&format=jpg&w=640. The preferred width of 800 px is equally close to 640 and 960, so the generator selects 640 for src. Download picture.html and check the source order and the 640w candidates.

Compare the three format sets for a hero image
Element640 px candidate query
AVIF sourcefit=crop&format=avif&w=640
WebP sourcefit=crop&format=webp&w=640
JPG imgfit=crop&format=jpg&w=640

Open the markup generator and load the harbor exampleWHATWG HTML, the picture element

Switch to filenames when you host each variant

Choose Filename suffix and extension if you host files such as harbor-640.avif and harbor-640.webp. Starting with /media/harbor.jpg generates matching base names for the candidates. Choose JPG, JPEG or PNG to match the actual extension of your fallback files.

The tool generates text locally and never requests the supplied URL. Before publishing, verify the content type and actual pixel width of each deployed candidate. Use the developer tools to see which image the browser selected.

WHATWG HTML, the picture element

Match sizes to the layout

The harbor example uses (max-width: 640px) 100vw, 640px. It describes an image as wide as the viewport through 640 px and a slot that stays at 640 CSS pixels above that point. Adjust sizes if your layout uses gutters or a different maximum width.

Use the actual image aspect ratio for the width and height attributes. The fallback img also carries the alternative text and any decoding or loading hints. Consider whether to omit lazy loading when the image must appear early in the initial viewport.

WHATWG HTML, the picture element

Common questions

Will a missing AVIF file make the browser try WebP automatically?

Format selection uses the declared type and browser support. Make sure every eligible source URL serves a usable file because selection does not test each URL before choosing a source.

Why does the generated HTML show & in query parameters?

The generator escapes ampersands in HTML attributes. The browser decodes the attribute back to ampersands in the URL it uses.

Sources and example

Put it into practice.

Related guides