Niv
Niv

Reputation: 2312

How to make an iframe take up all remaining room

I have the following code:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="shortcut icon" href="res/favicon.ico" />
<title>My website</title>

<style type="text/css">
    .wrap {
    position:relative;
    margin:0 auto;
    width:900px;
    text-align:center;
}

#header, #footer {
    width:100%;
    float:left;
}

#footer {
    position:fixed;
    bottom:0;
    z-index:999999;
}
</style>

</head>
<body>
  <div id="header">
      <div class="wrap">
           <div class="logo">
                <h1>Header</h1>
           </div>
           <div>Menu goes here</div>
      </div>
 </div>

 <iframe src="http://www.cnn.com"></iframe>

  <div id="footer">
      <div class="wrap">
           <h2>Footer</h2>
      </div>
 </div>
</body>
</html>

How can I setup the iframe so that it'll take all the remaining width and height between the header and the footer?

Upvotes: 1

Views: 4557

Answers (3)

Develoger
Develoger

Reputation: 3998

Solved it, needed some javascript :)

Live example:
http://simplestudio.rs/yard/framed/framed.html (offline)

CODE:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="shortcut icon" href="res/favicon.ico" />
<title>My website</title>

<style type="text/css">

#framed {

    position: relative;
    width: 100%;
    height: 100%;   

}

.wrap {
    position:relative;
    margin:0 auto;
    width:900px;
    text-align:center;
}

#header, #footer {
    width:100%;
    float:left;
}

#footer {
    position:fixed;
    bottom:0;
    z-index:999999;
}
</style>

    <script>

        var resize = setInterval(function(){chng_iframe_height('framed','header','footer')},500);

        function chng_iframe_height(ifrid,hid,fid) {

            var eheight = window.innerHeight;
            var ifrobj = document.getElementById(ifrid); 
            var header = document.getElementById(hid); 
            var footer = document.getElementById(fid); 
            var header_height = getComputedStyle(header).height;
            var footer_height = getComputedStyle(footer).height;

            var reserved_height = parseInt(header_height) + parseInt(footer_height);

            ifrobj.style.height = eheight - reserved_height +"px";     

        }
    </script>

</head>
<body onload="chng_iframe_height('framed','header','footer');">
  <div id="header">
      <div class="wrap">
           <div class="logo">
                <h1>Header</h1>
           </div>
           <div>Menu goes here</div>
      </div>
 </div>

 <iframe src="http://www.cnn.com" id="framed"></iframe>

  <div id="footer">
      <div class="wrap">
           <h2>Footer</h2>
      </div>
 </div>
</body>
</html>

Basicaly, I compute header and Footer rendered height, get window.innerHeight and from that numbers I know how much px is there left between header in footer so I assign that value to iframe and whoila it works...

Also I have set setinterval to call that function every half second so if you resize window it will almost immediately update iframe height...

Upvotes: 7

Vikram Shetty
Vikram Shetty

Reputation: 757

I made used of .css() and $(window).height() When you use $(window).height() make sure you have doctype as html

<script>
      var resize = setInterval(function(){chng_iframe_height('framed','header','footer')},500);

        function chng_iframe_height(ifrid,hid,fid) {

            var eheight = $(window).height();
            var ifrobj = document.getElementById(ifrid); 
            var header_height = $("#" + hid).css("height"); // getComputedStyle(header).height;
            var footer_height = $("#" + fid).css("height"); 

            var reserved_height = parseInt(header_height) + parseInt(footer_height);

            ifrobj.style.height = eheight - reserved_height +"px";     

        }
    </script>

Upvotes: 0

prototype
prototype

Reputation: 3313

Try adding a class to your iframe or style it on the spot like this:

<iframe style="height:100%; width:100%;" src="http://www.cnn.com"></iframe>

Try this also:

<iframe height="100%" width="100%" src="http://www.cnn.com"></iframe>

Also don't forget to remove the padding/margin on the body tag so the header and footer go all the way:

body {
    margin:0;
    padding: 0;
}

EDIT:

If this does not work you'll have to add fixed width and height.

Upvotes: 1

Related Questions