Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (20.3k points)

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 the scroll.

2 Answers

0 votes
by (40.7k points)

You can try adding this to your <head> section:

<script>

  function resizeIframe(obj) {

    obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';

  }

</script>

And try changing your iframe like this:

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

0 votes
by (37.3k points)

Below is the code for automatically making an iframe that adjusts the height according to the contents without using a scrollbar.

 Code:

<iframe id="myIframe" src="your-page.html" onload="adjustIframeHeight()" style="width:100%; border:none;"></iframe>

<script>

function adjustIframeHeight() {

    var iframe = document.getElementById('myIframe');

    iframe.style.height = iframe.contentWindow.document.body.scrollHeight + 'px';

}

</script>

Related questions

0 votes
1 answer
0 votes
1 answer

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...