Course contentsShow
← Web Development
Lesson 1267 of 2,98222. Responsive Images and MediaFree lesson

Responsive Video Embeds

Apply aspect-ratio or padding techniques to create responsive containers for iframe video embeds (YouTube, Vimeo).

Responsive Video Embeds

What you'll learn: How to make embedded videos (like YouTube or Vimeo) scale fluidly across all screen sizes without breaking their layout.


The Problem with Iframe Videos

When you embed a video using an <iframe> (YouTube, Vimeo, etc.), it comes with fixed width and height attributes. On smaller screens, these fixed dimensions can overflow their container or look awkwardly sized. We need the video to shrink and grow proportionally—just like responsive images do.

Solution 1: The aspect-ratio Property

The modern approach uses the aspect-ratio CSS property you've already learned. Wrap your iframe in a container and let CSS handle the sizing:

.video-container {
  width: 100%; /* Full width of parent */
  aspect-ratio: 16 / 9; /* Maintain video proportions */
}

.video-container iframe {
  width: 100%;
  height: 100%;
}

The iframe automatically fills the container while maintaining the 16:9 ratio.

Solution 2: The Padding-Bottom Technique (Legacy)

For older browsers, use the padding-bottom hack. Since padding percentages are based on width, we can create a proportional box:

.video-wrapper {
  position: relative;
  padding-bottom: 56.25%; /* 9/16 = 0.5625 = 56.25% for 16:9 */
  height: 0;
}

.video-wrapper iframe {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

Why This Matters

Think of it like a picture frame that always stays proportional—whether it's on a phone or desktop, the video never gets squished or cut off. This prevents layout shift and maintains visual harmony across devices.

Key Takeaway: Use aspect-ratio or the padding-bottom technique to create fluid containers that keep embedded videos responsive and perfectly proportioned on any screen size.