Source Code -> Iframe with 100% Height

Resizing an IFrame to fill in 100% height
Ever wonder how to create an iframe that fills the entire height (and width) of a page?

You probably tried writing something like:
[cc lang=”html” escaped=”true”]<iframe width=”100%” height=”100%”></iframe>[/cc]
and expected the iframe to fill up whatever it could.
The width=100% does what you expected and fills up the entire width of the page, but the height just stays about 200px.

The problem is that the IFrame is not a block element so its hight is fixed to about 200px unless you set the hight to exact size, e.g.: 550px but in most cases this won’t solve the problem I’ve described earlier.

There are many solutions of the problem some works well some don’t. For example there is a pure CSS solution that will fix it.

The solutions

 – CSS approach

You have to use the following style:
[cc lang=”css”]
html, body, iframe {
margin:0; /* remove any margins of the IFrame and the body tag */
height:100%; /* set height 100% so that it fills the entire view port*/
iframe {
display:block; /* force the iframe to display as block */
height:100%; /* assign 100% height */
width: 100%;
And here is the actual HTML:
[cc lang=”html”]


So this is great solution but it works only on full height page without any other elements. Let’s think of a page where we have header, main menu and footer and we want to stretch the IFrame within the content body of page and in this case we will need some JavaScript which will be run once the main page is loaded or resized:

 – JavaScript solution

Put this in the head of your page:
[cc lang=”javascript”]
function resize_iframe()
var height=window.innerWidth;//Firefox
if (document.body.clientHeight)
//resize the iframe according to the size of the
//window (all these should be on the same line)
parseInt(height – document.getElementById(“glu”).offsetTop-8)+”px”;

// this will resize the iframe every
// time you change the size of the window.
And inside your page’s body create the iframe tag:
[cc lang=”html”]


– JavaScript – second solution

Similar to the previous one there is another JavaScript function that can solve the problem:
[cc lang=”javascript”]function resizeIframe() {
var height = document.documentElement.clientHeight;
height -= document.getElementById(‘frame’).offsetTop;
height -= 20; /* whatever you set your body bottom margin/padding to be */
document.getElementById(‘frame’).style.height = height +”px”;
document.getElementById(‘frame’).onload = resizeIframe;
window.onresize = resizeIframe;


You’re more than welcome to share your thoughts on the previous solutions. If you stumbled on another one that hasn’t been mentioned here I’d like to see it and implement it in real world scenario so feel free to post a comment.


Just a guy with strong interest in PHP and Web technologies

Tagged with: , , ,
5 comments on “Source Code -> Iframe with 100% Height
  1. click here says:

    Interesting blog! Is your theme custom made or did you
    download it from somewhere? A design like yours with a few simple tweeks would really make my blog stand out.

    Please let me know where you got your design. Cheers

  2. Visit Here says:

    As the admin of this site is working, no uncertainty
    very rapidly it will be famous, due to its feature

  3. awesome site says:

    I rarely comment, but i did a few searching and wound up here
    Source Code -> Iframe with 100% Height | Yin Yang Source. And I do have 2 questions for you if it’s allright. Is it only me or does it seem like some of the comments look as if they are written by brain dead people? 😛 And, if you are writing at other social sites, I would like to keep up with anything new you have to post. Could you make a list of all of your social pages like your Facebook page, twitter feed, or linkedin profile?

  4. Kim says:

    This code helped me so much thank you! Saved me alot of headache!

  5. godzilla fighting games says:

    good article it was really enjoyable to read it ,

Leave a Reply to Kim Cancel reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.