Skip to content

Instantly share code, notes, and snippets.

@johnfmorton
Created May 18, 2021 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnfmorton/aa07ec4d4684724b7ac1479b7bd9ff2f to your computer and use it in GitHub Desktop.
Save johnfmorton/aa07ec4d4684724b7ac1479b7bd9ff2f to your computer and use it in GitHub Desktop.
Picture tag helper snippet
<!-- Revised snippet based on https://www.stefanjudis.com/snippets/a-picture-element-to-load-correctly-resized-webp-images-in-html/ -->
<picture>
<!-- load avif if supported -->
<source type="image/avif"
srcset="image-100x200.avif 100w,
image-200x400.avif 200w"
sizes="
(max-width: 768px) calc(100vw - 3em),
(max-width: 1376px) calc(50vw - 8em),
550px"
>
<!-- load webp images if supported -->
<source type="image/webp"
srcset="
image-100x200.webp 100w,
image-200x400.webp 200w"
sizes="
(max-width: 768px) calc(100vw - 3em),
(max-width: 1376px) calc(50vw - 8em),
550px">
<!-- load traditional supported image format -->
<img
srcset="
image-100x200.jpg 100w,
image-200x400.jpg 200w"
sizes="
(max-width: 768px) calc(100vw - 3em),
(max-width: 1376px) calc(50vw - 8em),
550px"
src="image-100x200.jpg 100w"
alt="Alt tag goes here"
loading="lazy"
decoding="async"
width="400"
height="800">
</picture>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment