question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Lighthouse Tests Image Tag complains about no width and height

See original GitHub issue

Describe 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

  1. No error message for correctly formatted code.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
adamrainecommented, Jun 29, 2021

This is WAI because a layout shift is still possible as the image loads in.

1reaction
adamrainecommented, Jun 28, 2021

Based on our documentation and some brief testing, it looks like sizes in srcset and sizes 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found