Click here to Skip to main content
Click here to Skip to main content

How To Adjust IFrame Height on Its Content

, 6 Jul 2007
Rate this:
Please Sign up or sign in to vote.
How to adjust an iFrame height to its content: tested in Mozilla, Opera, Internet Explorer


Some browsers do not support all W3C standards and you will face a problem when you want to adjust IFrame height on its content using JavaScript. I have spent some hours experimenting with different DOM-objects and I have found quite a good solution for Mozilla, Internet Explorer and Opera browsers.

Using the Code

If you have IFrame in your HTML, the first step is to add onload handler as follows:

<iframe id='myFrame' 

frameborder=0 scrolling=no width=100%  



Then you need to place such a script BEFORE the IFrame element:

 <script type="text/javascript">
    function getElement(aID)
        return (document.getElementById) ?
            document.getElementById(aID) : 


    function getIFrameDocument(aID){ 
        var rv = null; 
        var frame=getElement(aID);
        // if contentDocument exists, W3C 

compliant (e.g. Mozilla) 
        if (frame.contentDocument)
            rv = frame.contentDocument;
        else // bad Internet Explorer  ;)
            rv = document.frames[aID].document;
        return rv;

    function adjustMyFrameHeight()
        var frame = getElement

        var frameDoc = getIFrameDocument

        frame.height = frameDoc.body.offsetHeight;


  • 6th July, 2007: Initial post


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
Ukraine Ukraine
No Biography provided

Comments and Discussions

GeneralMy vote of 4 Pinmemberamanalam14-Oct-10 4:04 
QuestionDoesn't work? PinmemberBen Adler14-Sep-10 8:52 
Generalserver iframe PinmemberPavel Yermalovich10-May-10 12:16 
Generalrunat="server" PinmemberPavel Yermalovich10-May-10 12:09 
it works properly when i use iframe without runat="server"
Generalrunat="server" PinmemberPavel Yermalovich10-May-10 12:06 
GeneralOnly IE PinmemberDanGetz22-Aug-09 21:47 
GeneralI had to make a change for IE8 PinmemberDanGetz22-Aug-09 20:55 
GeneralGreat Job PinmemberMember 39633857-Jul-09 6:09 
GeneralDoesn't get smaller Pinmemberpinow17-Apr-09 1:45 
Generalfunction improvement Pinmemberlanello9-Mar-09 3:19 
GeneralSmall Typo PinmemberThe JZ28-Oct-08 9:17 
Generalissue Pinmember_test_123_16-Jul-07 23:35 
GeneralRe: issue Pinmembermeglio17-Jul-07 0:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 6 Jul 2007
Article Copyright 2007 by meglio
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid