Automatically resize an iframe when its content height changes
Written byPhuoc Nguyen
Category
Level 2 — Intermediate
Created
13 Sep, 2023
When a page includes an iFrame, we typically set a fixed height using the
`height`
property:<iframe style="height: 400px">
...
</iframe>
But if you'd rather adjust the height of the iFrame dynamically to fit its content, rather than setting a fixed or maximum height, you can do it using JavaScript and the Document Object Model (DOM). In this post, I'll show you how to make it happen.
#Markup
Let's start by attaching the ID attribute to the iframe. This way, we can easily reference the iFrame element later on. It's worth noting that there are no
`max-height`
or `height`
properties being used here.<iframe id="frame">
...
</iframe>
To retrieve the reference to the iframe, we can use the
`getElementById`
function.document.addEventListener('DOMContentLoaded', () => {
const iframeEle = document.getElementById('frame');
});
#Automatically adjusting iFrame height
To make sure an iFrame's height automatically adjusts when its content changes, we first need to track when the content changes size.
One of the best ways to do this is by using the ResizeObserver API, which is available in modern browsers. This API allows you to observe changes to an element's size. By creating a new
`ResizeObserver`
instance and passing in a callback function that will be called whenever there are changes to the size of the tracked element, we can easily keep track of any changes that occur within the iFrame.For example, if we want to track the size of an iFrame's body, we can use this code:
const iframeBody = iframeEle.contentDocument.body;
const ro = new ResizeObserver(function() {
// Called when the body size changes
});
ro.observe(iframeBody);
Here, we create a new
`ResizeObserver`
instance that triggers a callback function whenever there are changes to the size of the element being tracked. We then pass the iFrame's body to the `observe()`
function to start tracking its size.Within the callback function, we can dynamically update the height of the iFrame. This allows us to keep the iFrame's size in sync with the content it displays.
const ro = new ResizeObserver(function() {
iframeEle.style.height = `${iframeBody.scrollHeight}px`;
});
The
`iframeBody.scrollHeight`
property gives you the height of everything inside the iFrame, including margins, padding, and borders.#Conclusion
By following these simple steps, you can easily adjust the height of an iFrame to fit its content using JavaScript and the DOM. This technique is incredibly useful for creating responsive web pages that automatically adjust to the size of their content.
Questions? 🙋
Do you have any questions? Not just about this specific post, but about any topic in front-end development that you'd like to learn more about? If so, feel free to send me a message on Twitter or send me an email. You can find them at the bottom of this page.
I have a long list of upcoming posts, but your questions or ideas for the next one will be my top priority. Let's learn together! Sharing knowledge is the best way to grow 🥷.
Recent posts ⚡
Copy the content of an element to your clipboard
01 Oct, 2023
Make a text area fit its content automatically
30 Sep, 2023
Quickly insert alternate characters while typing
30 Sep, 2023
Zebra-like background
30 Sep, 2023
Add autocomplete to your text area
28 Sep, 2023
Linear scale of a number between two ranges
28 Sep, 2023
Highlight the current line in a text area
27 Sep, 2023
Create your own custom cursor in a text area
27 Sep, 2023
Mirror a text area for improving user experience
26 Sep, 2023
Display the line numbers in a text area
24 Sep, 2023
Select a given line in a text area
24 Sep, 2023
Newsletter 🔔
If you're into front-end technologies and you want to see more of the content I'm creating, then you might want to consider subscribing to my newsletter.
By subscribing, you'll be the first to know about new articles, products, and exclusive promotions.
Don't worry, I won't spam you. And if you ever change your mind, you can unsubscribe at any time.
Phước Nguyễn