Responsive images and media
Overview
Images are often the largest things on a page — in bytes and in layout. Responsive images need to do two jobs:
- Fit the layout: never overflow, keep a consistent shape, and not make the page jump when they load. That’s CSS.
- 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:
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>:
<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:
.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) */
}coveris right for photos and thumbnails.containis right for logos and product shots, which mustn’t be cropped.object-positionaccepts keywords or percentages (50% 20%) to choose the focal point.
Circles
.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:
.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
<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"
/>srcsetlists the available files and their real widths (400w= 400 pixels wide).sizestells 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:
<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:
<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
<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:
widthandheightattributes,max-inline-size: 100%,alttext,srcset+sizesfor big images,loading="lazy"below the fold, andaspect-ratio+object-fitfor consistent shapes.
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.
<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><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; }
img { max-inline-size: 100%; block-size: auto; display: block; }
.thumb {
inline-size: 100%;
aspect-ratio: 16 / 9;
object-fit: cover;
object-position: top;
}
.avatar {
inline-size: 4rem;
aspect-ratio: 1;
object-fit: cover;
border-radius: 50%;
}
.video {
inline-size: 100%;
block-size: auto;
aspect-ratio: 16 / 9;
border: 0;
}
</style>Summary
img { max-inline-size: 100%; block-size: auto; }stops images overflowing their containers.- Set
widthandheightattributes so the browser reserves the right space before the image loads. aspect-ratioplusobject-fit: covercrops any image to a consistent shape;object-positionpicks the focus.srcsetandsizeslet the browser download the smallest file that looks sharp;<picture>switches formats or crops.- Embeds and video use
inline-size: 100%withaspect-ratio: 16 / 9— no padding hacks needed.