iFrame Height Auto (CSS)

后端 未结 8 1929
无人共我
无人共我 2020-12-08 09:54

I am having problems with my iframe. I really want the frame to auto adjust heights according to the content within the iframe. I really want to do this via the CSS without

相关标签:
8条回答
  • 2020-12-08 10:02

    This is my PURE CSS solution :)

    Add, scrolling yes to your iframe.

    <iframe src="your iframe link" width="100%" scrolling="yes" frameborder="0"></iframe>
    

    The trick :)

    <style>
        html, body, iframe { height: 100%; }
        html { overflow: hidden; }
    </style>
    

    You don't need to worry about responsiveness :)

    0 讨论(0)
  • 2020-12-08 10:02

    hjpotter92

    Add this to your section:

    <script>
      function resizeIframe(obj) {
        obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
      }
    </script>
    

    And change your iframe to this:

    <iframe src="..." frameborder="0" scrolling="no" onload="resizeIframe(this)" />
    

    It is posted Here

    It does however use javascript, but it is simple and easy to use code that will fix your problem.

    0 讨论(0)
  • 2020-12-08 10:05

    According to this post

    You need to add the !important css modifier to your height percentages.

    Hope this helps.

    0 讨论(0)
  • 2020-12-08 10:06

    You have to use absolute position along with your desired height. in your CSS, do the following:

    #id-of-iFrame {
        position: absolute;
        height: 100%;
    }
    
    0 讨论(0)
  • 2020-12-08 10:07

    @SweetSpice, use position as absolute in place of relative. It will work

    #frame{
    overflow: hidden;
    width: 860px;
    height: 100%;
    position: absolute;
    }
    
    0 讨论(0)
  • 2020-12-08 10:10

    I had this same issue but found the following that works great:

    The key to creating a responsive YouTube embed is with padding and a container element, which allows you to give it a fixed aspect ratio. You can also use this technique with most other iframe-based embeds, such as slideshows.

    Here is what a typical YouTube embed code looks like, with fixed width and height:

     <iframe width="560" height="315" src="//www.youtube.com/embed/yCOY82UdFrw" 
     frameborder="0" allowfullscreen></iframe>
    

    It would be nice if we could just give it a 100% width, but it won't work as the height remains fixed. What you need to do is wrap it in a container like so (note the class names and removal of the width and height):

     <div class="container">
     <iframe src="//www.youtube.com/embed/yCOY82UdFrw" 
     frameborder="0" allowfullscreen class="video"></iframe>
     </div>
    

    And use the following CSS:

     .container {
        position: relative;
         width: 100%;
         height: 0;
         padding-bottom: 56.25%;
     }
     .video {
         position: absolute;
         top: 0;
         left: 0;
         width: 100%;
         height: 100%;
     }
    

    Here is the page I found the solution on:

    https://www.h3xed.com/web-development/how-to-make-a-responsive-100-width-youtube-iframe-embed

    Depending on your aspect ratio, you will probably need to adjust the padding-bottom: 56.25%; to get the height right.

    0 讨论(0)
提交回复
热议问题