Auto-Resize iFrame Height on Content Change – iFrame Adjuster
<iframe> elements when their content changes. It works by establishing a postMessage communication channel between the parent page and each embedded frame. This makes your iframes always match their content height.
[data-iframe-adjust] element on the page in a single call.postMessage API with a namespaced key to communicate height data between page and frame.1. Install iframe-adjuster and import it into your entry file:
# NPM $ npm install iframe-adjuster
import 'iframe-adjuster';
2. Load the minified build directly from the local.
<script src="/dist/iframe-adjuster.min.js"></script>
3. Add the data-iframe-adjust attribute to every <iframe> you want the plugin to manage.
The plugin assigns each frame a unique internal ID (
frame-1,frame-2, etc.) stored in adata-iframe-idattribute. You do not need to set this manually.
<!-- The data-iframe-adjust attribute flags this element for the plugin --> <iframe data-iframe-adjust src="contact-form.html" frameborder="0"></iframe> <!-- You can mark multiple iframes on the same page --> <iframe data-iframe-adjust src="newsletter-signup.html" frameborder="0"></iframe>
4. Call iFrameAdjuster() once on the parent page.
You do not call
iFrameAdjuster()from inside the iframe. The plugin loads in both contexts and detects automatically which side it is on. In the frame, it enters “child mode” and starts reporting height. On the parent page, it enters “host mode” and starts listening.
document.addEventListener("DOMContentLoaded", () => {
iFrameAdjuster();
}); 5. Useful tips:
width: 100%; border: none; on the iframe element via CSS. The plugin only manages height.load event listener on each marked iframe. If your iframe loads via JavaScript after the page is ready, call iFrameAdjuster() after you inject the iframe into the DOM.0 or left at the browser default. The plugin overrides it on the first animation frame.aspect-ratio or the padding-bottom hack covers the need with zero JavaScript.Q: Does the plugin work with cross-origin iframes?
A: The postMessage call is sent with "*" as the target origin, so it sends across origins. The receiving frame still needs to load the iFrame Adjuster script. If your CSP policy restricts postMessage, you need to update your headers to permit it.
Q: Will the plugin conflict with other postMessage listeners on my page?
A: No. Every message the plugin sends includes a key: "iframe-adjuster" property. The event handlers check for this key at the top of the callback and return early for any message that does not match.
Q: Does it support iframes that load content dynamically via JavaScript (SPA routes)?
A: Yes, with a caveat. The requestAnimationFrame loop runs continuously and picks up height changes regardless of what triggered them, including client-side route changes.
The post Auto-Resize iFrame Height on Content Change – iFrame Adjuster appeared first on CSS Script.
A series of intrusions in early 2026 in which threat actors compromised FortiGate Next-Generation Firewalls…
Amy Guimond, who grew up in Henniker, moved back to town a few years ago…
Town officials in Pembroke have learned from past mistakes. This year, when the wireless microphone…
A long-vacant house on Airport Road sustained major damage in a fire early Saturday morning.…
Amy Bogart has had enough of the state failing to adequately fund education programs, such…
By the time Kelly Bokhan came to Loudon’s town meeting, she felt her wallet was…
This website uses cookies.