# width, height & Layout Shift

> Two numeric attributes on `<img>` tell the browser how much space to hold open before the image loads. Without them, every image that loads shifts the text around it.

Subject: HTML | Lesson 18 of 42 | 5 min read
Interactive lesson: https://codewithanimation.in/html/width-height-and-cls
Previous: [The img Element & Alt Text](https://codewithanimation.in/html/img-and-alt.md) | Next: [Responsive Images](https://codewithanimation.in/html/responsive-images.md)

## What they are

The `width` and `height` attributes on `<img>` describe the intrinsic dimensions of the image in CSS pixels. They are not the same as a CSS `width` or `height` style: the attributes describe the image's natural size, and the browser uses them to compute an aspect ratio before the image bytes have arrived.

Cumulative Layout Shift (CLS) is a Core Web Vital that measures how much page content moves unexpectedly after the initial render. Images without `width` and `height` are the single most common cause of a bad CLS score, because the browser allocates zero height for the image slot and then shoves everything below the image downward when the file finishes loading.

## Why it matters

A page that shifts while the user is reading is uncomfortable and error-prone. A user who is about to tap a button and the page jumps may tap the wrong thing. Google's CLS metric captures exactly this: cumulative unexpected movement during the page load window. A score above 0.1 is considered poor, and a page full of sizeless images can easily hit 0.5 or higher.

Setting `width` and `height` is a one-attribute fix per image that eliminates the most common source of CLS entirely. The browser reads the two numbers, divides them to get the aspect ratio, and reserves that slot in the layout immediately. By the time the image loads, the slot is already the right size and nothing moves.

## How it works

Set `width` and `height` to the image's actual pixel dimensions: `<img src="photo.jpg" alt="..." width="800" height="600">`. These values do not have to match the CSS display size. What matters is that they reflect the correct aspect ratio of the image.

Modern browsers (since 2019) derive a default `aspect-ratio: 800 / 600` from the attributes and apply it automatically. This means you can still use CSS `width: 100%; height: auto` to make the image responsive, and the browser will maintain the ratio from the HTML attributes, reserving the right amount of vertical space at every viewport width. The attributes and the CSS cooperate rather than conflict.

For `<video>` elements the same rule applies: add `width` and `height` attributes to prevent layout shift as the video metadata loads. For images served at different sizes through `srcset` (concept #19), set `width` and `height` to the dimensions of the default `src` image.

## Common mistake: Assuming CSS width:100% makes the attributes redundant

A common thought is: if CSS controls the image size, why bother with the HTML attributes? The reason is timing. CSS renders after HTML, and the browser needs the aspect ratio information at parse time to reserve space in the layout. The attributes hand the ratio to the browser immediately; CSS `height: auto` then uses that ratio. Without the attributes, the browser has nothing to compute the height from before the image loads, so it defaults to zero.

## Tip: Use your image editor or build tool to read dimensions automatically

Typing `width` and `height` for every image by hand is tedious and prone to error. Most image processing tools (Sharp, Squoosh, ImageMagick) and build plugins (Next.js `<Image>`, Astro's image component) read dimensions automatically and inject the attributes. If you are writing plain HTML, an image in the browser dev tools Inspector shows the intrinsic size: right-click the image, inspect element, and look for the natural dimensions.

## Try it: Try it

See the difference between an image slot with no dimensions reserved versus one where the browser has held the correct space open from the start.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/html/width-height-and-cls

## Check yourself

### Question 1

An image loads and the paragraph below it jumps down. What caused that?

- A. The <img> had no width and height attributes, so the browser reserved zero space
- B. The image used CSS width: 100%, which overrides the layout
- C. The image file was too large, so the browser delayed layout

Correct answer: A

Without `width` and `height` attributes, the browser has no aspect ratio to compute a height from, so it allocates zero vertical space. When the image loads, the real height is applied and content below shifts downward.

- Why not B: CSS width: 100% on its own does not cause a shift. The problem is missing height information at parse time, which is what the HTML attributes provide.
- Why not C: File size affects download time, not layout allocation. The browser allocates zero height immediately (not after the file is large), and the shift happens when the image finally loads.

### Question 2

An <img> has width="1200" height="800" and CSS width: 100%; height: auto. What does the browser render?

- A. An image that fills its container's width and maintains a 3:2 aspect ratio
- B. An image that is exactly 1200px by 800px regardless of screen size
- C. A conflict between the attributes and CSS that produces unpredictable sizing

Correct answer: A

The browser derives `aspect-ratio: 1200 / 800` (3:2) from the attributes. `width: 100%` sets the display width; `height: auto` uses the ratio to compute the display height. The image fills its container and stays in ratio.

- Why not B: CSS width: 100% overrides the attribute for display size. But the browser derives aspect-ratio: 3/2 from the attributes, which height: auto then uses to compute the display height.
- Why not C: There is no conflict. The attributes describe the intrinsic ratio; CSS width and height control the display size. They cooperate: width:100% sets the display width and height:auto uses the aspect ratio to set the display height.

### Question 3

The original image is 2400x1600px but you display it at 800x533px with CSS. What do you put in the attributes?

- A. width="2400" height="1600": the intrinsic (natural) dimensions
- B. width="800" height="533": the CSS display dimensions
- C. No attributes needed; CSS handles the size entirely

Correct answer: A

The attributes should reflect the image's natural (intrinsic) dimensions. What matters for CLS prevention is that the ratio is correct: `2400x1600` and `800x533` are both 3:2 and both work. The spec says to use the intrinsic size.

- Why not B: Either works as long as the ratio is correct (3:2 in both cases), but the spec recommendation is to use the intrinsic dimensions of the source image.
- Why not C: CSS controls display size but not the pre-load space reservation. The HTML attributes give the browser the aspect ratio before the image bytes arrive, preventing CLS.

## Remember this

- Always set `width` and `height` on `<img>` to the image's intrinsic pixel dimensions to prevent layout shift.
- Browsers derive `aspect-ratio` from the attributes; `width: 100%; height: auto` in CSS then maintains that ratio responsively.
- Missing dimensions = zero height reserved = content below shifts when the image loads (CLS).
- The attributes describe the natural image size; CSS controls the display size. Both can coexist without conflict.
