Skip to content

Instantly share code, notes, and snippets.

@dmpinder
Created May 29, 2021 22:56
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 dmpinder/4218cda18c2ac085324365b6c0ace5c8 to your computer and use it in GitHub Desktop.
Save dmpinder/4218cda18c2ac085324365b6c0ace5c8 to your computer and use it in GitHub Desktop.
WebP mixin for SASS with @supports detection
@mixin webp($webp, $fallback) {
background: url($fallback);
/* Chrome 66+, Edge 79+, Opera 53+, Android Brower 80+ */
@media screen and (-webkit-min-device-pixel-ratio: 0) and (min-resolution:.001dpcm) {
@supports (background-image: -webkit-image-set(url($webp) 1x)) {
background: -webkit-image-set(url($webp) 1x);
}
}
/* FF 66+ */
@supports (flex-basis: max-content) and (-moz-appearance: meterbar) {
background: url($webp);
}
}
// Usage
@include webp('hero.webp', 'hero.jpg');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment