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


For example:

<iframe name="Stack" src="" width="740"
        frameborder="0" scrolling="no" id="iframe"> ...

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

3/8/2013 7:21:00 PM

Accepted Answer

Add this to your <head> section:

  function resizeIframe(obj) { = obj.contentWindow.document.documentElement.scrollHeight + 'px';

And change your iframe to this:

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

As found on sitepoint discussion.

12/11/2019 11:44:58 PM

You can use this library, which both initially sizes your iframe correctly and also keeps it at the right size by detecting whenever the size of the iframe's content changes (either via regular checking in a setInterval or via MutationObserver) and resizing it.

Their is also a React version.

This works with both cross and same domain iframes.


Here is a compact version:

<iframe src="hello.html" sandbox="allow-same-origin"

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:

<script type="text/javascript">
  function resizeIframe(obj){ = 0; = obj.contentWindow.document.body.scrollHeight + 'px';

And add the following onload attribute to your iframe, like so

<iframe onload='resizeIframe(this)'></iframe>

The hjpotter92 answer works well enough in certain cases, but I found the iframe content often got bottom-clipped in Firefox & IE, while fine in Chrome.

The following works well for me and fixes the clipping problem. The code was found at I have made a slight modification to take the onload attribute out of the HTML. Place the following code after the <iframe> HTML and before the closing </body> tag:

<script type="text/javascript">
function getDocHeight(doc) {
    doc = doc || document;
    var body = doc.body, html = doc.documentElement;
    var height = Math.max( body.scrollHeight, body.offsetHeight, 
        html.clientHeight, html.scrollHeight, html.offsetHeight );
    return height;

function setIframeHeight(id) {
    var ifrm = document.getElementById(id);
    var doc = ifrm.contentDocument? ifrm.contentDocument: 
        ifrm.contentWindow.document; = 'hidden'; = "10px"; // reset to minimal height ...
    // IE opt. for bing/msn needs a bit added or scrollbar appears = getDocHeight( doc ) + 4 + "px"; = 'visible';

document.getElementById('ifrm').onload = function() { // Adjust the Id accordingly

Your iframe HTML:

<iframe id="ifrm" src="some-iframe-content.html"></iframe>

Note if you prefer to include the Javascript in the <head> of the document then you can revert to using an inline onload attribute in the iframe HTML, as in the dyn-web web page.


Avoid inline JavaScript; you can use a class:

<iframe src="..." frameborder="0" scrolling="auto" class="iframe-full-height"></iframe>

And reference it with jQuery:

$('.iframe-full-height').on('load', function(){ +'px';