Archived topic
Image elements do not have explicit width and height
8 replies · Started by Sabbir on April 4, 2021
How to solve the problem. "Set an explicit width and height on image elements to reduce layout shifts and improve CLS."
I didn't use WPSP
Hi there,
There are multiple images being flagged but not all are from GeneratePress.
As for the images flagged from GeneratePress(the logo), we can address that by using a PHP filter to add height and width to it.
Here's how to do it for the desktop logo image:
https://docs.generatepress.com/article/generate_logo_output/#setting-a-width-and-height
Here's how to do it on mobile logo:
https://docs.generatepress.com/article/generate_mobile_header_logo_output/#setting-a-width-and-height
As for the other image/s:
You seem to be using a plugin called Azonpress. As this is a third-party plugin outside of our scope, we don't exactly know the best practice in adding image height and width attributes inserted on it.
I recommend contacting Azonpress' support on how to address this issue.
how to use those codes?
Hi there,
those codes are PHP - this article explains how to add them:
I added the PHP code in "functions.php"
and should I replace "Setting a Width and Height to Adding a New Class" or use any one code?
or do you have a plan to fix the problem in next update? I don't like to add like this
thank you
Can you make sure the code is added in the child theme's function.php and not the parent theme's function.php
Hi Leo, When I did it broke my headers
Hi there,
If functions.php usage is too confusing, revert the changes you've made and try using Code snippets plugin instead.
https://en-ca.wordpress.org/plugins/code-snippets/
Example Usage: https://share.getcloudapp.com/rRuGLKXA
Note: Make sure your GeneratePress theme is updated. I believe the flexbox version of the site automatically assigns these attribute when the customizer setting is set to Flexbox on Appearance > Customize > General. I could be wrong about this though, but keeping the theme up to date is good.