Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iframe 100% height inside body with padding

Tags:

html

css

iframe

I have an iframe in my HTML document and I'm having a bit of trouble.

I also have a URL bar (fixed position element) at the top of the page that should stay with the user as they scroll. That works fine. I'd like the iframe to fill the remaining space but not be covered up by the URL bar.

This is what I'm talking about. http://s75582.gridserver.com/Ls

How can I fix this so that the URL bar doesn't cover up part of the page? When I try setting padding in the body, it just creates an extra, annoying scroll bar.

like image 903
Giles Van Gruisen Avatar asked Aug 20 '09 03:08

Giles Van Gruisen


People also ask

How do I make my iframe height 100%?

given the iframe is directly under body. If the iframe has a parent between itself and the body, the iframe will still get the height of its parent. One must explicitly set the height of every parent to 100% as well (if that's what one wants).

How do I adjust the iframe height automatically?

Answer: Use the contentWindow Property You can use the JavaScript contentWindow property to make an iFrame automatically adjust its height according to the contents inside it, so that no vertical scrollbar will appear.

Does padding count in height?

The width and height properties include the content, padding, and border, but do not include the margin.


1 Answers

Whilst you can't say ‘height: 100% minus some pixels’ in CSS, you can make the iframe 100% high, then push its top down using padding. Then you can take advantage of the CSS3 box-sizing property to make the padding get subtracted from the height.

This:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html><head>
    <title>test</title>
    <style type="text/css">
        html, body { margin: 0; padding: 0; height: 100%; }
        #bar { height: 32px; background: red; }
        iframe {
            position: absolute;
            top: 0; left: 0; width: 100%; height: 100%;
            border: none; padding-top: 32px;
            box-sizing: border-box; -moz-box-sizing: border-box; -webkit-box-sizing: border-box;
        }
    </style>
</head><body>
    <iframe src="http://www.google.com/"></iframe>
    <div id="bar">foo</div>
<body></html>

Works on IE8, Moz, Op, Saf, Chrome. You'd have to carry on using a JavaScript fallback to make the extra scrollbar disappear for browsers that don't support box-sizing though (in particular IE up to 7).

like image 178
bobince Avatar answered Nov 15 '22 20:11

bobince