Auto-adjust iframe height to content without scrollbar

When embedding content from another website or page into an iframe, it is often necessary to adjust the height of the iframe to match the height of the content. This can be done using JavaScript to dynamically calculate and set the height of the iframe based on the height of the content.

To achieve this, we can use the window.postMessage() method to communicate between the parent page and the iframe. In the parent page, we can listen for messages from the iframe and then adjust the height of the iframe accordingly. In the iframe, we can send a message to the parent page containing the height of the content.

Here is an example of how to do this:

In the parent page:

window.addEventListener('message', function(event) {
  if (event.origin !== '') return; // Replace with your iframe URL
  var height = parseInt(;
  document.getElementById('my-iframe').style.height = height + 'px';

<iframe id="my-iframe" src="" frameborder="0"></iframe>

In the iframe:

var height = document.body.scrollHeight;
parent.postMessage(height, ''); // Replace with your parent page URL

This will automatically adjust the height of the iframe to match the height of the content, without the need for a scrollbar. Note that the iframe and parent page must be on the same domain for this to work.

By using this method, we can ensure that our embedded content is always displayed correctly and without any unnecessary scrollbars.

Click to rate this post!
[Total: 0 Average: 0]

Related posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Go up

Below we inform you of the use we make of the data we collect while browsing our pages. You can change your preferences at any time by accessing the link to the Privacy Area that you will find at the bottom of our main page. More Information