How do you prevent responsive media from causing layout shifts?

Prevent responsive media layout shifts by reserving its final space before the resource loads. Images and videos can expose an intrinsic ratio through HTML width and height attributes. Iframes and dynamic embeds usually need a CSS aspect-ratio or a container with a known minimum height. The reserved shape must match the rendered media.
Keep the dimensions even when CSS makes the media fluid. The browser uses them to calculate a ratio, while max-width or width controls the displayed size. For art-directed images whose sources have different shapes, define the component ratio in CSS or ensure each selected source reserves the right space at its breakpoint.
img, video {
max-width: 100%;
height: auto;
}
.video-frame {
width: 100%;
aspect-ratio: 16 / 9;
}
.video-frame > iframe {
width: 100%;
height: 100%;
border: 0;
}
Lazy loading changes when media arrives, so it makes space reservation more visible. Do not insert an embed into an empty zero-height container after the user scrolls to it. Reserve the player, advertisement or map area from the start. A placeholder should use the same dimensions as the final content.
Background images do not size their elements. Give a background section content-driven padding, min-height or aspect ratio, and use a fallback color while the image downloads. Web fonts can also change media captions and adjacent layout, so avoid coupling the media height to a single line of text that may wrap after a font swap.
Measure the page with cache disabled and realistic throttling. Browser performance tools can identify layout-shift sources and show which element moved. Test narrow and wide viewports, lazy-loaded content below the fold and consent-gated embeds. Fix the box that changed size instead of freezing the whole page with arbitrary fixed heights.