Community forum for knowledge and support

Updated 12 months ago

Adjusting the height of an iframe to fit its content

At a glance
The community member is looking for a way to have the height of an iframe adjust automatically to fit the content, without any white space or scrollbars. Another community member suggests using JavaScript and the "postMessage" API to achieve this, providing a link to a tutorial. The community members discuss the approach, and the original poster indicates they will follow up to ensure it works for their use case.
Useful resources

Is there a way to have the height of an iframe adjust on its own to fit just the content?
I'm thinking of embedding a form on a website with an iframe but I want it to just show the frame without any white space and without a scroll bar. Any ideas?

T
S
4 comments

There is, it takes a bit of javascript and 'postman', if that doesn't sound daunting let me know, i can probably find a good tutorial.

Would be great to see. Thank you

Check this out:
https://javascriptbit.com/transfer-data-between-parent-window-and-iframe-postmessage-api/

You want this part: "2. Sending data from child iframe to parent window:"

I'd recommend setting it up just like the demo first, to see it work, and then adjust for your use case. My suggestion is to have javascript inside the iframe detect the page height (and re calculate it on page resize, reload) and send that height to the parent via 'message'. Then the parent adjusts the height of the iframe to match.

We've done dynamic/responsive embeddable iframes like this for embeds across thousands of websites for years, its pretty bulletproof as long as you have control over the child completely, and whoever is installing the embed/iframe is okay with your small javascript snippet that runs on the parent. You might be both people in this case! Which would make it easy.

Thank you . I'm just seeing this now.
I will DM you the details to make sure it will work. Thank you

Add a reply
Sign up and join the conversation on Slack