In short
The width and height attributes of an <img> element give the image's dimensions in pixels. The browser derives its width-to-height ratio from them and reserves the right amount of space even before receiving the file, including when CSS resizes it for a phone screen. The task is to fill in these two attributes on every image, with accurate values, and to check that the site's CSS does not contradict them. Expected result: text no longer jumps when images arrive.
Why it matters
Without dimensions, an image is zero pixels tall until it arrives, then pushes everything after it down. This jump counts towards CLS (Cumulative Layout Shift), one of the three Core Web Vitals, and it causes missed clicks or makes readers lose their place.
Now that browsers derive an aspect-ratio from the width and height attributes, these attributes no longer freeze the displayed size. The browsers' default style sheet applies the equivalent of:
img {
aspect-ratio: auto attr(width) / attr(height);
}
With max-width: 100% and height: auto in your CSS, the image adapts to the screen and the reserved space keeps the right proportion. This is why you need to fill in width and height even on a fully responsive site.
Search engines do not use these attributes to understand an image: the effect on SEO comes only through user experience and Core Web Vitals. For an overview of the other elements that shift the page (ads, iframes, banners), see reserve space for media and ads (opens in a new tab).
What it improves
- CLS This is the simplest and most effective fix for images. See target CLS ≤ 0.1 at the 75th percentile (opens in a new tab).
- Lazy loading A deferred image without dimensions causes a jump when it arrives, in the middle of scrolling.
- Reading comfort and conversion No more buttons slipping away from under the finger.
- Lighthouse audit The “Layout shift culprits” insight no longer lists images without dimensions.
- Rankings Indirect effect, through Core Web Vitals.
How to check
In the browser console, list images without dimensions and those whose attributes do not match the file's real proportion:
console.table([...document.images].filter((img) => img.complete && img.naturalWidth).map((img) => {
const w = Number(img.getAttribute('width'));
const h = Number(img.getAttribute('height'));
const declared = w && h ? w / h : null;
const real = img.naturalWidth / img.naturalHeight;
return {
file: (img.currentSrc || img.src).split('/').pop().split('?')[0].slice(0, 40),
attributes: w && h ? `${w} × ${h}` : '(MISSING)',
ratioGap: declared ? `${Math.round(Math.abs(declared - real) / real * 100)}%` : '-',
cssHeight: getComputedStyle(img).height,
};
}));
To fix:
(MISSING): no space reserved, unless a CSSaspect-ratioor a fixed-size container takes care of it.- Ratio gap above a few percent: wrong values, the image will be distorted or the reserved space will be wrong.
- Visibly stretched image: the CSS forces a width without
height: auto.
Then measure actual shifts, scrolling through the whole page:
new PerformanceObserver((list) => {
list.getEntries().filter((entry) => !entry.hadRecentInput).forEach((entry) => {
console.log(entry.value.toFixed(4), entry.sources.map((source) => source.node));
});
}).observe({ type: 'layout-shift', buffered: true });
Compliant result: no image without dimensions in the content, and no shift whose source is an image.
How to implement
Before you start
Target markup and CSS:
<img src="/images/atelier-800.jpg" width="800" height="533" alt="TIG welding station in the workshop" loading="lazy">
img {
max-width: 100%;
height: auto;
}
Rules:
- The values are the file's dimensions in pixels, or those of any variant with the same proportion:
800 × 533and1600 × 1066reserve the same space. Write integers, without units. - Keep
height: autoin the CSS whenever the width is constrained (max-width,width: 100%). Without it, the attribute's height applies as is and the image is squashed. - With
srcset, the attributes describe the proportion; the displayed size comes from CSS andsizes. See use srcset and sizes (opens in a new tab). - In a
<picture>, putwidthandheighton the<img>. If a<source>has a different crop (square on mobile, panoramic on desktop), give it its ownwidthandheight. - Image cropped by CSS in a fixed-proportion slot (square thumbnails): reserve the space with
aspect-ratioandobject-fit:
.product-card img {
width: 100%;
height: auto;
aspect-ratio: 1 / 1;
object-fit: cover;
}
- Unknown dimensions (external image, legacy content): failing the attributes, give the container a plausible
aspect-ratio, or a minimum height. - SVG: an
<img>element displaying an SVG also needswidthandheight; an inline SVG needs aviewBoxattribute, and preferablywidthandheight.
WordPress
Versions WordPress 6.4 and later.
WordPress adds width and height to images inserted in content when it knows their dimensions (since version 5.5), and wp_get_attachment_image() always outputs them. Three cases escape this:
- Images hard-coded in the theme: replace
<img src="<?php echo $url; ?>">withwp_get_attachment_image($attachment_id, 'large'), or add the attributes by hand. - External images or images inserted by URL: WordPress does not know their dimensions. Upload them to the Media Library.
- SVGs uploaded through a plugin: the Media Library does not always record their dimensions. Check the markup produced.
Page builders (Elementor, Divi) sometimes output images without dimensions, or CSS backgrounds: check the HTML of their widgets.
Shopify
Versions Online Store 2.0 themes, Liquid, November 2026.
The image_tag filter automatically adds width and height, computed from the dimensions and proportion requested in image_url. Do not remove them (width: nil) unless you have a specific need. Images written by hand in the theme with <img src="{{ image | image_url: width: 600 }}"> do not have them: go through image_tag, or write the attributes with image.width and image.height while keeping the proportion.
Images inserted in product descriptions through the text editor often have no dimensions: add them in the editor's HTML for the most visible ones.
PrestaShop
Versions PrestaShop 8 and 9, Classic theme.
The image types defined in Design > Image Settings have known dimensions: in the theme's templates, write them from the available variables, for example:
<img src="{$product.cover.bySize.home_default.url}"
width="{$product.cover.bySize.home_default.width}"
height="{$product.cover.bySize.home_default.height}"
alt="{$product.cover.legend}" loading="lazy">
Recent versions of the Classic theme already do this for product thumbnails; check the modules that display images (slider, brands, blog).
Magento
Versions Magento Open Source and Adobe Commerce 2.4.6 to 2.4.9, Luma theme.
Luma's thumbnail templates write width and height from the dimensions declared in etc/view.xml. If you change these dimensions, regenerate the images (bin/magento catalog:images:resize) so that files and attributes stay consistent. Page Builder banners are often CSS backgrounds: reserve their height in CSS.
Drupal
Versions Drupal 10 and 11.
Image fields and image styles output width and height as soon as the dimensions are known. Images inserted in the body text through the editor also receive them when they go through the media library or the CKEditor 5 image button; images pasted as an external URL do not.
Wix
Versions Wix Editor and Wix Studio, November 2026.
Automatic: Wix and Squarespace size images and reserve their space. Remaining shifts tend to come from third-party apps and banners.
Webflow
Versions Webflow Designer, November 2026.
Image elements uploaded from the assets know their dimensions, but the published tag does not always carry both attributes, depending on the element's size settings. Check on the published site; otherwise, give the image or its container an aspect-ratio in the Style panel.
Laravel
Versions Laravel 11 to 13.
Record the dimensions at upload time, then write them in the view:
use Illuminate\Http\UploadedFile;
/** @return array{width: int, height: int} */
function imageDimensions(UploadedFile $file): array
{
[$width, $height] = getimagesize($file->getRealPath());
return ['width' => $width, 'height' => $height];
}
<img src="{{ $media->url }}" width="{{ $media->width }}" height="{{ $media->height }}"
alt="{{ $media->alt }}" loading="lazy">
For content entered in an editor and stored as HTML, complete the tags when saving rather than on every display.
Next.js
Versions Next.js 15 and 16.
next/image requires width and height, except for a statically imported image (dimensions read at build time) or in fill mode. These values set the proportion; the displayed size comes from CSS:
<Image src="https://cdn.example.com/atelier.jpg" width={1600} height={1067}
alt="TIG welding station in the workshop" style={{ width: '100%', height: 'auto' }} />
With fill, the parent container must have a size or an aspect-ratio, and a relative position.
Nuxt
Versions Nuxt 3 and 4, @nuxt/image module 1 and 2.
Give NuxtImg a width and height: they are carried over to the tag and used for resizing.
<NuxtImg src="/images/atelier.jpg" width="800" height="533" alt="TIG welding station in the workshop" />
Astro
Versions Astro 5 to 7.
For a local image imported from src/, Image infers width and height from the file and writes them in the tag. For an image in public/, specify them yourself; for a remote image, specify them or add inferSize.
Common mistakes
- Removing
widthandheight“for responsive design”: it is the CSS (max-width: 100%; height: auto) that makes the image fluid, not the absence of attributes. - Forgetting
height: autoin the CSS: squashed or stretched images. - Using wrong values (
width="100%", or the dimensions of an old version of the file): wrong proportion, wrong reserved space. - Writing units (
width="800px"): invalid value. - Forgetting images inserted by writers and third-party modules, even though the templates are correct.
- Relying on script-based lazy loading with a one-pixel placeholder image: without dimensions, the real image causes a jump when it arrives.
- Overlooking CSS background visuals: they have no attributes; reserve the block's height in CSS.
Frequently asked questions
Do responsive images still need width and height?
Yes. Browsers use the width and height attributes to compute the image's proportion and reserve its space before it arrives, even when CSS resizes it. Pair them with max-width: 100% and height: auto in the style sheet: the image stays fluid on every screen, and text no longer jumps when it loads.
What values should go in width and height?
The file's dimensions in pixels, without units, or those of any variant with the same proportion. With srcset, the value mainly provides the width-to-height ratio: 800 × 533 and 1600 × 1066 reserve the same space. The size actually displayed comes from CSS and the sizes attribute, not from these attributes.
Do width and height affect SEO?
Not directly: search engines do not use them to understand an image. Their effect comes through CLS, one of the Core Web Vitals: without dimensions, the page shifts when images arrive, which degrades the experience and this metric. Core Web Vitals are one ranking signal among many others, generally with a modest effect.
What should I do for an image whose dimensions are unknown?
Reserve the space through the container: a plausible CSS aspect-ratio property, for example 16 / 9 for a video or 1 / 1 for a product thumbnail, and object-fit: cover on the image so that it fills the slot without distortion. For recurring content, it is better to record the dimensions at upload time, then write them in the tag.
Resources
- web.dev: optimize Cumulative Layout Shiftweb.dev
- MDN: img element, width and height attributesdeveloper.mozilla.org
- MDN: aspect-ratio propertydeveloper.mozilla.org
- Smashing Magazine: setting height and width on images is important againsmashingmagazine.com
- Shopify: image_tag filtershopify.dev
- Next.js: width, height and fillnextjs.org
- Astro: image referencedocs.astro.build
Updated on
Topic checklist (PDF)