Resize iframe height according to content height in it

Resize iframe height according to content height in it

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Resize iframe height according to content height in it

Embedding a third-party blog or application via an <iframe> is a common pattern, but achieving a seamless, scrollbar-free experience often hits a hard wall immediately: cross-origin restrictions. The specific scenario you described—where a static width works fine but the height needs to be dynamic—is a classic case of the same-origin policy blocking standard JavaScript solutions.

When your main application attempts to read document.body.scrollHeight from within the iframe's parent window, the browser throws an "Access Denied" error. This is not a bug; it is a fundamental security feature designed to prevent malicious sites from stealing data or layout information from other domains. Consequently, simple client-side JavaScript snippets that attempt to calculate the internal content height will fail as soon as the src attribute points to a different domain than your host.

You might consider using Ajax or PHP to fetch the height of the external page and set it dynamically. Unfortunately, this approach encounters the exact same barrier. Since the iframe loads a remote document (e.g., bagtheplanet.blogspot.com), any request from your server trying to scrape that content via HTTP headers will be blocked by Content Security Policy headers sent by the target site. The browser explicitly forbids cross-origin requests unless the target server explicitly allows them via CORS headers, which personal blogs and many third-party sites rarely configure for arbitrary domains.

To bypass these restrictions without forcing a complex proxy setup on every single blog you embed, you must utilize the postMessage API. This mechanism is specifically designed to allow safe communication between windows that do not share the same origin. Instead of your parent window trying to read the child's DOM (which fails), you ask the child frame to tell your parent how tall it should be.

Here is how to implement this pattern:

  1. Modify the iframe source: You cannot simply load the external blog directly. You must wrap that URL in a bridge page that you control. This bridge page loads the external content via an <iframe> itself and listens for messages from the parent.

    <!-- Your controlled bridge page -->
    <iframe src="https://bagtheplanet.blogspot.com/" id="content-frame"></iframe>
    <script>
      // Listen for messages from the external site (if they support it)
      // Or, more commonly, rely on your own wrapper logic
      window.addEventListener('message', (event) => {
        const height = event.data;
        document.getElementById('content-frame').style.height = height + 'px';
      });
    </script>
    
  2. Inject the script into the target: The external blog (bagtheplanet.blogspot.com) needs to know to send its height back to your wrapper. You would typically need to modify their source code or rely on a specific implementation if they offer a "embed mode." If you cannot modify their source, this specific cross-domain calculation is impossible without them cooperating.

However, in many modern use cases, the external site provides an embed script that handles the postMessage handshake automatically. You simply include their provided JS, which listens for messages from your parent window or calculates its own height and exposes it via a global variable or message event.

If you cannot get cooperation from the external site, your only viable option is to accept that the height must be static, or use an iframe sandbox with specific permissions if the target allows CORS headers (which is rare for generic blog sites). Alternatively, you can render the content into your own layout entirely using a server-side proxy that fetches and sanitizes the HTML, stripping out scripts and styles while preserving dimensions, though this introduces significant maintenance overhead regarding broken layouts.

For projects built on Laravel, if you are embedding multiple third-party resources, consider building a dedicated internal view loader service. This service acts as your own proxy, fetching the content through your server's domain (bypassing CORS restrictions entirely) and serving it to the iframe from yourdomain.com/path/to/blog. Since this is now a same-origin request, your parent JavaScript can freely read the DOM height and adjust the container dynamically without permission errors.

The trade-off here is that you lose some of the original site's scripts or specific security contexts, but you regain full control over the layout. If the external provider does not offer an embed API and refuses to enable CORS, there is no robust JavaScript-only solution; postMessage requires the sender (the iframe content) to initiate the height update, which they must allow.

Our network