Forum Moderators: open

Message Too Old, No Replies

Iframe - how to get an automatic height

         

SpitfireXD

3:02 am on Jul 12, 2007 (gmt 0)

10+ Year Member



I am working on a new website and I am trying to use a iframe like this
<iframe
src ="http://www.example.com/forums/index.php"
width="100%" height="1500">
</iframe>

what I WANT to do is make it so the height automatically sizes and I know % height doesnt work so I didnt even bother wasting my time on that one so what I really want is something that makes the iframe AUTO adjust to the height of what I put in it.

[edited by: tedster at 6:10 am (utc) on July 12, 2007]
[edit reason] use example.com - not real domains [/edit]

Marshall

3:51 am on Jul 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You could try em's, but I don't know if that works. The other option is a resize script with a widow size detector. And I can't say I tried this, but what if you set the W & H using CSS instead of in the iframe line. You might be able to get away with %.

Marshall

vincevincevince

3:58 am on Jul 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to use Javascript and both pages need to be on the same domain.

Then (as I remember) you can call, from the page loaded into the IFRAME:

<body onLoad="parent.window.document.getElementById('theiframe').height=this.offsetHeight">

SpitfireXD

12:46 am on Jul 13, 2007 (gmt 0)

10+ Year Member



Example marshall?

kinda like

{
.forumheightwidth
}
height:100%;
width:100%;
{

and im not to sure on the iframe code?

vincevincevince

12:59 am on Jul 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



SpitfireXD - did you try that javascript I posted above?

Marshall

1:57 am on Jul 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



SptfireXD,

I tried this and it will adjust the width, but not the height even if you set the height to %, em or auto:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
#size {
width: 50%; /* Set to fit your needs */
height: 200px; /* Set to a minimum height as per your needs */
}
</style>
</head>
<body>

<iframe id="size" src="http://www.yourdomain.com/Search/index.asp"></iframe>
</body>
</html>

Marshall

Trace

3:23 pm on Jul 13, 2007 (gmt 0)

10+ Year Member



100% height for an iframe works perfectly fine. It will take 100% of the height available from it's container. Here's what's missing from Marshall's example;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
html, body{
height:100%;
}

#size {
width: 50%; /* Set to fit your needs */
height: 100%; /* Set to a minimum height as per your needs */
}
</style>
</head>
<body>
<iframe id="size" src="http://www.google.com"></iframe>
</body>
</html>

[edited by: Trace at 3:24 pm (utc) on July 13, 2007]

vincevincevince

3:26 pm on Jul 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



makes the iframe AUTO adjust to the height of what I put in it.

I think that's what's wanted, not to make the IFRAME fit the container.

Trace

4:21 pm on Jul 13, 2007 (gmt 0)

10+ Year Member



My bad - it got a little confusing half way through the thread.

kaled

10:21 am on Jul 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The content of the iframe must be enclosed in div. After setting the width of the iframe, you can measure the height of the div and set the height of the iframe accordingly (thus eliminating the need for a scrollbar).

As a concept this should work, however, making it work in reality is likely to be tricky. However, with judicious use of events such as onload it is probably possible.

Server-side includes are probably the way to go.

Kaled.