Click here to Skip to main content
14,332,142 members

How To Adjust IFrame Height on Its Content

Rate this:
4.27 (12 votes)
Please Sign up or sign in to vote.
4.27 (12 votes)
6 Jul 2007
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 Pin
amanalam14-Oct-10 3:04
memberamanalam14-Oct-10 3:04 
QuestionDoesn't work? Pin
Ben Adler14-Sep-10 7:52
memberBen Adler14-Sep-10 7:52 
Generalserver iframe Pin
Pavel Yermalovich10-May-10 11:16
memberPavel Yermalovich10-May-10 11:16 
Generalrunat="server" Pin
Pavel Yermalovich10-May-10 11:09
memberPavel Yermalovich10-May-10 11:09 
Generalrunat="server" Pin
Pavel Yermalovich10-May-10 11:06
memberPavel Yermalovich10-May-10 11:06 
What do i need to do if i want my iframe to be server control? i have got error

Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS1026: ) expected

Source Error:


Line 53:         </tr>
Line 54:     </table>
Line 55:     <iframe id="frmLection" frameborder="0" width="100%" scrolling="no"
Line 56:                         src="Lection1.html"  onload='adjustMyFrameHeight();' runat="server"
Line 57:                         >

GeneralOnly IE Pin
DanGetz22-Aug-09 20:47
memberDanGetz22-Aug-09 20:47 
GeneralI had to make a change for IE8 Pin
DanGetz22-Aug-09 19:55
memberDanGetz22-Aug-09 19:55 
GeneralGreat Job Pin
Member 39633857-Jul-09 5:09
memberMember 39633857-Jul-09 5:09 
GeneralDoesn't get smaller Pin
pinow17-Apr-09 0:45
memberpinow17-Apr-09 0:45 
Generalfunction improvement Pin
lanello9-Mar-09 2:19
memberlanello9-Mar-09 2:19 
GeneralSmall Typo Pin
jzonthemtn28-Oct-08 8:17
memberjzonthemtn28-Oct-08 8:17 
Generalissue Pin
_test_123_16-Jul-07 22:35
member_test_123_16-Jul-07 22:35 
GeneralRe: issue Pin
meglio16-Jul-07 23:29
membermeglio16-Jul-07 23:29 

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

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

Posted 6 Jul 2007

Tagged as


22 bookmarked