📖 Scenario: You want to embed a video on your webpage that looks good on all devices. To do this, you will use Bootstrap's ratio utilities to keep the video in a fixed aspect ratio, so it doesn't look stretched or squished.
🎯 Goal: Create a responsive embedded video using Bootstrap's ratio utilities with a 16:9 aspect ratio.
📋 What You'll Learn
Use a div with Bootstrap's ratio class to set the aspect ratio
Set the aspect ratio to 16:9 using the appropriate Bootstrap class
Embed a YouTube video inside the ratio container using an iframe
Make sure the iframe has the correct attributes for accessibility and responsiveness
💡 Why This Matters
🌍 Real World
Embedding videos and other media in a responsive way is common for blogs, tutorials, and marketing websites to ensure good viewing on all devices.
💼 Career
Knowing how to use Bootstrap's ratio utilities helps front-end developers create responsive layouts quickly and accessibly, a valuable skill in web development jobs.
Progress0 / 4 steps
1
Create the HTML skeleton with Bootstrap CSS link
Write the basic HTML5 structure with lang="en", meta charset="UTF-8", and viewport meta tag. Include the Bootstrap 5 CSS CDN link inside the <head>. Add an empty <body> section.
Bootsrap
Hint
Remember to include the Bootstrap CSS link inside the <head> section for styling.
2
Add a container div with Bootstrap's ratio class for 16:9 aspect ratio
Inside the <body>, add a <div> with the classes ratio and ratio-16x9. This will create a container that keeps a 16:9 aspect ratio.
Bootsrap
Hint
Use both ratio and ratio-16x9 classes on the same div.
3
Embed a YouTube video inside the ratio container
Inside the div with classes ratio and ratio-16x9, add an iframe with src="https://www.youtube.com/embed/dQw4w9WgXcQ". Also add title="YouTube video player", allowfullscreen, and allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" attributes for accessibility and functionality.
Bootsrap
Hint
Make sure the iframe has all the required attributes for accessibility and functionality.
4
Add a heading and container for better structure and responsiveness
Above the div with the ratio classes, add a <main> element. Inside <main>, add a <div> with class container. Inside this container, add an <h1> with the text Responsive Video Embed. Move the ratio div inside this container below the heading.
Bootsrap
Hint
Use semantic <main> and Bootstrap's container class for better page structure and responsiveness.
Practice
(1/5)
1. What is the main purpose of Bootstrap's ratio utility classes like ratio-16x9?
easy
A. To change the background color of embedded content
B. To add borders around embedded content
C. To keep embedded videos or iframes responsive and maintain their aspect ratio
D. To add padding inside embedded content
Solution
Step 1: Understand the purpose of ratio utilities
Bootstrap's ratio utilities are designed to keep embedded content like videos or iframes responsive by maintaining a fixed aspect ratio.
Step 2: Identify what ratio classes do
Classes like ratio-16x9 set the width and height ratio so the content scales properly on different screen sizes.
Final Answer:
To keep embedded videos or iframes responsive and maintain their aspect ratio -> Option C
Quick Check:
Ratio utilities = responsive aspect ratio [OK]
Hint: Ratio classes keep embeds sized correctly on all screens [OK]
Common Mistakes:
Thinking ratio classes add borders or colors
Confusing ratio with padding or margin utilities
Using ratio classes without wrapping content in a div
2. Which is the correct HTML structure to use Bootstrap's ratio utility for a 4:3 embedded video?
easy
A.
B.
C.
D.
Solution
Step 1: Check the required wrapper and classes
Bootstrap requires a div with class ratio plus the aspect ratio class like ratio-4x3 wrapping the embedded content.
Step 2: Verify the iframe is inside the wrapper
The iframe should not have the ratio class directly; it must be inside the div with the ratio classes.
Final Answer:
<div class='ratio ratio-4x3'><iframe src='video.mp4'></iframe></div> -> Option A
Quick Check:
Wrapper div with ratio classes = correct syntax [OK]
Hint: Wrap iframe in div with both 'ratio' and ratio-size classes [OK]
Common Mistakes:
Applying ratio class directly on iframe
Missing the 'ratio' base class on the wrapper div
Using only one class without the other
3. What will be the visual result of this code snippet?
B. Missing the base class ratio on the wrapper div
C. The ratio class should be on the iframe, not the div
D. The src attribute value is invalid
Solution
Step 1: Check the required classes for ratio utility
Bootstrap requires the wrapper div to have the base class ratio along with the aspect ratio class like ratio-16x9.
Step 2: Verify the given code
The div only has ratio-16x9 but is missing the base ratio class, so the aspect ratio styling won't apply correctly.
Final Answer:
Missing the base class ratio on the wrapper div -> Option B
Quick Check:
Always include 'ratio' base class with ratio-size class [OK]
Hint: Always use both 'ratio' and ratio-size classes on wrapper div [OK]
Common Mistakes:
Using ratio class on iframe instead of div
Forgetting to add the base 'ratio' class
Assuming iframe needs ratio classes
5. You want to embed a video that should always appear as a perfect square on all devices, but also add a subtle shadow around it. Which Bootstrap classes and structure should you use?
hard
A. <div class='ratio ratio-1x1 shadow-sm'><iframe src='video.mp4' title='Video'></iframe></div>
B. <iframe class='ratio-1x1 shadow-sm' src='video.mp4' title='Video'></iframe>
C. <div class='ratio-16x9 shadow-sm'><iframe src='video.mp4' title='Video'></iframe></div>
D. <div class='shadow-sm'><iframe class='ratio ratio-1x1' src='video.mp4' title='Video'></iframe></div>
Solution
Step 1: Choose the correct ratio class for a square
The class ratio-1x1 ensures the embed is a perfect square on all devices.
Step 2: Add shadow styling correctly
The shadow utility shadow-sm should be added to the same wrapper div that holds the ratio classes to apply the shadow around the embed.
Step 3: Confirm correct HTML structure
The iframe should be inside the div with both ratio and shadow-sm classes. Applying ratio classes directly on iframe is incorrect.
Final Answer:
<div class='ratio ratio-1x1 shadow-sm'><iframe src='video.mp4' title='Video'></iframe></div> -> Option A
Quick Check:
Wrapper div with ratio and shadow classes = correct [OK]
Hint: Add shadow class to ratio wrapper div for styling [OK]
Common Mistakes:
Putting ratio classes on iframe instead of div
Using wrong ratio class for square shape
Adding shadow to iframe instead of wrapper
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.