I've been waiting to use .webp
images for a very long time.
It's 2022, so, given:
- the image format has existed for a decade
- Chrome started supporting
.webp
in Jan 2014 - Firefox started supporting
.webp
in Jan 2019
I decided the day before yesterday to take the plunge and convert a bunch of .png
images on a website to .webp
images.
Rookie error. Because, of course, I should have checked: https://caniuse.com/webp first and that would have told me that Safari on anything earlier than macOS 11.0 Big Sur (Nov 2020) does not support .webp
images.
But I'm tired of waiting. So... Safari can carry on using .png
images I suppose.
Because I absolutely do want to serve .webp
images to Firefox, Brave, Chrome, Edge, Opera etc. users.
If I were using marked up images, declaring one or more fallback images is elementary:
<picture>
<source srcset="a-webp-for-most-browsers.webp" type="image/webp">
<source srcset="a-png-for-safari.png" type="image/png">
<img src="a-png-for-safari.png" alt="My Image">
</picture>
But (sigh) in this instance, the images are CSS background images, so options for creating fallbacks are more limited:
- CSS
image-set
still has pretty low support at present - CSS feature queries using
@supports
do not extend to image formats.
Am I limited to browser-sniffing via PHP, like this:
if (preg_match('/Mac OS X/', $_SERVER['HTTP_USER_AGENT'])) {
$Body_Element_CSS_Class_List .= ' oh-no-here-comes-safari';
}
Is there a better / more reliable approach I can adopt than browser-detection?