【问题标题】:Chrome prefers jpg instead of WebpChrome 更喜欢 jpg 而不是 Webp
【发布时间】:2022-01-03 13:33:39
【问题描述】:

我有很多 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>

【问题讨论】:

标签: html image jpeg webp fallback


【解决方案1】:

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:由逗号分隔的一个或多个字符串的列表,表示由浏览器使用的源表示的一组可能的图像。

【讨论】:

  • 天哪,非常感谢!这是修复它!
  • 但是 Google Pagespeed 仍然告诉我使用现代的 img 格式。任何想法?编辑:sry 只是缓存。现在很好:)
猜你喜欢
  • 2018-12-03
  • 2016-09-24
  • 2010-09-24
  • 2011-05-13
  • 1970-01-01
  • 2010-09-20
  • 2015-08-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多