Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 604
Fero Avatar asked Sep 06 '11 09:09

Fero


People also ask

How do I adjust the iframe height automatically?

Answer: Use the contentWindow Property You can use the JavaScript contentWindow property to make an iFrame automatically adjust its height according to the contents inside it, so that no vertical scrollbar will appear.

How do I make my iframe height 100%?

Next, we put the <iframe> inside this box, making it fill the whole box. To size the <iframe> , we ignore the width="560" height="315" element properties, and instead use the CSS properties width:100%;height:100%; . That's it: a full-width <iframe> with fixed aspect ratio. Enjoy.

Can IFrames be made responsive?

The iframe itself ('the box') can be responsive. But everything inside the iframe is a separate page, and therefore not in the domain of your CSS nor JS.


2 Answers

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> 
like image 167
Naveen Velaga Avatar answered Sep 23 '22 19:09

Naveen Velaga


Not quite sure what the 300 is supposed to mean? Miss typo? However for iframes it would be best to use CSS :) - Ive found befor when importing youtube videos that it ignores inline things.

<style>     #myFrame { width:100%; height:100%; } </style>  <iframe src="html_intro.asp" id="myFrame"> <p>Hi SOF</p> </iframe> 
like image 35
Graeme Leighfield Avatar answered Sep 19 '22 19:09

Graeme Leighfield