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.
Here's what I did. I had the same problem and ended up searching the web for resources for hours.
I added this to the head section.
Please note that my iframe is located inside the middle cell of a table that has 3 rows and 1 column.
I think the best way to achieve this scenario using css position. set position relative to your parent div and position:absolute to your iframe.
for other padding and margin issue now a days css3 calc() is very advanced and mostly compatible to all browser as well.
check calc()
Similar answer of @MichAdel, but I'm using JQuery and more elegant.
iframe_id
is the ID of the iframe tagWhile I agree JS seems a better option, I have a somewhat CSS only working solution. The downside of it is that if you have to add content to your iframe html document frequently, you would have to adapt one percentage trough time.
Solution:
Try not specifying any height for BOTH your html documents,
then only code this:
note: the div should be your main section.
This should relatively work. the iframe does exactly calculates 300% of the visible window height. If you html content from the 2nd document (in the iframe) is smaller in height than 3 times your browser height, it works. If you don't need to add content frequently to that document this is a permanent solution and you could just find your own % needed according to your content height.
This works because it prevents the 2nd html document (the one embed) to inherit its height frome the parent html document. It prevents it because we didn't specify an height for both of them. When we give a % to the child it looks for its parent, if not, it takes its content height. And only if the other containers aren't given heights, from what I tried.
Having tried the css route for a while, I ended up writing something fairly basic in jQuery that did the job for me:
Tested in IE8, Chrome, Firefox 3.6
New in HTML5: Use calc (on height)