← Back to Bootstrap Course | Chapter 10: Responsive Design | Lesson 2 of 7

Responsive embeds

Ratio helpers keep videos and iframes at the correct shape as the width changes.

In this page:

  1. Responsive embeds
Syntax
markup
<div class="ratio ratio-16x9">
  <iframe src="url" allowfullscreen></iframe>
</div>

Responsive embeds

Wrap an iframe, video or embed in .ratio with a ratio class such as .ratio-16x9, .ratio-4x3, .ratio-1x1 or .ratio-21x9. The child fills the box automatically. Custom ratios use the --bs-aspect-ratio CSS variable.

Note: ratio-16x9 is the standard widescreen video shape.

Example: Responsive embeds

markup
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
<div class="container" style="max-width: 32rem">
  <div class="ratio ratio-16x9 bg-dark text-white d-flex align-items-center justify-content-center">
    <div class="d-flex align-items-center justify-content-center">16:9 embed area (iframe or video goes here)</div>
  </div>
</div>
</body>
</html>
Live Example
Related Topics
Common Mistakes
  1. Using old embed-responsive classes
  2. Fixed iframe width and height
  3. Forgetting the ratio wrapper
Chapter Summary
  • .ratio wraps embeds
  • ratio-16x9, 4x3, 1x1, 21x9
  • The child fills the box
  • Custom ratios use a CSS variable
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.