Adjust width and height of iframe to fit with content in it Adjust width and height of iframe to fit with content in it javascript javascript

Adjust width and height of iframe to fit with content in it


<script type="application/javascript">function resizeIFrameToFitContent( iFrame ) {    iFrame.width  = iFrame.contentWindow.document.body.scrollWidth;    iFrame.height = iFrame.contentWindow.document.body.scrollHeight;}window.addEventListener('DOMContentLoaded', function(e) {    var iFrame = document.getElementById( 'iFrame1' );    resizeIFrameToFitContent( iFrame );    // or, to resize all iframes:    var iframes = document.querySelectorAll("iframe");    for( var i = 0; i < iframes.length; i++) {        resizeIFrameToFitContent( iframes[i] );    }} );</script><iframe src="usagelogs/default.aspx" id="iFrame1"></iframe>


one-liner solution for embeds:starts with a min-size and increases to content size. no need for script tags.

<iframe src="http://URL_HERE.html" onload='javascript:(function(o){o.style.height=o.contentWindow.document.body.scrollHeight+"px";}(this));' style="height:200px;width:100%;border:none;overflow:hidden;"></iframe>