【发布时间】:2021-12-19 11:57:19
【问题描述】:
灯塔建议using next-gen image formats for faster download and less data consumption。这是使用 HTML <picture> 元素完成的。
那么当我们使用<picture>元素时,如何将CSS样式仅定位到加载的图像?
在下面的 sn-p 中,您可以看到边框也被添加到其他元素。
picture * {
border: 2px solid red;
}
<div style=" display: flex; gap: 30px; ">
<p>AVIF: 50</p>
<p>WebP: 100</p>
<p>img: 150</p>
</div>
<picture>
<source type="image/avif" srcSet="https://via.placeholder.com/50" width="50" height="50" />
<source type="image/webp" srcSet="https://via.placeholder.com/100" width="100" height="100" />
<img src="https://via.placeholder.com/150">
</picture>
注意:在撰写本文时,Chrome 支持 AVIF、Edge 和 Firefox 支持 WebP,因此根据您的浏览器加载相应的文件类型。
Screenshot of above snippet result taken from Microsoft Edge for reference:
【问题讨论】:
-
图片img{...} ?
标签: html css image lighthouse