Search code examples
htmliframeresize

How to set iframe size dynamically


How should I set the dimensions of an iframe dynamically, so the size is flexible for different viewport sizes?

For example:

<iframe src="html_intro.asp" width="100%" height="300">
  <p>Hi SOF</p>
</iframe>

In this case the height is different depending on the browser's window size. It should be set dynamically depending on the size of the browser window.

At any size, the iframe aspect ratio should be the same. How can this be done?


Solution

  • If you use jquery, it can be done by using $(window).height();

    <iframe src="html_intro.asp" width="100%" class="myIframe">
    <p>Hi SOF</p>
    </iframe>
    
    <script type="text/javascript" language="javascript"> 
    $('.myIframe').css('height', $(window).height()+'px');
    </script>