C

CSS Handbook

Clean • Professional

CSS Responsive Videos

2 minute

CSS Responsive Videos

Just like images, videos must adapt to different screen sizes and devices to ensure a smooth user experience, faster loading, and a clean layout. Fixed-size videos can look broken on mobile screens, which is why responsive videos are an essential part of modern web design.

Responsive videos automatically adjust their size and aspect ratio depending on the screen—whether it’s mobile, tablet, or desktop. Instead of staying fixed, they scale fluidly so the video looks perfect everywhere.

Example: A YouTube video that looks great on a laptop but also resizes neatly to fit on a small mobile screen.

Why Responsive Videos Matter

  • Mobile users may see cropped or oversized videos without responsiveness.
  • Desktop users need high-resolution playback, while mobile can use smaller streams.
  • Fixed-size videos can break page layouts or cause overflow.
  • Proper responsive videos improve SEO, accessibility, and UX.

Embedding Videos in HTML

Using <video> Element

<video controls>
  <source src="video.mp4" type="video/mp4">
  <source src="video.webm" type="video/webm">
  Your browser does not support the video tag.
</video>

Attributes:

AttributeDescription
controlsShows play/pause, volume, and timeline.
autoplayStarts playback automatically (use cautiously).
loopPlays video repeatedly.
mutedStarts video muted (required for autoplay in some browsers).
posterDisplays an image before the video loads.
preloadauto, metadata, none — hints how much to preload.

Embedding Third-Party Videos

  • YouTube / Vimeo: Use <iframe> embedding.
<iframe src="<https://www.youtube.com/embed/VIDEO_ID>"
        title="Sample Video"
        frameborder="0"
        allowfullscreen></iframe>

CSS Techniques for Responsive Videos

Videos often need a container wrapper to maintain aspect ratio and fit different devices.

Aspect Ratio with Padding Hack

<div class="video-wrapper">
  <iframe src="<https://www.youtube.com/embed/VIDEO_ID>" frameborder="0" allowfullscreen></iframe>
</div>
.video-wrapper {
  position: relative;
  width: 100%;
  padding-top: 56.25%; /* 16:9 Aspect Ratio */
}

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

Using object-fit for <video> Cropping

video {
  width: 100%;
  height: 300px;
  object-fit: cover; /* crop to fit container */
  object-position: center; /* center the crop */
}

Performance Optimization

TechniqueBenefit
Use optimized formatsMP4, WebM, or AV1 for modern browsers.
Lazy-loading videosLoad videos only when in viewport.
Autoplay mutedAvoids blocking autoplay on mobile.
Smaller resolutions for mobileReduce bandwidth and load time.
Preload wiselypreload="metadata" to save bandwidth.

Example:

<video controls preload="metadata" poster="thumbnail.jpg" width="100%">
  <source src="video.mp4" type="video/mp4">
  <source src="video.webm" type="video/webm">
  Your browser does not support the video tag.
</video>

Accessibility Best Practices

Always provide captions/subtitles for accessibility:

  • Include a poster image for users with slow connections.
<track src="captions.vtt" kind="subtitles" srclang="en" label="English">

Full Example of a Responsive Video Embed

<div class="video-wrapper">
  <iframe src="https://www.youtube.com/embed/VIDEO_ID"
          title="Responsive Video Example"
          frameborder="0"
          allowfullscreen></iframe>
</div>
.video-wrapper {
  position: relative;
  width: 100%;
  padding-top: 56.25%; /* 16:9 Aspect Ratio */
}

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

 

Article 0 of 0