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.
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.
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 :
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)
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.