Automatically Adjust iFrame Height According to its Contents Using JavaScript

  2067 views   2 years ago Javascript

Use the contentWindow Property

You can use the JavaScript contentWindow property to make an iFrame automatically adjust its height according to the contents inside it, so that no vertical scrollbar will appear.

Let's try out the following example to understand how it really works:

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<title>JavaScript Auto Adjust iFrame Height Based on Content</title>
        width: 100%;
        border: 2px solid #ccc;
    <iframe src="demo.php" id="myIframe"></iframe>
    // Selecting the iframe element
    var iframe = document.getElementById("myIframe");
    // Adjusting the iframe height onload event
    iframe.onload = function(){ = iframe.contentWindow.document.body.scrollHeight + 'px';


Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]