This question already has an answer here:
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.
jQuery's .contents() method method allows us to search through the immediate children of the element in the DOM tree.
jQuery:
Remember that the body of the page inner the iframe must have its height
CSS:
Add this to your
<head>
section:And change your iframe to this:
As found on sitepoint discussion.
The suggestion by hjpotter92 does not work in safari! I have made a small adjustment to the script so it now works in Safari as well.
Only change made is resetting height to 0 on every load in order to enable some browsers to decrease height.
Add this to
<head>
tag:And add the following
onload
attribute to your iframe, like soThis works for me (also with multiple iframes on one page):
This works for me (mostly).
Put this at the bottom of your page.
The first half is from ???, and works when there is html in the iframe. The second half sets the iframe to page height (not content height), when iframes class is
iframe_fullHeight
. You can use this if the content is a PDF or other such like, but you have to set the class. Also can only be used when being full height is appropriate.Note: for some reason, when it recalculates after window resize, it gets height wrong.
I did it with AngularJS. Angular doesn't have an ng-load, but a 3rd party module was made; install with bower below, or find it here: https://github.com/andrefarzat/ng-load
Get the ngLoad directive:
bower install ng-load --save
Setup your iframe:
Controller resizeIframe function: