Static & Dynamic Images — UI/UX

Static images (a logo, a cover photo) have a size you know: write the width and height attributes, plus height: auto in the CSS, so the browser reserves their…

You open a marketplace and are about to tap "Buy". Just as your thumb comes down, the product photo finishes loading. Everything under the photo jumps, and you hit another button instead. The photo isn't wrong. What went wrong: the space for it was never reserved.

How you reserve that space depends on the kind of image. A static image is a file chosen when the page is designed and that doesn't change, such as the Mist Homestay logo or the Terrace Room's cover photo. You already know its size and ratio (its width compared with its height). A dynamic image comes from data, such as the room photos Mrs. Sri uploads from her phone or a guest's profile picture. Its size and ratio are only known once the file arrives. Picture the photo board in the homestay's lobby. Mrs. Sri's family photo is framed to fit its size (static). Next to it are empty frames of a fixed size for guests' photos, and guests put in whatever they have: portrait, landscape, big, small. The frame decides the size, and the extra part of the photo goes behind it. The frame is also already hanging before the photo arrives, so no other photo has to move. On a web page, that empty frame is an image box whose size is set before the image file arrives.

Reserve the space: layout shift and CLS

Before an image file arrives, the browser doesn't know its height. With no hint, the <img> (the tag for an image) counts as 0px tall, and the content under it is laid out right away. As soon as the photo arrives, everything under it jumps down. This jump is called a layout shift. If it happens just as a guest is about to tap "Book the room", they may hit another button.

CLS (Cumulative Layout Shift) is a score that measures these jumps. CLS is one of the Core Web Vitals, a set of measures from Google of how comfortable a web page is to use. According to web.dev (Google's guide site for the web), the score of one jump = impact fraction × distance fraction:

The thresholds: 0.1 or less is good, up to 0.25 needs improvement, above 0.25 is poor.

The Terrace Room photo loads with no size hint at all: no width, height or aspect-ratio (all three are covered below). On a phone with a 360 × 640 screen, the room name, the price and the booking button show first. Then the photo arrives and pushes everything down. Is the CLS score still "good"? Guess first, then calculate it together below.

The photo is 312px wide, so a 4:3 photo is 234px tall. The block under it (name, price, button) is 146px tall:

So the answer is no. 0.1446 is past the 0.1 line, in "needs improvement". This table holds the scores Chrome reports for each way of reserving the space. They were measured with a photo that arrives late on purpose:

How the space is reserved 4:3 photo (1200 × 900) Portrait 3:4 photo from Mrs. Sri's phone (900 × 1200)
No size 0.1446 0.2570, poor
width="1200" height="900" 0 0.1124
aspect-ratio: 4 / 3 + object-fit: cover 0 0

Static images: write width and height

<img class="photo" src="terrace-room-800.jpg" width="1200" height="900" alt="...">

<style>
  .photo { display: block; width: 100%; height: auto; }
</style>

Dynamic images: lock the box with aspect-ratio and object-fit

Mrs. Sri's uploads can be portrait or landscape. For example, she uploads a 900 × 1200 portrait photo. The 4:3 box from those width and height attributes becomes 312 × 416 as soon as the file arrives, so the content under it jumps again: 0.1124. For dynamic images, it's the box that gets locked:

.photo {
  display: block;
  width: 100%;
  aspect-ratio: 4 / 3;      /* the box is always 4:3, whatever the file */
  object-fit: cover;        /* fill the box, crop what's left over */
  background: #e2e8f0;      /* the colour while the photo hasn't arrived or failed */
}

The right file size: srcset and sizes

The Terrace Room photo is ready in three files of different widths. This code lets the browser pick one of them:

<img class="photo"
  src="terrace-room-800.jpg"
  srcset="terrace-room-480.jpg 480w, terrace-room-800.jpg 800w, terrace-room-1200.jpg 1200w"
  sizes="(min-width: 1248px) 384px, (min-width: 768px) calc((100vw - 96px) / 3), calc(100vw - 48px)"
  width="1200" height="900" alt="...">

Lazy loading: only for what's far down

loading="lazy" makes the browser load an image only when it gets close to the screen. It suits the Garden Room photo far down the page, or Mrs. Sri's photo in the footer (the bottom part of the page): a guest who doesn't scroll doesn't need to download them. Don't put it on the Terrace Room photo shown at the very top. There, lazy only delays the image seen first. In the exercise, the image is a photo far below the first screen (what you see before you scroll), so lazy fits.

Alt text for the booking decision

WCAG 1.1.1 asks every meaningful image to have a text alternative: the alt attribute. Screen readers read that text out in place of the image. Write what a guest needs to decide, not the file name:

alt The problem
alt="IMG_2034.jpg" or alt="photo" says nothing
alt="Terrace Room" repeats the card's heading
alt="Double bed facing a big window; outside, Mount Prau under the morning mist" right: the guest learns about the bed and the view

An image that's only decoration gets alt="" (empty, but present), so screen readers skip it. If alt is missing altogether, a screen reader may read out the file name.

⚠️ Photos that jump, squash, or can't be read aloud

TL;DR: Static images (a logo, a cover photo) have a size you know: write the width and height attributes, plus height: auto in the CSS, so the browser reserves their space. Dynamic images (uploads, avatars or profile pictures) have a ratio you don't know: lock the box with aspect-ratio: 4 / 3 and object-fit: cover, plus a background colour for while the photo hasn't arrived. cover fills the box and crops the extra without changing the photo's shape, while the default, fill, stretches the photo. With no space reserved, the content under an image jumps. The CLS (the score for those jumps) of one 4:3 photo on a 360 × 640 phone is 0.1446, and good is 0.1 or less. srcset (the list of files) and sizes (the width of the photo's place, called the slot) let the browser pick a file. It picks the smallest file at least slot × DPR wide (DPR: how many screen pixels fill one CSS pixel). loading="lazy" (load an image only when it gets close to the screen) is for images far down, not the top photo. alt (the image's text alternative, WCAG 1.1.1) says what a guest needs to decide. A decorative image gets alt="".

⚡ Reserve the space first

Write:

<img class="photo" src="terrace-room-800.jpg" width="1200" height="900" alt="...">

<style>
  .photo { display: block; width: 100%; height: auto; }
</style>
Guess first: without width and height on an img tag, the browser doesn't know the photo's height before the file arrives. What happens to the content under it when the photo arrives?

It jumps down. This jump is called a layout shift. With no size, the image counts as 0px at first, then the content under it is pushed down when the photo arrives. One 4:3 photo on a 360 × 640 phone gives a CLS (the jump score) of 0.1446, past the 0.1 "good" line.

Meaning: width and height are attributes for the file's real width and height. From them, the browser knows the ratio before the file arrives, so the space is already reserved. width: 100% and height: auto make the photo follow the card's width at that ratio.

⚡ Uploaded photos: lock the box

Write:

.photo {
  display: block;
  width: 100%;
  aspect-ratio: 4 / 3;      /* the box is always 4:3, whatever the file */
  object-fit: cover;        /* fill the box, crop what's left over */
  background: #e2e8f0;      /* the colour while the photo hasn't arrived or failed */
}

Meaning: An uploaded photo's ratio is only known once the file arrives. aspect-ratio locks the box's ratio. object-fit: cover fills the box and crops the extra with no distortion (the photo keeps its shape). The default is fill, which squashes a portrait photo.

⚡ Lazy and alt

Meaning: loading="lazy" makes the browser load an image only when it gets close to the screen. Use it for images far down the page, not the top photo. alt is the image's text alternative, which screen readers read out. Write what a guest needs to decide, such as the bed and the room's view, not the file name. A decorative image gets alt="".

What you will learn