Image elements do not have explicit width and height

孤者浪人 提交于 2021-01-07 02:45:30

问题


Lighthouse keeps telling me that "Image elements do not have explicit width and height" eventhough i've tried to add it both in css or in img but the problem persists:

img.medium {
  max-width: 29rem;
  width: 100%;
}

or

<img
   src={user.seller.logo}
   alt={user.seller.name}
   width="100%"
   height="auto"
/>

How can i tackle this?


回答1:


Short Answer

Add the images native width and height as attributes on the image. This lets the browser calculate the aspect ratio for the image.

<img width="600px" height="400px" src="some-image.webp"/>

Long Answer

width: 100% does not give an element an explicit width.

What it does is define the width relative to it's parent container.

height:auto also does not give an image an explicit height, you are instructing the browser to make the image height whatever it thinks is appropriate based on the image proportions or the size of the container.

The biggest problem with this is the height of an image (as although the width of 100% is not explicit, it will be easy to calculate the actual width purely from the CSS).

When the page is requested the browser does not know the proportions of the image until it starts downloading.

For this reason the page renders (assuming you have inlined your critical CSS) then it requests the image and finds out how tall the image is. The container for the image will then change size to accommodate this image and you will get a layout shift, contributing to cumulative layout shift.

How to fix it

Option 1 - define the width and height using attributes

Option one is to define the image height and width using attributes:

<img width="600px" height="400px" src="some-image.webp"/>

In modern browsers this will then be used to calculate an aspect ratio for the image and sufficient space will then be allocated on the page before the image starts downloading.

You can then use width:100%; height: auto; as you do now and it will all work as expected.

Using width and height attributes is the recommended best practice.

please note - the width and height values only have to be the correct aspect ratio if you are using CSS to override the dimensions (so width=200 height=100 would give the same result as width=400 height=200 assuming you set the width in the CSS).

Option 2 - use "Aspect Ratio Boxes"

In this technique you define the image height as a proportion of the width within your CSS.

In essence we give the image a zero height and then use padding to allocate the correct amount of space.

.image-div {
  overflow: hidden;
  height: 0;
  padding-top: 56.25%; /*aspect ratio of 16:9 is 100% width and 56.25% height*/
  background: url(/images-one.webp);
} 

If you don't need to support Internet Explorer (as it is a little temperamental) you can use calc().

padding-top: calc(900 / 1600 * 100%);

This padding technique is explained in detail in this article from css-tricks.

Although this is a bit of a hack, the advantage is no inline attributes on your images, so it is useful for people who like to keep their HTML clean (and for certain scenarios such as if you want make all images the same aspect ratio)

The problem with both

There is only one issue with both techniques, you need to know the image width and height before they are rendered in order to calculate the aspect ratio.

There isn't much you can do to avoid this unless you are willing to make a fixed height and width container for the image.

Then if the image is a different aspect ratio to your container you will have some white space around the image but the page will not have a layout shift (which would be preferable in most circumstances) - assuming you use overflow:hidden etc. on the container.

.container{
  width:50vw; 
  height:28.125vw; /*fixed height, doesn't have to be aspect ratio correct, see example 2 */
  overflow:hidden;
  margin: 20px;
}
.container2{
  width:50vw; 
  height:40vw; /*fixed height, but this time there is extra white space (shown as dark grey for the example) below the image.*/
  overflow:hidden;
  background: #333;
  margin: 20px;
}
img{
    width: 100%;
    height: auto;
}
<div class="container">
<img src="https://placehold.it/1600x900"/>
</div>

<div class="container2">
<img src="https://placehold.it/1600x900"/>
</div>


来源:https://stackoverflow.com/questions/65076805/image-elements-do-not-have-explicit-width-and-height

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!