PrestaShop

Boost PrestaShop PageSpeed: Fix 'Image Width/Height' Warnings for CLS

As e-commerce migration experts at Migrate My Shop, we frequently encounter merchants striving to optimize their PrestaShop stores for speed and performance. One of the most common hurdles, and a significant factor in Google's Core Web Vitals, is the dreaded PageSpeed Insights warning: "Image elements do not have explicit width and height." This seemingly minor detail can have a major impact on your site's SEO, user experience, and ultimately, your conversion rates.

Recently, a PrestaShop user, pierroweb, faced this exact challenge. Despite attempting to add dimensions via custom CSS in Elementor, the PageSpeed warning persisted. This scenario highlights a critical misunderstanding: for optimal performance and to satisfy PageSpeed Insights, image dimensions must be declared directly within the HTML tag, not just through CSS.

HTML Image Tag with Missing vs. Correct Width and Height Attributes
HTML Image Tag with Missing vs. Correct Width and Height Attributes

Why Explicit Width and Height Attributes Matter for Your PrestaShop Store

The primary reason Google PageSpeed Insights flags images without explicit dimensions is to prevent Cumulative Layout Shift (CLS). CLS is a Core Web Vital metric that measures the visual stability of a page. When a browser loads a page, it needs to know how much space to reserve for each image. If an image's dimensions aren't specified in the HTML, the browser initially renders the page without allocating space for it. Once the image finally loads, the content around it suddenly shifts to accommodate the image, creating a jarring and frustrating user experience.

For an e-commerce site, this can be particularly damaging. Imagine a customer trying to click an "Add to Cart" button, only for an image above it to load and push the button out of reach. This leads to misclicks, frustration, and potentially, lost sales. Explicit width and height attributes in the HTML tell the browser exactly how much space to reserve, preventing these disruptive layout shifts.


Product Name


Product Name

PrestaShop's Native Approach to Image Dimensions

The good news is that PrestaShop is designed to handle this automatically. Out of the box, PrestaShop generates various image sizes (e.g., home_default, large_default, medium_default) based on the configurations you set in your back office under Design > Image Settings. When these image types are properly configured and used within your theme's templates, PrestaShop's Smarty templating engine should automatically output the correct width and height attributes for your tags.

PrestaShop themes typically utilize Smarty variables like {$image.bySize.home_default.width} and {$image.bySize.home_default.height} to dynamically insert these dimensions into the HTML. This ensures that every image rendered through the standard PrestaShop image handling process has its dimensions explicitly declared.

PrestaShop Image Settings for PageSpeed Optimization

A visual representation of PrestaShop's 'Design > Image Settings' interface, with a focus on image type dimensions, overlaid with a positive Google PageSpeed Insights score improvement graph.

Common Scenarios & Solutions for Missing Image Dimensions

If PageSpeed Insights is still flagging your PrestaShop store, it usually points to one of the following issues:

1. Theme or Module Interference

Problem: Custom themes or third-party performance/image optimization modules can sometimes override PrestaShop's default behavior, inadvertently stripping out the width and height attributes from the tags. This is a common issue we see during PrestaShop migrations where older themes or incompatible modules are carried over.

Solution:

  • Deactivate & Test: Temporarily disable recently installed or updated modules, especially those related to performance, caching, or image handling. Clear your PrestaShop cache and re-test PageSpeed Insights.
  • Theme Review: If the issue persists, your theme might be the culprit. Consider switching to the default PrestaShop theme (e.g., Classic) in a development environment to see if the problem resolves. If it does, your custom theme's templates need to be audited.

2. Outdated or Incorrect Template Overrides

Problem: Many PrestaShop stores use template overrides (files in themes/yourtheme/templates/) to customize the look and feel. If these overrides are old or incorrectly coded, they might not be using the correct Smarty variables to output image dimensions, or they might be hardcoding image tags without attributes.

Solution:

  • Identify & Update: Locate the .tpl files responsible for rendering the problematic images (e.g., product.tpl for product pages, category.tpl for category listings, index.tpl for home page features).
  • Implement Smarty Variables: Ensure your tags within these templates use the appropriate Smarty variables. For example:
    {$image.legend}
    Replace home_default with the specific image type relevant to your context.
  • Compare with Default: If unsure, compare your overridden .tpl files with the corresponding files in the default PrestaShop Classic theme to identify discrepancies.

3. Images Used as CSS Backgrounds

Problem: Hero banners, sliders, and some decorative elements often use images as CSS backgrounds (e.g., background-image: url(...)). For these, you cannot add width and height attributes to an tag because there isn't one.

Solution:

  • CSS aspect-ratio: For elements using background images, you can reserve space using the CSS aspect-ratio property. This tells the browser the intended aspect ratio of the element, allowing it to allocate space before the background image loads.
    .hero-banner {
      background-image: url('path/to/hero.jpg');
      background-size: cover;
      background-position: center;
      aspect-ratio: 16 / 9; /* Or your image's actual aspect ratio */
      width: 100%;
      height: auto; /* Ensure height adjusts based on aspect-ratio */
    }

4. Page Builders (like Elementor)

Problem: As pierroweb discovered, simply adding CSS in Elementor won't fix the core issue. While Elementor often generates standard tags, it might also rely on its own rendering logic or use background images for certain sections.

Solution:

  • Check Elementor Settings: Review the image widgets or sections in Elementor. Some builders have options to explicitly set image dimensions.
  • Inspect Generated HTML: Use your browser's developer tools to inspect the HTML output by Elementor. If it's an tag, the solution lies in ensuring the width and height attributes are present, potentially by modifying the underlying PrestaShop template Elementor uses or by consulting Elementor's support for best practices with PrestaShop.
HTML Image Tag with Missing vs. Correct Width and Height Attributes

A comparison showing a browser's developer console. On the left, an tag is highlighted, missing width and height attributes. On the right, the same tag is shown with the width and height attributes correctly implemented in the HTML.

Beyond Dimensions: Comprehensive PrestaShop Image Optimization

While fixing explicit width and height attributes is crucial for CLS, it's just one piece of the image optimization puzzle. For a truly fast PrestaShop store, consider these additional steps:

  • Image Compression: Use tools or modules to compress images without significant loss of quality.
  • Next-Gen Formats: Convert images to modern formats like WebP, which offer superior compression.
  • Lazy Loading: Implement lazy loading so images only load when they enter the user's viewport. PrestaShop 1.7.7+ includes native lazy loading for product images.
  • CDN Integration: Serve images from a Content Delivery Network (CDN) to reduce latency for users worldwide.

Need Expert Help with Your PrestaShop Migration or Optimization?

Resolving image dimension issues and optimizing your PrestaShop store for peak performance can be complex, especially when dealing with custom themes, modules, or during a migration. At Migrate My Shop, we specialize in seamless PrestaShop migrations and comprehensive optimization strategies. Our team of experts can help you diagnose and fix performance bottlenecks, ensuring your e-commerce site is fast, stable, and ready to convert.

Don't let technical hurdles slow down your business. Contact us today for a consultation and let us help you unlock your PrestaShop store's full potential!

Share:

Start with the tools

Explore migration tools

See options, compare methods, and pick the path that fits your store.

Explore migration tools