Sub topic 10
Width, height, and lazy loading
This chapter assumes you can write an img element with src and alt, and that you know how relative paths work. It covers two things that affect how a page feels while it loads: reserving space for images, and delaying images the visitor may never see.
Why layout shift happens
A browser reads HTML from top to bottom. When it meets an img, it does not yet know how large the picture is. Unless you say otherwise, it gives the image no space. Text is laid out right away. When the image file arrives, the browser learns its size and pushes the content below it down. This jump is called layout shift. It is annoying: a reader can lose their place, or click the wrong link because the page moved.
Setting width and height
Add width and height attributes to tell the browser the image's dimensions before the file loads.
<img src="images/red-fox.jpg" alt="A red fox standing in snow" width="640" height="427" loading="lazy">Three details matter:
- The values are plain numbers of CSS pixels. Write
640, not640px. - Use the image's real pixel dimensions, or any pair with the same aspect ratio. The browser uses the ratio to reserve a box of the right shape.
- The attributes do not stop you from resizing the image with CSS. A common stylesheet rule is
img { max-width: 100%; height: auto; }. The image then shrinks on narrow screens, and the browser still keeps the correct ratio from the attributes.
Lazy loading
By default, the browser downloads every image on the page right away, even those far below the visible area. The loading attribute changes that.
| Value | Behavior |
|---|---|
| `eager` | Load immediately. This is the default. |
| `lazy` | Wait until the image is near the visible area, then load it. |
With loading="lazy", a long article with twenty photos only downloads the first few at first. The rest load as the reader scrolls. This saves bandwidth and speeds up the initial view.
Common errors
- Lazy-loading the main image at the top of the page. An image that is visible on first load should load as soon as possible. Leave
loadingoff (or useeager) for banner and hero images. Uselazyfor images further down. - Using
lazywithoutwidthandheight. The image loads late, so the missing space causes a bigger shift when it finally appears. Always combine the two. - Writing units.
width="640px"is invalid HTML. Usewidth="640". - Wrong ratio. If the attributes say 640 by 427 but the file is square, the reserved box is the wrong shape and the layout still shifts, or the image is distorted.
Exercise
A page has a large banner image at the top and twelve gallery photos far below it. Which images should get loading="lazy", and which attributes should every image have to avoid layout shift?
Show answer
Put loading="lazy" on the gallery photos only, because the banner is visible immediately and should load right away. Every image, including the banner, should have width and height attributes (numbers without units, matching the file's aspect ratio) so the browser reserves space before the file arrives.