Jump to content

CLS problem image width and height


Rico92

Recommended Posts

22 minutes ago, webprog said:

Hello, your slider in module tm_imageslider generates images without width and height. So, you need to make changes in this module or find another module.

Hi, thank you for your answer. It is not just the slider module. It goes for all images on my website according to pagespeed. Every page i look up seems to have this problem. So, i am looking for a one-hit solution that fixes it all

pagespeed4.jpg

pagespeed3.jpg

Link to comment
Share on other sites

Hi.
If it is not a default or classic theme, it is always reasonable to contact the template developer. Apparently he forgot to add the width and height attributes to all TPL templates.
Global javascript could also be used to add the width and height attributes.
But there is a problem with the fact that your template contains a page preloader.
So the javascript would have to wait for the previous javascript to exit.

  • Like 1
Link to comment
Share on other sites

Sample custom.js:

$(document).ajaxStart(function() {
    var imgs = document.getElementsByTagName('img');
    if (imgs){ 
        for (var i = 0; i < imgs.length; i++) {
            var width = imgs[i].naturalWidth;
            var height = imgs[i].naturalHeight;
            imgs[i].setAttribute('width', width);
            imgs[i].setAttribute('height', height);
        } 
    }
});

You can put this code in the file:
./themes/your-theme/assets/js/custom.js
If such a file "custom.js" does not exist, create it.

Edited by 4you.software (see edit history)
  • Like 1
Link to comment
Share on other sites

  • 4 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...