picture元素使用

141 阅读1分钟

今天看avif图片格式科普的时候看到这么段代码。

<picture>
  <source srcset="img/photo.avif" type="image/avif">
  <source srcset="img/photo.webp" type="image/webp">
  <img src="img/photo.jpg" alt="Description of Photo">
</picture>

发现picture元素支持渐进式地声明源。

上述代码在支持picture元素的浏览器中会先去加载avif格式的图片,如果不可用则会去加载下一个源;

在不支持picture元素的浏览器中会直接降级为img标签。