How to adjust the width and height of iframe to fit with content in it ?

Using iframe tag the content inside the tag is displayed with a default size if the height and width are not specified. thou the height and width are specified then also the content of the iframe tag is not displayed in the same size of the main content. It is difficult to set the size of the content in the iframe tag as same as the main content. So its need to be dynamically set the content size when the content of iframe is loaded on the web page. Because its not possible to set the height and width all the time while the same code execute with a different content.

There is a way to make it dynamically by using some attribute of JavaScript.
The attributes used here are,

  • contentWindow : This property returns the Window object used by an iframe element, basically its defines the iframe window.
  • scrollHeight : This property defines the entire height of an element in pixels, the element is iframe.
  • scrollWidth : This property defines the entire width of an element in pixels, the element is iframe.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
<h4 style="color:#006400; font-size:24px;">
Adjust width and height of iframe to fit 
with content in it using JavaScript</h4>
</head>
<body>
    <iframe style="width: 100%;border:3px solid black; 
     " src="iframe Pge.html" id="Iframe"></iframe>
    <!--iframe tag-->
      
    <script>
        // Selecting the iframe element
        var frame = document.getElementById("Iframe");
          
        // Adjusting the iframe height onload event
        frame.onload = function()
        // function execute while load the iframe
        {
          // set the height of the iframe as 
          // the height of the iframe content
          frame.style.height = 
          frame.contentWindow.document.body.scrollHeight + 'px';
           
  
         // set the width of the iframe as the 
         // width of the iframe content
         frame.style.width  = 
          frame.contentWindow.document.body.scrollWidth+'px';
              
        }
        </script>
</body>
</html>                                                        

chevron_right


Output :

Before adjusting the height and width of iframe the site looks like-

After adjusting the height and width of iframe the site looks like-

The html code for the page what is used inside the iframe :



filter_none

edit
close

play_arrow

link
brightness_4
code

<html >
<head>
  
</head>
<body style="margin:0px;">
    <table style="width:100%; border-collapse:collapse;
         font:14px Arial,sans-serif;">
    <tr>
     <td colspan="2" style="padding:10px; background-color:#16641a;">
     <h1 style="font-size:24px; color:#fbfffb;">
        Welcome to GeeksforGeeks</h1>
     </td>
        </tr>
        <tr style="height:300px;">
        <td style="width:20%; padding:20px; 
            background-color:#ffffff; ">
        <ul style="list-style:none; padding:0px; 
               line-height:24px;">
        <li><a href="#" style="color:rgb(70, 192, 59);">
           Coding</a></li><br>
        <li><a href="#" style="color:rgb(70, 192, 59);;">
            WebTechnology</a></li><br>
        <li><a href="#" style="color:rgb(70, 192, 59);">
           DSA</a></li>
          </ul>
      </td>
        <td style="padding:20px; 
        background-color:#5c9b37; vertical-align:top;">
           <h2>Way to gain knowledge</h2>
        <ul style="list-style:none; padding:0px; line-height:24px;">
        <li style="color:#ffffff;">Learn</a></li><br>
        <li style="color:#ffffff;">Practice</a></li><br>
        <li style="color:#ffffff;">Apply to real world</a></li>
        </ul>
        </td>
    </tr>
    <tr>
       <td colspan="2" style="padding:5px; 
          background-color:#2b2a2a; text-align:center;">
      <p style="color:#ffffff;">copyright © 
         geeksforgeeks, Some rights reserved</p>
  
            </td>
        </tr>
    </table>
</body>
</html>

chevron_right


Another way set the height and width of the iframe to fit with content is to set the Height and Width to 100%,

<iframe width="100%" height="100%" src="URL" ></iframe>

(This may not be applicable for all cases)

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<html
 
       <head
       <h4 style="color:#006400; font-size:24px;">
     Adjust width and height manually to fit iframe content</h4>
       </head
         
       <body
           <div id="frame"
               <iframe  width="100%" height="100%" 
                border:3px solid black;" src="iframe Pge.html" 
                   id="iFrame1" ></iframe>
           </div
       </body
         
       </html

chevron_right


Output :

Same as previous,

Another way set the height and width of the iframe to fit with content is to use resize property of CSS, this method is not dynamic, but useful for some time.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
     <head
           <style
                                             
              #frame { 
                  width: 300px; 
                  height: 200px; 
                  resize: both; 
              
           </style
                              
           <h4 style="color:#006400; font-size:24px;">
            Adjust width and height manually to fit 
            iframe content using CSS</h4>
      </head
        
      <body style="background-color:#ffffff"
              <iframe  height ="100%" width="100%" 
               style="border:3px solid black;" 
             src="iframe Pge.html"  id="frame" ></iframe>
      </body
        
  </html

chevron_right


Output :

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.