Search code examples
csswebp

How to detect if webp images are supported via CSS


How do you detect in CSS if webp images are supported?

.home-banner-background {
     background-image: url('img/banner.jpg');
 }
 /*Here is an if statement that will check if webp is supported*/ {
        .home-banner-background {
            background-image: url('img/banner.webp');
        }
}

Is there an "if" statement which can do that?


Solution

  • You can use Modernizr. It is a tool that detect available features on the user's browser. It is just a script to add in your website, and with it, you can write something like that :

    .no-webp .home-banner-background {
         background-image: url('img/banner.jpg');
     }
    
    .webp .home-banner-background {
         background-image: url('img/banner.webp');
    }