iframe dynamic height width change based on inner content

set iframe height based on content - jquery
jquery iframe height auto adjust
how to set iframe height and width dynamically
resize iframe based on content
jquery get iframe content height
iframe size to fit
iframe default height
iframe height of content cross domain

I have iframe with content like below,

    <iframe  frameborder="no" src="http:localhost/com" id="iframe">
     <div style="height:850px;width:700px;">div text </div>

This shows the iframe with hori. and vertical scroll but not the height on inner content of 850px.

how can i change the iframe height dynamiclly based on inner content height n width. Im using jquery inside the content.

Its cross domain js widget so most of the below answers throwing permission denied error

Thanxs, Nithish


if you are using jquery, then you can easily find (and set) the height of any div (in your domain - not on external content) as such:

// get the height - in case we want to factor it into the equation below!!
var myDivHeight = $('iframe').css('height');

// set the height
var docHeight = $(document).css('height');
$('iframe').css('height', docHeight);

hope this helps ...

Automatically Adjust iFrame Height According to its Contents Using , Automatically Adjust iFrame Height According to its Contents Using to make an iFrame automatically adjust its height according to the contents inside it, <title>​JavaScript Auto Adjust iFrame Height Based on Content</title> width: 100%; Setting Iframe Height to Match Content Height. On this page we present JavaScript that sets the height of an iframe to match the height of its content so that regular window scrollbars are used instead of iframe scrollbars. An example demonstrates. The JavaScript consists of two functions: getDocHeight and setIframeHeight.

var newHeight = $("iframe[id='iframeAutenticador']").contents().find("html").height();

use this.

How to Automatically Resize an iframe - Better Programming, The allows web developers to embed one HTML page inside another. To do this, we'd need to dynamically change the iframe's height but security considerations mean it is <iframe id="iframe" src="child.html" style="width:100​%;border:none;"></iframe> Writer and developer based in London. The snippet above creates a function called resize that can be used to dynamically change the height and length of an iFrame based on the size of the content it contains. It will work even if you don't have a predefined width and height of your iFrame in the HTML, however it won't work unless the content within the iFrame comes from the same

You should create a function to measure the height of the content, and set the IFrame height and then call the resize function when the content is loaded.

function resizeIframeToFitContent(iframe) {
    // This function resizes an IFrame object
    // to fit its content.
    // The IFrame tag must have a unique ID attribute.
    iframe.height = document.frames[iframe.id]

Resize Iframe to Fit Content (Same Domain Only), Normally you set and width and height for iframes. If the content inside is bigger, scrollbars have to suffice. The script below attempts to fix that  I'm using iframe to load pages. My iframe is in the main page which does not re-load once loaded. Only the pages in iframe changes based on the link clicked from main page that target the iframe. How do I adjust height of iframe in this case. – this. __curious_geek Nov 4 '09 at 5:56

If domains are the same for both pages:

parent.document.getElementById("iframe").style.height = $(document).height()+"px";

You can use jQuery in parent page too (if its available there).

auto adjust height of iframe based on content, scrollHeight+45) +'px';" scrolling="no" style="width:100%;border:none;"></iframe>​. thank you, Steveriches! i tried it but it seems this still adds the  a) putting the onload to the iframe, the iframe re-configures the height everytime the iframe loads. Also, b) putting the f.style.height to change twice, the iframe has always the correct height, even if i go from a bigger to a smaller linked page. It works for me in ie v7, ff v3.0.7 and Chrome. Hope it comes in handy, Thanks again for sharing

I found this here: stackoverflow.com/questions/1145850/

and expanded upon it to re-size width also...you just add this in your head:

    <script type="text/javascript">
        function getDocHeight(doc) {
            doc = doc || document;
            // stackoverflow.com/questions/1145850/
            var body = doc.body, html = doc.documentElement;
            var height = Math.max( body.scrollHeight, body.offsetHeight, 
                html.clientHeight, html.scrollHeight, html.offsetHeight );
            return height;
        function getDocWidth(doc) {
            doc = doc || document;
            // stackoverflow.com/questions/1145850/
            var body = doc.body, html = doc.documentElement;
            var width = Math.max( body.scrollWidth, body.offsetWidth,
                html.clientWidth, html.scrollWidth, html.offsetWidth );
            return width;
        function setIframeSize(id) {
            var ifrm = document.getElementById(id);
            var doc = ifrm.contentDocument? ifrm.contentDocument: 
            ifrm.style.visibility = 'hidden';
            ifrm.style.height = "10px"; // reset to minimal height ...
            ifrm.style.width = "10px"; // reset to minimal width ...
            // IE opt. for bing/msn needs a bit added or scrollbar appears
            ifrm.style.height = getDocHeight( doc ) + 4 + "px";
            ifrm.style.width = getDocWidth( doc ) + 4 + "px";
            ifrm.style.visibility = 'visible';

Then make sure you have a unique id in your iframe and call the SetIframeSize:

<iframe src="source.html" id="uniqueid" onload="setIframeSize(this.id)"></iframe>

Setting Iframe Element Height to Iframe Content Height, JavaScript to set the height of an iframe to match the height of its content so that regular window scrollbars are used instead of iframe scrollbars. But if the content in the iframe changes, the iframe size may need to be adjusted further. A common use of an iframe is to present a form, so a page reload is unnecessary when the form submits. After the form is submitted, a thank-you page is displayed within the iframe.

Responsive IFRAMEs, The problem, iframes set to 100% become fluid, not responsive. Your iframe should now proportionally resize based on the browser size. The padding-top value is calculated based on the aspect ratio of your content. if the iframe contains dynamic content that changes height when a button is clicked inside the frame? According to spec, yes, that seems to be the case, although using percentages seems to be implemented quite widely. Alternatively, height and width of an iframe can be set using CSS without touching height and width attributes, as far as I can tell. – user234932 Feb 4 '17 at 9:18

How to make iFrame resize based on dynamic content each time the , Is there any way of making the iframe change its height after the content inside changes? Example: http://thc-racing.ucoz.com/index/forum/0-14. Hi, I have an iframe on my page and I need its height to be the height of the content inside it. The iframe source is on the same domain. I have a JS solution for this but I am looking for a CSS

Resize an iframe based on the content, How to apply border inside a table ? The iframe HTML element is often used to insert contents from another source. Contents overflow-x: auto;. resize: both;. position: relative;. z-index: 2;. } iframe {. width: 100%; property that will iFrame automatically adjust its height according to the contents, no scrollbars will appear​. Resize iframe based on content Posted on March 17, 2010 by admin One thing that many are using easyXDM (and other cross-domain hacks) for, is to dynamically change the size of embedded iframes based on the contents size, so as to avoid the scrollbars.

  • possible duplicate of How to auto-size an iFrame? Also, check this one.
  • One cross-domain solution is described in my second link. But imho, it's better to get rid of iframe and switch to script tag.
  • Hi its javascript widget inside that im loading an iframe we can place tht widget anywhere
  • well, as long as you have the id of the main containing div of the 'widget', the logic still remains with a minor change. i.e. use $('#idWidgetcontainer').css('height', docHeight); instead.
  • ok, is this widget definately from inside your own domain?? (see my little bit above [(in your domain - not on external content)]. it's not possible to get metrics on externally generated content.
  • Do NOT use the language attribute anymore. It's deprecated. Btw., you should rather use lowercase markup style.
  • Actually you should use this, provided you're targeting lower versions of IE. This was the only thing that gave me the correct scroll height in IE 8>
  • Hi its javascript widget inside that im loading an iframe we can place tht widget anywhere