How to set iframe height:auto in HTML5

18,531

I see no problem in your code, but don't forget the style tag

I tested it on IE 11 and it works.

<div class="stockIframe ">
    <iframe src="http://google.com" onload="autoResize(this)" height="100%" width="100"></iframe> 
</div>

<style>
   html,body { height: 100% }
   .stockIframe {  width:100%; height:100%; }
   .stockIframe iframe {  width:100%; height:100%; border:0;overflow:hidden }
</style>

<script>
   function autoResize(i) {
     var iframeHeight=
     (i).contentWindow.document.body.scrollHeight;
     (i).height=iframeHeight+20;
   } 
</script>
Share:
18,531
user3400080
Author by

user3400080

Updated on June 09, 2022

Comments

  • user3400080
    user3400080 almost 2 years

    I'm trying to make iframe height auto(100%), so searched lot of examples.

    But the problem is... it does not working good in IE, only works in Chrome.

    <div class="stockIframe ">
    <iframe src="http://google.com" onload="autoResize(this)" height="100%" width="100%"></iframe> 
    </div>
    
    html,body { height: 100% }
    .stockIframe {  width:100%; height:100%; }
    .stockIframe iframe {  width:100%; height:100%; border:0;overflow:hidden }
    
    
    <script>
    function autoResize(i)
    {
        var iframeHeight=
        (i).contentWindow.document.body.scrollHeight;
        (i).height=iframeHeight+20;
    }
    
    
    </script>
    

    Anyone know how to fix it ?