107

I am using Bootstrap.

I have a text which contains 2 or 3 iframes based embed videos.

This data is fetched from database.

How can I make these iframes responsive?

Example Code:

<div class="row">

   <div class="col-lg-12 col-md-12 col-sm-12">

     SOME TEXT SOME TEXT SOME TEXT SOME TEXT
     SOME TEXT SOME TEXT SOME TEXT SOME TEXT

     <iframe src="http://youtube.com/...."></iframe>

     SOME TEXT SOME TEXT SOME TEXT SOME TEXT
     SOME TEXT SOME TEXT SOME TEXT SOME TEXT

     <iframe src="http://youtube.com/...."></iframe>


     SOME TEXT SOME TEXT SOME TEXT SOME TEXT
     SOME TEXT SOME TEXT SOME TEXT SOME TEXT

     <iframe src="http://youtube.com/...."></iframe> 

   </div>

</div>

This is a dynamic data. How can I make it responsive?

Christina
  • 34,296
  • 17
  • 83
  • 119
New Co
  • 1,587
  • 3
  • 15
  • 18
  • possible duplicate of [Making an iframe responsive](http://stackoverflow.com/questions/17838607/making-an-iframe-responsive) – mpgn Aug 10 '14 at 11:25
  • 1
    It's good to note which version of Bootstrap. I've answered this with two options. – Christina Aug 10 '14 at 20:54

7 Answers7

224

Option 1

With Bootstrap 3.2 you can wrap each iframe in the responsive-embed wrapper of your choice:

http://getbootstrap.com/components/#responsive-embed

<!-- 16:9 aspect ratio -->
<div class="embed-responsive embed-responsive-16by9">
  <iframe class="embed-responsive-item" src="…"></iframe>
</div>

<!-- 4:3 aspect ratio -->
<div class="embed-responsive embed-responsive-4by3">
  <iframe class="embed-responsive-item" src="…"></iframe>
</div>

Option 2

If you don't want to wrap your iframes, you can use FluidVids https://github.com/toddmotto/fluidvids. See demo here: http://toddmotto.com/labs/fluidvids/

   <!-- fluidvids.js -->
    <script src="js/fluidvids.js"></script>
    <script>
    fluidvids.init({
      selector: ['iframe'],
      players: ['www.youtube.com', 'player.vimeo.com']
    });
    </script>
Christina
  • 34,296
  • 17
  • 83
  • 119
  • 2
    unable decrease height of `iframe`. I changed from `100%` to `80%` class `.embed-responsive iframe`. But is giving empty space. after the video. How to avoid this space. – SatyaTNV Aug 26 '15 at 18:38
  • FINALLY this works perfectly! It might be nice to include the obligatory `

    Your browser does not appear to support iframes

    ` between the `iframe` tags, but I'm wondering if that's even relevant these days...thanks again, many (hacky) solutions were close, but this is perfect!
    – svenevs Nov 27 '16 at 18:33
  • 1
    to Option 1: the url to the docs changed to https://getbootstrap.com/docs/3.3/components/#responsive-embed – Alex Aug 24 '17 at 09:36
  • use one aspect ratio and include this to get it work-->> and within iframe tag --> frameborder="0" style="overflow: hidden; overflow-x: hidden; overflow-y: hidden; height: 100%; width: 100%; position: absolute; top: 0px; left: 0px; right: 0px; bottom: 0px; "height="100%" width="100%" – krishna Aug 18 '20 at 12:54
  • The URL changes depending on the version of Bootstrap. Currently referred to as Embeds. https://getbootstrap.com/docs/4.5/utilities/embed/ – Michael Glenn Nov 14 '20 at 14:52
32

Please, Check this out, I hope it's help

<div class="row">
 <iframe class="col-lg-12 col-md-12 col-sm-12" src="http://www.w3schools.com">
 </iframe>
</div>
Mohamed
  • 337
  • 3
  • 2
20

The best solution that worked great for me.

You have to: Copy this code to your main CSS file,

.responsive-video {
    position: relative;
    padding-bottom: 56.25%;
    padding-top: 60px; overflow: hidden;
}

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

and then put your embeded video to

<div class="responsive-video">
    <iframe ></iframe>
</div>

That’s it! Now you can use responsive videos on your site.

PJunior
  • 2,649
  • 1
  • 33
  • 29
sunblast
  • 219
  • 2
  • 7
9

So, youtube gives out the iframe tag as follows:

<iframe width="560" height="315" src="https://www.youtube.com/embed/2EIeUlvHAiM" frameborder="0" allowfullscreen></iframe>

In my case, i just changed it to width="100%" and left the rest as is. It's not the most elegant solution (after all, in different devices you'll get weird ratios) But the video itself does not get deformed, just the frame.

Julián
  • 91
  • 1
  • 1
3

Option 3

To update current iframe

$("iframe").wrap('<div class="embed-responsive embed-responsive-16by9"/>');
$("iframe").addClass('embed-responsive-item');
Ratha Hin
  • 670
  • 7
  • 13
2

Working during August 2020

use this

<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

use one aspect ratio

<div class="embed-responsive embed-responsive-4by3">
  <iframe class="embed-responsive-item" src="…"></iframe>
</div>

within iframe use options

 <iframe class="embed-responsive-item" src="..."
  frameborder="0"
    style="
      overflow: hidden;
      overflow-x: hidden;
      overflow-y: hidden;
      height: 100%;
      width: 100%;
      position: absolute;
      top: 0px;
      left: 0px;
      right: 0px;
      bottom: 0px;
    "
    height="100%"
    width="100%"
  ></iframe>
krishna
  • 101
  • 1
  • 11
1

You can use the below syntax to make iframes responsive in Bootstrap 5:

<div class="ratio ratio-16x9">
  <iframe src="LinkHere" title="YouTube video" allowfullscreen></iframe>
</div>

Aspect ratios other than 16x9:

<div class="ratio ratio-1x1">
  <div>1x1</div>
</div>
<div class="ratio ratio-4x3">
  <div>4x3</div>
</div>
<div class="ratio ratio-16x9">
  <div>16x9</div>
</div>
<div class="ratio ratio-21x9">
  <div>21x9</div>
</div>
Harshana
  • 5,151
  • 1
  • 17
  • 27