Responsive embeds
Ratio helpers keep videos and iframes at the correct shape as the width changes.
In this page:
Syntax
<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
<!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
- Using old embed-responsive classes
- Fixed iframe width and height
- 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: