Responsive images and media

Topic 4 of 5 in Responsive design7 min readBeginnerFree course

Overview

Images are often the largest things on a page — in bytes and in layout. Responsive images need to do two jobs:

  1. Fit the layout: never overflow, keep a consistent shape, and not make the page jump when they load. That’s CSS.
  2. Load the right file: a phone shouldn’t download a 3000px-wide image for a 360px slot. That’s HTML (srcset, sizes, <picture>).

Fitting the layout

Never overflow

Images display at their natural size by default, so a 2000px photo breaks out of a 360px phone screen. One rule fixes it everywhere:

CSS
img, svg, video {
  max-inline-size: 100%;
  block-size: auto;     /* keep the aspect ratio when the width shrinks */
  display: block;       /* removes the gap below inline images */
}

Reserve space with width and height

Always put width and height attributes on <img>:

HTML
<img src="course.jpg" width="1200" height="675" alt="…" />

The browser uses them to calculate the aspect ratio before the image downloads, and reserves exactly the right space. Without them, the image starts at height 0 and the content below jumps down when it loads — a layout shift that’s annoying to read and hurts your Core Web Vitals score. With block-size: auto in your CSS, the attributes don’t fix the displayed size; they only provide the ratio.

Consistent shapes: aspect-ratio and object-fit

User-uploaded thumbnails come in every shape. To show them all at the same shape, set the box’s ratio and tell the image how to fill it:

object-fit decides how an image fills its box. The same 3:2 image in a square box: fill (the default) stretches it, contain shows the whole image with letterbox bars, cover fills the box and crops the overflow, none keeps the natural size, scale-down uses none or contain, whichever is smaller. Add object-position to choose which part stays visible.

CSS
.thumb {
  inline-size: 100%;
  aspect-ratio: 16 / 9;
  object-fit: cover;          /* fill the box, crop the rest */
  object-position: top;       /* keep the top visible (faces, headlines) */
}
  • cover is right for photos and thumbnails.
  • contain is right for logos and product shots, which mustn’t be cropped.
  • object-position accepts keywords or percentages (50% 20%) to choose the focal point.

Circles

CSS
.avatar {
  inline-size: 4rem;
  aspect-ratio: 1;
  object-fit: cover;
  border-radius: 50%;
}

Background images

For decorative images in CSS, background-size: cover and background-position do the same job as object-fit and object-position. Use <img> for anything meaningful (with alt text); use backgrounds for decoration only.

Video and embeds

<video> works like an image. <iframe> embeds (YouTube, maps) have no intrinsic ratio, so give them one:

CSS
.video {
  inline-size: 100%;
  block-size: auto;
  aspect-ratio: 16 / 9;
  border: 0;
}

You may see an old “padding-bottom: 56.25%” wrapper hack for this; aspect-ratio replaces it.

Loading the right file

srcset with widths, and sizes

srcset and sizes let the browser choose: <img src="course-800.jpg" srcset="course-400.jpg 400w, course-800.jpg 800w, course-1600.jpg 1600w" sizes="(width >= 64rem) 33vw, 100vw" alt="…">. A phone (390px wide, 3x density) shows the image at 100vw = 390px, needs about 1170px, and picks the 1600w file. A desktop (1440px, 1x) shows it at 33vw = 475px and picks the 800w file. You describe the files and the layout; the browser picks.

HTML
<img
  src="course-800.jpg"
  srcset="course-400.jpg 400w, course-800.jpg 800w, course-1600.jpg 1600w"
  sizes="(width >= 64rem) 33vw, 100vw"
  width="1600" height="900"
  alt="Students building a grid layout"
/>
  • srcset lists the available files and their real widths (400w = 400 pixels wide).
  • sizes tells the browser how wide the image will be displayed at each viewport size: a third of the viewport on large screens (in a three-column grid), the full width otherwise. The browser needs this because it chooses the file before the CSS layout is known.
  • The browser combines the display width with the screen’s pixel density and picks the smallest file that will look sharp. It may also take the network speed and its cache into account.

sizes should match your CSS layout roughly — it doesn’t have to be exact. If you omit it, the browser assumes 100vw, which over-downloads for images in columns.

Density descriptors

For images displayed at a fixed size (logos, avatars), use x descriptors instead:

HTML
<img src="logo.png" srcset="logo.png 1x, logo@2x.png 2x" width="120" height="40" alt="Academy" />

: formats and art direction

<picture> lets you offer modern formats with a fallback, or a different crop at different sizes:

HTML
<picture>
  <source type="image/avif" srcset="hero.avif" />
  <source type="image/webp" srcset="hero.webp" />
  <source media="(width < 40rem)" srcset="hero-square.jpg" />
  <img src="hero.jpg" width="1600" height="700" alt="…" />
</picture>

The browser uses the first <source> that matches; the <img> is required and is what actually gets laid out and styled.

Lazy loading

HTML
<img src="…" loading="lazy" … />

loading="lazy" defers images below the fold until the user scrolls near them. Don’t lazy-load the main image at the top of the page (the hero) — it delays the most important content. For that one, fetchpriority="high" can help it load sooner.

Every image checklist: width and height attributes, max-inline-size: 100%, alt text, srcset + sizes for big images, loading="lazy" below the fold, and aspect-ratio + object-fit for consistent shapes.

Exercise

A course card with a perfect thumbnail

Images in the starter have different shapes. Make every .thumb fill its card’s width at a 16 / 9 ratio, cropping rather than stretching, with the crop anchored to the top of the image. Make the .avatar a 4rem circle that crops a non-square photo. Make the .video iframe responsive at 16 / 9. Reserve space for the images so the page doesn’t jump when they load.

HTML
<article class="card">
  <img class="thumb" width="400" height="600" alt="Tall photo" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='600'%3E%3Crect width='400' height='600' fill='%23c9d8f8'/%3E%3C/svg%3E" />
  <h3>Grid layout</h3>
  <img class="avatar" width="300" height="200" alt="Instructor" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='200'%3E%3Crect width='300' height='200' fill='%23e9dffc'/%3E%3C/svg%3E" />
</article>
<iframe class="video" title="Course trailer" src="about:blank"></iframe>

<style>
  * { box-sizing: border-box; }
  .card { max-inline-size: 22rem; border: 1px solid #ccc; }
  /* your code here */
</style>

Summary

  • img { max-inline-size: 100%; block-size: auto; } stops images overflowing their containers.
  • Set width and height attributes so the browser reserves the right space before the image loads.
  • aspect-ratio plus object-fit: cover crops any image to a consistent shape; object-position picks the focus.
  • srcset and sizes let the browser download the smallest file that looks sharp; <picture> switches formats or crops.
  • Embeds and video use inline-size: 100% with aspect-ratio: 16 / 9 — no padding hacks needed.