【发布时间】:2019-12-13 00:20:45
【问题描述】:
如何在 CSS 中检测是否支持 webp 图片?
.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');
}
}
是否有一个“if”语句可以做到这一点?
【问题讨论】:
-
可以肯定的是,仅使用 CSS 是不可能“检测到”的。
-
css 中没有 if 语句。如果不支持某些内容,它只会忽略该规则。由于 css 有效,因此不会忽略丢失的图像或不受支持的图像。
-
只有“if statements in CSS”才能测试属性支持——
@supports (property: value){/*rules*/}——不支持图像文件格式。能够做到@supports (background-image: url('existing-image.webp')){/*...*/}会很好,但由于很明显的原因,这不起作用。 -
这里工作解决方案示例stackoverflow.com/a/64048033/1266559
-
回复:“css 中没有 if 语句。”
@media和@supports都代表if结构。