I want to design a web page with a banner and an iframe. I hope the iframe can fill all the remaining page height and be resized automatically as the browser is resizing. Is it possible to get it done without writing Javascript code, only with CSS?
I tried set height:100%
on iframe, the result is quite close but the iframe tried to fill the whole page height, including the 30px
height of banner div element, so I got unneccessary vertical scrollbar. It's not perfect.
Update Notes: Excuse me for not describing the question well, I tried CSS margin, padding attribute on DIV to occupy the whole remining height of a web page successfully, but the trick didn't work on iframe.
<body>
<div style="width:100%; height:30px; background-color:#cccccc;">Banner</div>
<iframe src="http: //www.google.com.tw" style="width:100%; height:100%;"></iframe>
</body>
Any idea is appreciated.
You can do this by measuring the body size on load/resize events and setting the height to the (full height - banner height).
Note that currently in IE8 Beta2 you can't do this onresize as that event is currently broken in IE8 Beta2.
The trick is to understand what the 100% is taken of. Reading CSS specs can help you there.
To make a long story short - there is such a thing as "containing block" - which is not necessary the parent element. Simply said, it is the first element up the hierarchy that has position:relative or position:absolute. Or the body element itself if there is nothing else. So, when you say "width: 100%", it checks the width of the "containing block" and sets the width of your element to the same size. If there was something else there, then you might get contents of a "containing block" that are larger than itself (thus "overflowing").
Height works the same way.
With one exception. You can't get height to 100% of the browser window. The very top level element, against which 100% can be calculated, is the body (or html? not sure) element, and that stretches just enough to contain its contents. Specifying height:100% on it will have no effect, because it has no "parent element" against which to measure 100%. Window itself doesn't count. ;)To make something stretch exactly 100% of the window, you have two choices:
Update: I'm not sure if I wasn't wrong already when I posted this, but this certainly is outdated now. Today you can do this in your stylesheet:
html, body { height: 100% }
and it will actually stretch to the whole of your viewport. Even with a DOCTYPE.min-height: 100%
could also be useful, depending on your situation.And I wouldn't advise anyone to make a quirks-mode document anymore either, because it causes way more headaches than solves them. Every browser has a different quirks-mode, so getting your page to look consistently across browsers becomes two orders of magnitude more difficult. Use a DOCTYPE. Always. Preferably the HTML5 one -
<!DOCTYPE html>
. It's easy to remember and works like a charm in all browsers, even the 10 years old ones.The only exception is when you have to support something like IE5 or something. If you're there, then you're on your own anyway. Those ancient browsers are nothing like the browsers today, and little advice that is given here will help you with them. On the bright side, if you're there, you probably just have to support ONE kind of browser, which gets rid of the compatibility problems.
Good luck!
Update 2: Hey, it's been a long time! 6 years later, new options are on the scene. I just had a discussion in the comments below, here are more tricks for you that work in today's browsers.
Option 1 - absolute positioning. Nice and clean for when you know the precise height of the first part.
Some notes - the
second-row
container is needed becausebottom: 0
andright: 0
doesn't work on iframes for some reason. Something to do with in being a "replaced" element. Butwidth: 100%
andheight: 100%
works just fine.display: block
is needed because it's aninline
element by default and whitespace starts creating weird overflows otherwise.Option 2 - tables. Works when you don't know the height of the first part. You can use either actual
<table>
tags or do it the fancy way withdisplay: table
. I'll go for the latter because it seems to be in fashion these days.Some notes - the
overflow: auto
makes sure that the row always includes all of its contents. Otherwise floating elements can sometimes overflow. Theheight: 100%
on the second row makes sure it expands as much as it can squeezing the first row as small as it gets.Option 3 - flexbox. The cleanest one of them all, but with a less than stellar browser support. IE10 will need
-ms-
prefixes for the flexbox properties, and anything less will not support it at all.Some notes - the
overflow: hidden
is because the iframe still generates some sort of overflow even withdisplay: block
in this case. It isn't visible in the fullscreen view or the snippet editor, but the small preview window gets an extra scrollbar. No idea what that is, iframes are weird.I used display:table to fix a similar issue. It almost works for this, leaving a small vertical scroll bar. If you're trying to populate that flexible column with something other than an iframe it works fine (not
Take the following HTML
Change the outer div to use display:table and ensure it has a width and height set.
Make the banner a table-row and set its height to whatever your preference is:
Add an extra div around your iframe (or whatever content you need) and make it a table-row with height set to 100% (setting its height is critical if you want to embed an iframe to fill the height)
Below is a jsfiddle showing it at work (without an iframe because that doesn't seem to work in the fiddle)
https://jsfiddle.net/yufceynk/1/
It's right, you are showing an iframe with 100% height respect to its container: the body.
Try this:
Of course, change the height of the second div to the height you want.
you cant set the iframe height in % because your parent body height is not 100% so make the parent height as 100% and then apply iframe height 100%
You can do it with
DOCTYPE
, but you have to usetable
. Check this out: