1

我有一个网站,我已经做出响应并利用该<picture>元素来提供 webp、avif 并为旧浏览器提供 png 后备。它应该看起来像这样(Chrome/Safari):

普通的

出于某种原因,某些浏览器(桌面和移动设备上的 Firefox 94,iOS 14 上的 Safari 为例)将加载图像的 AVIF 格式,但会使用我在元素内的显式widthheight来自后备标签,从而导致扭曲的图像。<img><picture>

拉伸

有问题的站点是:https ://blah.cloud/now (将浏览器拉到 <768px 宽以查看问题)。

我进行图像选择的代码如下:

<picture>
<source media="(min-width: 768px)" srcset="/images/logo-title.avif" type="image/avif" alt="logo" width="245" height="34">
<source media="(min-width: 768px)" srcset="/images/logo-title.webp" type="image/webp" alt="logo" width="245" height="34">
<source srcset="/images/logo.avif" type="image/avif" alt="logo" width="65" height="65">
<source srcset="/images/logo.webp" type="image/webp" alt="logo" width="65" height="65">
<img src="/images/logo-title.png" alt="logo" aria-label="logo" width="245" height="34">
</picture>

width显式的原因height是为了让 Google PSI 避免 CLS。

对于为什么会发生这种行为,我有点不知所措——如果它正在拉动 avif/webp 图像,并且我已经确认它是,它应该使用widthand heightassociated with that元素?

任何想法都非常感谢。

4

1 回答 1

0

不幸的是,元素没有widthandheight属性!<source>[ MDN ]

您可以改为调整图像资源的大小。然后将<picture>元素设为块元素并在 CSS 中调整其大小。要调整图像object-fit: cover;请在.<img>

于 2021-11-13T14:12:43.050 回答