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.
Another way to do that would be to use the
position: fixed;
on parent node.If I am not mistaken,
position: fixed;
ties the element to viewport, thus, once you give this nodewidth: 100%;
andheight: 100%;
properties, it will span over entire screen. From this point on, you can put<iframe>
tag inside it and span it over remaining space (both in width and in height) with simplewidth: 100%; height: 100%;
CSS instruction.Example code
Maybe this has been answered already (a few answers above are "correct" ways of doing this), but I thought I'd just add my solution as well.
Our iFrame is loaded within a div, hence I needed something else then window.height. And seeing our project already relies heavily on jQuery, I find this to be the most elegant solution:
Where of course "#middle" is the id of the div. The only extra thing you'll need to do is recall this size change whenever the user resizes the window.
Here are a few modern approaches:
Approach 1 - Combination of viewport relative units /
calc()
.The expression
calc(100vh - 30px)
represents the remaining height. Where100vh
is the height of the browser and the usage ofcalc()
effectively displaces the height of the other element.Example Here
Support for
calc()
here; support for viewport relative units here.Approach 2 - Flexbox approach
Example Here
Set the
display
of the common parent element toflex
, along withflex-direction: column
(assuming you want the elements to stack on top of each other). Then setflex-grow: 1
on the childiframe
element in order for it to fill the remaining space.Since this approach has less support1, I'd suggest going with the aforementioned approach.
1Though it seems to work in Chrome/FF, it doesn't work in IE (the first method works in all current browsers).
The "seamless" attribute is a new standard aiming to solve this issue:
http://www.w3schools.com/tags/att_iframe_seamless.asp
When assigning this attribute it will remove borders and scroll bars and size the iframe to its content size. though it is only supported in Chrome and latest Safari
more on this here: HTML5 iFrame Seamless Attribute
You can do this with html/css like this:
or you can go old-school and use a frameset perhaps: