Skip to main content

ImgWidthAndHeight

Enforces setting the width and height attributes on img tags, avoiding cumulative layout shift (CLS).

When width and height attributes aren't set, then the browser doesn't know aspect ratio of the image before it is downloaded. Unless another technique is used to allocate space, the browser considers the image's height to be 0px until the image is loaded.

This causes the following issues:


The following examples contain code snippets that either fail or pass this check.

<img alt="cat" src="cat.jpg" />
<img alt="cat" src="cat.jpg" width="100px" height="100px" />
<img alt="{{ image.alt }}" src="{{ image.src }}" />

Note

You also need to set the CSS width of the img for the image to be responsive.

<img alt="cat" src="cat.jpg" width="100" height="200" />
<img
alt="{{ image.alt }}"
src="{{ image.src }}"
width="{{ image.width }}"
height="{{ image.height }}"
/>

The following example contains the default configuration for this check:

ImgWidthAndHeight:
enabled: true
severity: error
ParameterDescription
enabledWhether the check is enabled.
severityThe severity of the check.

Anchor to Disabling this checkDisabling this check

It's not recommended to disable this check. However, you can disable the check if you use alternative methods, such as aspect ratio boxes, to avoid content-layout shift without setting the width and height attributes.


Was this page helpful?