html - Chrome 更喜欢 jpg 而不是 Webp

标签 html image jpeg webp fallback

我有很多 image/webp 图片,希望浏览器为 Safari 提供后备 image/jpg

出于某种原因,Chrome(以及所有其他浏览器)仍在使用 jpg 图像而不是 webp。

<picture>
     <source class="content_image" height="150" width="150" src="<?php echo get_stylesheet_directory_uri();?>/assets/pictures/section/logo_dsv_150x150.webp" type="image/webp">
     <source class="content_image" height="150" width="150" src="<?php echo get_stylesheet_directory_uri();?>/assets/pictures/section/logo_dsv_150x150.png" type="image/png">
     <img height="150" width="150" src="<?php echo get_stylesheet_directory_uri();?>/assets/pictures/section/logo_dsv_150x150.png" alt="lorem ipsum">
</picture>

我也将它与 ACF 结合使用:

<?php
     $image_02_jpg = get_field('content_picture_02_jpg');
     $image_02_webp = get_field('content_picture_02_webp');
?>
<picture>
     <source class="content_image" width="679" height="450px" src="<?php echo $image_02_webp['url']; ?>" type="image/webp">
     <source class="content_image" width="679" height="450px" src="<?php echo $image_02_jpg['url']; ?>" type="image/jpeg"> 
     <img class="content_image" width="679" height="450px" src="<?php echo $image_02_jpg['url']; ?>" alt="content_image">
</picture>

最佳答案

source 元素上使用 srcset 属性代替 src

<?php
  $image_02_jpg = get_field('content_picture_02_jpg');
  $image_02_webp = get_field('content_picture_02_webp');
?>
<picture>
  <source srcset="<?php echo $image_02_webp['url']; ?>" type="image/webp">
  <source srcset="<?php echo $image_02_jpg['url']; ?>" type="image/jpeg"> 
  <img src="<?php echo $image_02_jpg['url']; ?>" alt="content_image">
</picture>

<source> Element

  • src:

  • srcset:一个或多个字符串的列表,用逗号分隔,表示一组可能的图像,这些图像由浏览器使用的源表示。

关于html - Chrome 更喜欢 jpg 而不是 Webp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70108316/

相关文章:

javascript - 单击菜单项后汉堡菜单返回 3 个链接

html - css margin : auto issue in footer

JavaScript 图像幻灯片

PHP成员(member)资格时间和有效期

javascript - 阻止网页页面跳转到顶部

arrays - 在vb.net中将base64解码为图像

javascript - 第一张图片未加载

diff - 将 JPEG 图像从自身中减去会得到非空图像

Node.js:从 jpeg 图像到视频文件的实时转换

javascript - 在 HTML 中识别和显示 360 度图像