admin管理员组文章数量:1302273
When attempting to use the new loading="lazy"
attribute on an img
tag i get the following error:
[Intervention] An <img> element was lazyloaded with loading=lazy, but had no dimensions specified. Specifying dimensions improves performance. See
When looking online, i came across the follow link specifies that adding width
and height
attributes to the img
helps the browser avoid reflow by painting a placeholder to the screen:
/native-lazy-loading#image-loading
My question is, it's pretty unmon these days to hard code a width
and height
of an image, if i override these in my style sheet with something like the following, will i still benefit from avoiding reflow?
img {
width: 100%;
height: auto;
}
Thanks in advance Sami.
When attempting to use the new loading="lazy"
attribute on an img
tag i get the following error:
[Intervention] An <img> element was lazyloaded with loading=lazy, but had no dimensions specified. Specifying dimensions improves performance. See https://crbug./954323
When looking online, i came across the follow link specifies that adding width
and height
attributes to the img
helps the browser avoid reflow by painting a placeholder to the screen:
https://web.dev/native-lazy-loading#image-loading
My question is, it's pretty unmon these days to hard code a width
and height
of an image, if i override these in my style sheet with something like the following, will i still benefit from avoiding reflow?
img {
width: 100%;
height: auto;
}
Thanks in advance Sami.
Share Improve this question asked Sep 3, 2019 at 8:17 SamuelSamuel 2,6356 gold badges34 silver badges41 bronze badges3 Answers
Reset to default 5Short answer is no... Even if 'reflow' behaviour is strictly dependent on the browser's implementation, it's pretty valid to assume that almost any modification of the DOM will lead to a 'reflow', especially if it involves changing dimensions (elements displacing other elements) or adding/removing a node. It would be still advisable to add height and width attributes that will not only suppress the error/warning, but will also work as a fallback.
Most importantly, you will avoid an ugly 'jump' effect when the image loads if the written height/width matches the final style.
Additional note: 'reflows' are to be avoided when possible, but in your case, image loading will happen only once, so I would say It doesn't matter.
As Ernesto Stifano mentions, there is no way to get around the reflow issue without specifying the ratio of the image (i.e. letting the browser know a width and height attribute for the image).
If you don't want to specify an exact width and height though, you can add an intrinsicsize
attribute instead (see here) but you still need to set a width and height property (using the interface intrinsicsize="300 x 400"
, for example).
This can allow you to be able to set width: 100%
in the CSS and not have to worry about setting height: auto
constantly, which may help.
additionally to what Ernesto Stifano has mentioned previously, the current development regarding "intrinsicsize" is more or less to "Compute img/video/canvas aspect ratio from width and height HTML attributes" - pare to https://github./web-platform-tests/wpt/pull/18945 and https://github./WICG/intrinsicsize-attribute/issues/16
本文标签: javascriptNative lazy load image reflow (loading39quotlazyquot)Stack Overflow
版权声明:本文标题:javascript - Native lazy load image reflow (loading='"lazy") - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741694451a2392923.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论