Emmanuel Akinfulubi

HTML and CSSPractical III

Prevent image layout shift with aspect-ratio in CSS

37 Media that keeps its shape

easy

Give a fluid-width image a fixed shape so the layout never reflows as it loads, and fill the box without distorting the picture.

Stuck? Three hints, smallest first
  1. 1. aspect-ratio plus width: 100% means only one dimension is set and the ratio decides the other.
  2. 2. object-fit: cover fills the box and crops; contain fits it all in and leaves gaps.
  3. 3. Setting an explicit height as well leaves aspect-ratio nothing to compute.

index.html

<figure class="media">
  <img
    width="320" height="180"
    alt="A coach pulling out of bay B at first light"
    src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='320' height='180'%3E%3Crect width='320' height='180' fill='%230b3a8f'/%3E%3Ccircle cx='72' cy='60' r='30' fill='%23ffd75e'/%3E%3Crect y='140' width='320' height='40' fill='%2317171a'/%3E%3C/svg%3E">
  <figcaption>16 by 9, whatever width the column is.</figcaption>
</figure>

style.css

.media {
  max-width: 280px;
  margin: 0;
}

.media img {
  display: block;
  width: 100%;         /* one dimension only */
  aspect-ratio: 16 / 9;
  object-fit: cover;   /* fill the box, crop the surplus */
  object-position: center;
  border-radius: 8px;
  background: #f2f3f5;
}

.media figcaption {
  margin-top: 6px;
  color: #5c5c66;
  font-size: 0.8rem;
}
Rendered result. Shown, not machine-checked: there is no browser in the build.
8 checks that prove this answer

Assertions, run in the browser. The markup and styles above are handed to them as html and css, so press Run and each check prints ok. The build fails if a single one fails.

Checks for exercise 37

const ratio = (/aspect-ratio:\s*(\d+)\s*\/\s*(\d+)/.exec(css) || []).slice(1).map(Number);
const heightAt = (width) => Math.round((width * ratio[1]) / ratio[0]);

test('the ratio is 16 by 9', ratio, [16, 9]);
test('so at 280px wide the box is 158px tall', heightAt(280), 158);
test('and at 320px it is 180px', heightAt(320), 180);
test('only the width is set in CSS', /width:\s*100%/.test(css), true);
test('no explicit height competes with the ratio', /[^-]height:\s*\d/.test(css), false);
test('the image fills the box and crops', /object-fit:\s*cover/.test(css), true);
test('the intrinsic size is still on the element, for the pre-load reservation', /width="320" height="180"/.test(html), true);
test('and there is real alternative text', /alt="[^"]{15,}"/.test(html), true);

Output


      

Set one dimension and let aspect-ratio derive the other; set both and the ratio has nothing left to decide, which is why the fifth check refuses an explicit height. object-fit is the companion: without it a fluid image forced into a fixed ratio is stretched, and cover crops instead. The width and height attributes are still worth having even with a ratio in CSS — they are what reserves the box before the stylesheet or the image has arrived.

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.