Lighthouse Tests Image Tag complains about no width and height
See original GitHub issueDescribe the bug This code, according to many sources, is correctly formatted, and shows no CLS, which is good. There is no defined underlying CSS for this img:
<img srcset="
/assets/images/photos/someimage-400.png 400w,
/assets/images/photos/someimage-352.png 352w,
/assets/images/photos/someimage-320.png 320w,
/assets/images/photos/someimage-304.png 304w,
/assets/images/photos/someimage-288.png 288w
"
sizes="
(min-width: 1160px) 400px,
(min-width: 960px) 352px,
(min-width: 760px) 320px,
(min-width: 660px) 304px,
(min-width: 320px) 288px
"
src="/assets/images/photos/someimage-288.png" alt="A picture of something"
>
Problem Lighthouse performance complains that:
“Image elements do not have explicit width and height”.
However, if I insert a width and height into the src"…", the error goes away, but then executing the code ignores all of the srcset/sizes variables and only uses the src, regardless of screen size. I have tried changing the order of the min widths (and even changing them to max-widths). Same problem. I have also tried <picture>
tags, but with same result.
Expected behavior
- No error message for correctly formatted code.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Why Lighthouse says image elements lack width and height ...
Probably not, as lacking image height and width attributes can already cause layout shifts. In other words, if this is an issue now,...
Read more >Image elements do not have explicit width and height
Short Answer. Add the image's native width and height in pixels as attributes on the image. This lets the browser calculate the aspect...
Read more >Expected Image Size is incorrect · Issue #10802 - GitHub
I am experiencing a similar problem. I am loading a total of 17 images. Lighthouse is flagging just 2 of the images with...
Read more >Quick Tips: How to fix "Image elements do not have explicit ...
All you need to do is add the images native width and height to the image. You can do this with the width...
Read more >Use explicit width and height on image elements - GTmetrix
Images and/or videos that do not have explicit width and height attributes can cause large layout shifts as your page loads.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is WAI because a layout shift is still possible as the image loads in.
Based on our documentation and some brief testing, it looks like sizes in
srcset
andsizes
do not explicitly define dimensions for the image element, and therefore don’t prevent layout shifts. This is WAI as far as I can tell.