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.
- How to set div width to fit content using CSS ?
- How to set the div height to auto-adjust to background size?
- Resize an iframe based on the content
- How to insert HTML content into an iFrame using jQuery?
- CSS | fit-content() Property
- CSS | font-size-adjust Property
- How to adjust CSS for specific zoom level?
- How to get the width and height of an image ?
- How to change the width and height of Twitter Bootstrap's tooltips?
- How to animate div width and height on mouse hover using jQuery ?
- How to dynamically set the height and width of a div element using jQuery ?
- How to dynamically get the content width of a div using AngularJS ?
- How to make div height expand with its content using CSS ?
- How to dynamically get the content height of a div using AngularJS ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.