Add missing image dimensions
This feature is available in the Media tab.
How does this option help?
When a browser is displaying a web page, it loads the HTML first and waits for the images to be downloaded. If the width and height values are available, browsers can use that information to reserve the space needed for the images on the page.
When space for an image is not reserved, the browser will move content around to accommodate the image once the image is downloaded. This layout shift leads to a poor experience for the user.
This feature helps to address the following recommendations from GTmetrix and PageSpeed:
- Use explicit width and height on image elements
- Cumulative layout shift
How does it work?
WP Rocket will add the missing attribute for the image if t's missing height only, width only, or both height and width.
This feature is only applied on cached/optimized pages - check your site while logged out to see the effects.
WP Rocket scans the HTML to find images that:
- Have a
src
attribute, but are withoutwidth
orheight
attributes.
- Are accessible (i.e there's no
404
error). - Are hosted on your domain (to apply this feature on externally hosted images, please see this guide)
- We get the image size using the PHP function
getimagesize
and add width/height attributes using those values. - For SVG images, the SimpleXML PHP extension is required.
- If you are using AVIF images, you must ensure that the PHP version you are using is 8.2.X.
The following will be excluded:
- Images with
width="auto"
orheight="auto"
values. - Images where the width or height is added twice, i.e.
width="100" width="100"
. - Image that are part of a
picture
tag (to apply this feature on these images, please see this guide). - Images with
data-lazy-original
,data-no-image-dimensions
ordata-height-percentage
attributes. - Images that are hosted on external domains.
- Images with query strings in their URLs.
- Images that are dynamically added, i.e., images which are not present in the source code of the page but injected into the DOM later.
- Images that are Base64 encoded
<img src="data:image/png;base64, ...">
Example
If you have an image on your site like the following:
<img src="https://www.example.com/wp-content/uploads/2020/12/image.jpg">
Let's say the image has the dimensions of 200 x 200. It will be updated to:
<img width="200" height="200" src="https://www.example.com/wp-content/uploads/2020/12/image.jpg" > <br>
Excluding images
If this feature causes any problems, please see this guide to exclude specific images:
Disable Add missing image dimensions on specific images