Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make iframe automatically adjust height according to the contents without using scrollbar? [duplicate]

For example:

<iframe name="Stack" src="http://stackoverflow.com/" width="740"         frameborder="0" scrolling="no" id="iframe"> ... </iframe> 

I want it to be able to adjust its height according to the contents inside it, without using scroll.

like image 441
akashbc Avatar asked Apr 02 '12 11:04

akashbc


People also ask

How do I resize an iframe based on content?

The iframe HTML element is often used to insert contents from another source. Contents which needs resizing is done indirectly using a div tag. The trick is to initiate with a div tag and enclose within an iframe tag.

How can I set my iframe height to 100?

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.

How do I control the size of an iframe?

The width and height inside the embed code can be adjusted by changing the numbers between the quotation marks, shown below. The standard size of an iframe for desktop is a width of "560" and height of "315."

Can you make iframe 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.


1 Answers

Add this to your <head> section:

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

And change your iframe to this:

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

As found on sitepoint discussion.

like image 165
hjpotter92 Avatar answered Oct 21 '22 14:10

hjpotter92