Responsive YouTube <iframe> Embedding with Bootstrap
How to properly embed a YouTube video in a Bootstrap 4 or 5 site

Search for a command to run...
How to properly embed a YouTube video in a Bootstrap 4 or 5 site

No comments yet. Be the first to comment.
Two ways to make pop-up images in a Google Sheet

Since last we spoke I’ve been recording several new long and short form videos. Check out the YouTube Channel if you haven’t. I’ve also wrapped up training for a marathon I’ll be racing on March 3rd… 🤞🤞 Now to the sheets… Do you ever feel lost in E...

Originally published for freeCodeCamp In this article I will show you how to allow for multiple items to be selected using the drop-down data validation feature in Google Sheets. Here's the Google Sheet we'll use for the example. You can make a copy...

Originally published for freeCodeCamp Without clean data, your spreadsheet is knocking on death's door. In this tutorial, I will show you two fast ways to clean up the data in your Excel or Google Sheets spreadsheet. When dealing with data sets, esp...

Originally published for freeCodeCamp In this article I will show you how to format cells in Microsoft Excel. We'll be looking particularly at the Accounting format for cells with numbers. At the end of the article I'll give you two bonuses: a short...

It comes close, but if you copy and paste the embed code off of a YouTube video, you can still run into issues. Particularly on mobile. I found this out just this morning when adding a tutorial video to a simple web app I made using Bootstrap 5.

This worked fine, and previewed well on localhost:5500. But, it was screwy on mobile when I previewed the live site on my iPhone 11. The fixed width was extending off the screen to the right. If you're like me, anytime I encounter stuff like this, it causes me an infinite amount of strife.
Luckily, there's a pretty easy fix that Bootstrap documents. Bootstrap 4 handles it this way. Bootstrap 5 handles it this way.
Both have you wrapping a parent html <div> with either .embed-responsive or .ratio (depending on the Bootstrap version) and then having the iframe within that element.
My div looks like this:
<div class="tutorial container text-center my-5 ratio ratio-16x9">
</div>
And I stripped all the fluff out of the YouTube copy/paste embed code so that I was left with this for the iframe:
<iframe src="https://www.youtube.com/embed/qgInM6FH8Lk?rel=0"
allowfullscreen>
</iframe>
There are different semantics with Bootstrap 4 which accomplish the same thing:
div:
<div class="tutorial container text-center my-5
embed-responsive embed-responsive-16by9">
</div>
iframe:
<iframe class="embed-responsive-item"
src="https://www.youtube.com/embed/qgInM6FH8Lk?rel=0"
allowfullscreen>
</iframe>
Interestingly, I accidentally used the Bootstrap 4 solution initially and it worked on mobile while not fully expanding on larger screens.
After having found this original solution, I ran into another use case a couple weeks later. When I wrapped the iframe with ratio and ratio-16x9, I was getting unwanted behavior: the video was getting too big, and it was displaying beneath my Hero Section's Title instead of beside it.
If you're using rows and columns and hit a snag like this, you will want to make sure the div you're adding ratio too is within the div you're using col on.
I also found it useful to style the iframe with min and max widths and heights so it wouldn't shrink too small.
See below:
<!-- =================================HERO================================= -->
</header>
<div class="container-fluid bg-light border-3 border-gold bg-gold-gradient col-xxl-12 px-5 py-5">
<div class="row flex align-items-center g-5 py-5">
<div class="px-5 col-lg-6">
<h1 class="main-font display-1 fw-bold lh-1 mb-3 ">Origami West</h1>
<p class="lead">Super good thing win fast metal big sound.</p>
<div class="d-grid gap-2 d-md-flex justify-content-md-start">
<a href="#beta" class="btn btn-lg btn-outline-gold">Beta Sign-up</a>
</div>
</div>
<div class="col-10 col-sm-8 col-lg-6">
<div class="ratio ratio-16x9 m-0 p-0">
<iframe class="" style="min-width:280px; min-height:157px;" width="560px" height="315px" src="https://www.youtube.com/embed/lwkGjzT-0QA?controls=0" allowfullscreen></iframe>
</div>
</div>
</div>
</div>
Bonus! Here's the video in question. And it's super easy to embed right here on Hashnode. All that's required is a handy percent sign 😀. Here are the embed docs.
%[https://www.youtube.com/watch?v=qgInM6FH8Lk] 👇
And in writing this article, I realized that @Hashnode has made it even easier to find snazzy shortcuts without jumping all the way to the docs if you choose. There's a Guide link at the top of the article next to the Preview link.
This is a great, quick resource for markdown, embeds, and other commonly used tools for blogging here. 👇

Hope this is helpful for you, and you have a great day!