Media Element Lacking an Explicit Size: How to Fix Missing Media Dimensions in WordPress and Prevent Layout Shifts and Poor Cumulative Layout Shift Scores

September 03, 2026 by Andrew Smith

Fix the “media element lacking an explicit size” warning by giving every image, video, iframe, ad slot, and embed a reserved width and height before the browser renders it. In WordPress, that usually means using proper image output functions, adding missing width and height attributes, setting an aspect-ratio in CSS, and reserving space for third-party content.

TLDR: Missing media dimensions cause the browser to guess how much space an image or embed needs. When the file finally loads, the page jumps, which hurts Cumulative Layout Shift and annoys users. For example, a product page with three unsized images may show a CLS score of 0.28, while adding fixed dimensions and aspect ratios can bring it below the recommended 0.10. If users have to tap the same button twice because an image pushed it down, expect lower trust and fewer conversions.

What the warning actually means

The warning “media element lacking an explicit size” means a browser found an image, video, iframe, or similar element without clear dimensions. The browser starts painting the page before it knows the media size. Then the media loads. Space changes. Text moves. Buttons jump. That movement is measured as Cumulative Layout Shift, or CLS.

CLS is part of Google’s Core Web Vitals. A good CLS score is 0.10 or less. Anything above 0.25 is considered poor. This is not just a search issue. It is a usability issue. A shifting page feels broken, especially on mobile connections.

Honestly, it feels like such a small thing until you watch a checkout button jump under someone’s thumb. That is when missing dimensions stop being a technical warning and start costing money.

Why WordPress sites often have this problem

WordPress handles image dimensions well when themes and plugins use the correct functions. The trouble starts when media is inserted through page builders, custom HTML blocks, old shortcodes, ad scripts, sliders, or third-party embeds.

Common causes include:

  • Images inserted without width and height attributes, especially in custom templates.
  • CSS that forces images to resize without preserving their aspect ratio.
  • YouTube, Vimeo, Google Maps, and social embeds that load late and push content down.
  • Ad slots with no reserved space, causing large jumps after ad networks respond.
  • Lazy-loaded media where the placeholder has no dimensions.
  • Broken or missing thumbnail metadata after a migration or image optimization process.

How to find the affected media

Start with a real test, not guesswork. Use PageSpeed Insights, Lighthouse, Chrome DevTools, or the Core Web Vitals report in Google Search Console. Look for CLS warnings and layout shift records.

In Chrome DevTools, open the Performance panel, record a page load, and check the Layout Shifts track. Chrome highlights the element that moved. This is often faster than scanning the full page by hand. Expect to waste time on pages with sliders and ad stacks, since several elements may move in sequence.

Also inspect the page source. Search for <img, <iframe, and <video. If you see media elements without width and height, they are likely suspects.

Fix images with proper width and height attributes

For normal WordPress images, the safest fix is to let WordPress output image markup. Use wp_get_attachment_image() in themes and custom plugins. It adds useful attributes, including dimensions, when attachment metadata exists.

Example:

<?php
echo wp_get_attachment_image(
    $image_id,
    'large',
    false,
    array(
        'class' => 'featured-image',
        'loading' => 'lazy'
    )
);
?>

If you are writing plain HTML, add both attributes yourself:

<img src="/uploads/product.jpg" width="1200" height="800" alt="Product photo">

The image can still be responsive. Use CSS like this:

img {
  max-width: 100%;
  height: auto;
}

The width and height attributes tell the browser the image’s aspect ratio. CSS then scales it without causing a shift.

Regenerate missing image metadata

If WordPress does not know the dimensions of older images, the attachment metadata may be missing or damaged. This often happens after imports, server moves, or manual uploads through FTP.

Practical fixes include:

  • Regenerate thumbnails using a reputable thumbnail regeneration plugin.
  • Confirm that the original files still exist in /wp-content/uploads/.
  • Check that image optimization tools did not remove attachment metadata.
  • Re-upload badly broken images through the Media Library if needed.

After regeneration, clear all caches. That includes page cache, CDN cache, object cache, and any optimization plugin cache. Cached HTML can keep serving old image markup even after the media library is fixed.

Use CSS aspect-ratio for embeds and custom blocks

Images are only part of the problem. Videos, maps, calendars, forms, and social feeds can also cause ugly jumps. These elements often arrive from third-party scripts, so the browser has no early size clue.

Use a wrapper with a set aspect ratio:

.video-wrapper {
  width: 100%;
  aspect-ratio: 16 / 9;
}

.video-wrapper iframe {
  width: 100%;
  height: 100%;
  border: 0;
}

Then place the iframe inside it:

<div class="video-wrapper">
  <iframe src="https://www.youtube.com/embed/example" allowfullscreen></iframe>
</div>

This reserves the right space before the video loads. The page stays still. Users can read without the content slipping away.

Reserve space for ads, banners, and late-loading widgets

Ads are a frequent CLS offender. An empty ad container may start at zero height, then expand to 250, 280, or 600 pixels after the auction completes. That shift can wreck a good score.

Set a minimum height for every ad position:

.ad-slot-top {
  min-height: 280px;
}

.ad-slot-sidebar {
  min-height: 600px;
}

Choose sizes based on actual ad formats. Do not reserve a huge blank box just to be safe. That creates a poor reading experience. Review analytics to see which ad sizes appear most often. If a 300 by 250 unit fills 82% of mobile loads, reserve that size and handle rarer formats with care.

Check your theme and page builder output

Many CLS problems come from theme templates. Check files that output featured images, hero sections, cards, galleries, and related posts. If a template prints only the image URL inside an <img> tag, replace that code with WordPress image functions where possible.

Page builders can also strip or override dimensions. Review image widgets, background sections, carousels, and tabs. A hero background is a common problem because CSS background images do not have native width and height attributes. Give the hero section a fixed minimum height or an aspect ratio, especially above the fold.

Do not confuse lazy loading with layout stability

Lazy loading improves loading speed, but it does not fix missing dimensions by itself. A lazy-loaded image still needs reserved space. If the browser delays the image and there is no placeholder size, the page may shift later, just when the user starts reading.

For key above-the-fold media, avoid lazy loading when it hurts perceived speed. A featured image or logo should usually load early. WordPress adds lazy loading by default to many images, but modern versions try to avoid lazy loading the first content image. Still, custom themes can interfere.

A simple WordPress checklist

  • Audit high-traffic pages first. Start with home, product, category, post, and landing pages.
  • Add width and height to all image tags. Use WordPress image functions where possible.
  • Set aspect ratios for iframes and videos. A wrapper is usually best.
  • Reserve ad and widget space. Never let late scripts create layout height from zero.
  • Regenerate thumbnails. Fix missing attachment metadata after migrations.
  • Clear caches and retest. Test both mobile and desktop.
  • Watch real user data. Lab tools help, but field data shows what visitors experience.

What a good result looks like

After the fix, the visual layout should stay stable as the page loads. Images should fade or appear inside reserved boxes, not shove text downward. Videos should keep their shape before playback controls appear. Ads should fill known containers.

A serious target is CLS below 0.10 on key templates. If an article page drops from 0.31 to 0.06 after fixing image dimensions and ad slots, that is a meaningful improvement. Users feel the difference even if they never hear the term Core Web Vitals.

Missing media dimensions are not glamorous work. They are basic engineering hygiene. Fix them once in your WordPress theme, blocks, and media workflow, and you reduce layout shifts across hundreds or thousands of pages.