Effortlessly Support Next Gen Image Formats

Where Things Stand

This was written in June 2023, the week Apple announced support, and rewritten in November 2026. Safari has decoded JPEG XL since version 17. Chrome switched it back on in version 155 in October 2026, and Firefox followed the same month. For the first time, most browsers in use can show one.

This checks what your browser can decode, if JavaScript is on.

Spoiler: This piece recommends that you use the picture element, prioritizing better image formats in your web applications while still supporting legacy clients. If you’re already using it you can probably click back. The verbiage below is hopefully helpful for everyone else

Introducing the picture Element

Supported in every major browser, the picture element is primarily leveraged for its media conditions, offering a responsive design facility that varies which image is used depending upon the traits of the display device. A smaller display might load a smaller picture while a high DPI display might use a high resolution image, and so on.

The picture element supports backwards compatibility by hosting an internal img element. If the client doesn’t recognize the picture and source elements, it will ignore those entirely and use the img element instead. Similarly the img will be used if none of the types or media conditions match the client.

In this entry I’m going to focus on the type attribute offered by source children of the picture. By specifying the mime type for a series of images in a set of picture > source children, the browser will find the first image that matches its supported formats, loading and using that option. You can of course combine these with media conditions for type+responsive condition optimizations.

<picture>
    <source type="image/jxl" srcset="/images/flowers.jxl">
    <source type="image/avif" srcset="/images/flowers.avif">
    <source type="image/webp" srcset="/images/flowers.webp">
    <img src="/images/flowers.jpg" alt="Backyard flowers" width="3000" height="1039" loading="lazy">
</picture>
Backyard flowers

That’s it! It really is that easy.

The browser reads the sources top to bottom and takes the first type it can decode. One that knows JPEG XL gets the .jxl. One that doesn’t will likely get the AVIF, then the WebP, and anything older gets a classic, terrible JPEG. The line under the picture says which one yours took. Edge is Chromium underneath, but takes up formats in its own time: it went without AVIF for years.

Browsers that don’t understand the picture element will act as if it doesn’t exist and simply display the img. The width and height on the img still apply whichever source wins, so the page doesn’t jump as the picture arrives.

From a DOM perspective the img is still king, and the resolution of the picture/source artifacts will modify the img element. You can test the currentSrc property of the img to determine which image it selected, as was done on this page to fill out that line.

This is super trivial, yet I’ve seen users on software development boards proposing solutions such as inserting a client-sniffing reverse proxy that replaces img srcs on a need basis, or client-side feature sniffing scripts, and thankfully that sort of heavy-handed solution is entirely unnecessary. There are also server-side options such as subbing JXL for JPEG if the accepts header indicates, but that is not the scope of this, and often isn’t an option when you don’t control the proxy server or are static hosting.

Why JPEG XL?

We’ve had broadly-supported superior formats for years in the form of WEBP and AVIF (both derived from video codecs). These were already a huge step up from JPEG. What JPEG XL adds:

  • It repacks the JPEGs you already have, with zero loss. Not re-encoded: the same picture, stored better, and the original file can be rebuilt from it byte for byte1. Typically about 20% smaller. Nothing else here can do this.

  • Better visual quality at the same file size as JPEG, better than WEBP, and competitive with AVIF/HEIC. More on that below, because “competitive” is carrying some weight.

  • Lossless compression that beats PNG, making it an all-purpose solution for charts, figures and logos as well as photographs.

  • Supports HDR and wide colour.

  • Supports transparency/alpha channels. Not just a mask, but a full channel facilitating varied transparency. It actually supports arbitrary channels for any nature of additional data, such as depth data, making it an incredibly flexible format.

  • Supports animation, and progressive rendering, where a rough version of the picture appears early and sharpens as the rest arrives.

  • Fast to decode. Gentle on processors and batteries.

Not every client supports every feature: Safari doesn’t do the animation or the progressive rendering, for instance.

JPEG XL is a viable replacement for JPEGs, PNGs and GIFs. It is not all upside. Encoding is slower than JPEG, tooling still lags, and at very small file sizes AVIF looks better.

The Same Picture, the Same Size

A comparison is only fair when the files are the same size, so here are four formats each given the same number of bytes.

The comparison loads here, if JavaScript is on.

At 14 KB the JPEG falls apart into blocks, and AVIF holds together best: smoothing things over is what a video codec is for. At 40 KB, which is nearer what you’d actually serve, all four are close, and you have to magnify it to see that the JPEG is the roughest. So the honest pitch for JPEG XL isn’t that photographs look dramatically better at web sizes. It’s everything else on the list above, in one format.

Making the Files

libjxl is the reference library, and it comes with two command line tools.

# An existing JPEG: repacked, not re-encoded
cjxl photo.jpg photo.jxl

# ...and back again, to the identical file
djxl photo.jxl photo.jpg

# From a high quality original. -d is how far from the original it may stray:
# 1 is visually lossless, 0 is truly lossless. -e is how hard to try.
cjxl original.png photo.jxl -d 1 -e 7
cjxl logo.png logo.jxl -d 0 -e 9

I ran the first two over every JPEG on this site. All 52 came back out bit-identical, and together they were 17.5% smaller. The flowers above did better than most, going from 113,220 bytes to 81,011.

In a new workflow, go straight to JPEG XL from your high quality originals for peak quality. Many other tools, like ImageMagick, support it as well.

Not Just Photographs

JPEG XL is a replacement for PNG scenarios too. Here is a logo (for my own employer), as a lossless JPEG XL with a PNG to fall back to.

<picture>
    <source type="image/jxl" srcset="/images/yafla_logo.jxl">
    <img src="/images/yafla_logo.png" alt="logo" width="1830" height="877">
</picture>

PNG is a fantastic format for uses like this, but the JPEG XL is 9,778 bytes against 44,530 for the PNG, and both hold exactly the same pixels. Squeezing the PNG as hard as I could got it to 24,842 bytes, so call it two-fifths the size rather than a fifth.

The checkerboard is added to the background to demonstrate the transparency, which both formats support.

For a logo the real answer is usually a vector. The SVG of this one is 10,516 bytes and renders perfectly at any size, and every browser that understands picture also understands SVG, so there’s rarely a reason to list anything ahead of it.

How We Got Here

At WWDC 23, Apple announced that they would support JPEG XL2 in their upcoming software releases3. Not only in their new OS products, but in their Safari 17 release which could be used from macOS Monterey onwards.

Overnight a billion+ devices supported this feature-rich image format, which was a stark change from the very limited support it previously saw. Google had removed it from Chromium only months before4.

When I first wrote this I offered up a counterpoint: Safari had supported JPEG 2000 for over a decade with little impact on the computing world, the format still wallowing in obscurity. That one ended as you’d expect, with Safari dropping JPEG 2000 in version 18. JPEG XL went the other way. Within three years the PDF Association had picked it up, Google reversed course, and Firefox shipped a decoder written in Rust.

The difference is the value proposition. JPEG 2000 basically offered only a higher compression ratio. JPEG XL offers transparency, HDR, animations, compressing existing JPEGs with zero quality loss, lossless compression, and a number of other real-world benefits that can improve many workflows.

This is it. It’s finally time to move on from the thoroughly obsolete JPEG format! Okay, in reality JPEG will hang around forever. But you no longer have a superior option for some of your users. You have one for most of them.

Footnotes

  1. This works because the repacking keeps the JPEG’s own numbers and only swaps the last, wasteful stage of the old format for a better one. The saving depends on how the JPEG was made: closer to 10% for very high quality files, and closer to 30% for ones saved without any optimization. ↩

  2. Safari 17 also adds HEIF/HEIC support, which has always been a bit of a split brain situation on Apple platforms. Use iCloud Photos from Safari and it presents photos as JPGs to your browser (given that it didn’t support HEIC), but if you download while using a macOS client it downloads a less compatible HEIC variant that is a pain if you need to upload it to a website, with few supporting the format. If you access the same site from a Windows machine and choose the lower quality option it gives you a JPEG version. HEIC (which is another format derived from a video codec, as with webp and avif) is a big upgrade over the original JPEG from a visual fidelity perspective, but most of the time it’s a big hassle to deal with. ↩

  3. Relative to the time that I first authored this, 2023-06-07, when it was all still in beta. ↩

  4. I get why Google removed it, as it was a classic chicken/egg situation: With the feature poorly supported in browsers, where even on canary copies of Chrome it was hidden behind a flag, there was no benefit for content creators to support it. Every line of code represents security and maintenance concerns so eventually they pulled it. Adding 1B+ Apple device users supporting the format is what changed the sum. ↩