HTML Picture Tag Explained: Responsive Images, WebP and AVIF
I haven’t thought about HTML in a long time, but recently came across the usage of picture tag(s). I was so impressed that I wanted to blog about it. This is nothing new, and used a lot by the web already.
The picture tag gives us a lot of control over how images are loaded, solving two main problems: efficient formats and responsive sizing.
WebP Support
We all know WebP images are smaller and faster to load, but not every browser supported them initially (though support is great now). The picture tag allows us to serve WebP to browsers that support it, while falling back to PNG or JPEG for others.
| |
The browser parses the <source> tags from top to bottom. The first one with a supported type is chosen. If none match, it falls back to the standard <img> tag.