Make Iframe to fit 100% of container's remaining height

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>

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:

  • Use JavaScript
  • Don't use DOCTYPE. This is not a good practice, but it puts the browsers in "quirks mode", in which you can do height="100%" on elements and it will stretch them to the window size. Do note, that the rest of your page will probably have to be changed too to accommodate for the DOCTYPE changes.

    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.

    body, html {width: 100%; height: 100%; margin: 0; padding: 0}
    .first-row {position: absolute;top: 0; left: 0; right: 0; height: 100px; background-color: lime;}
    .second-row {position: absolute; top: 100px; left: 0; right: 0; bottom: 0; background-color: red }
    .second-row iframe {display: block; width: 100%; height: 100%; border: none;}
    <div class="first-row">
      <p>Some text</p>
      <p>And some more text</p>
    </div>
    <div class="second-row">
      <iframe src="https://jsfiddle.net/about"></iframe>
    </div>

    We use a JavaScript to solve this problem; here is the source.


    var buffer = 20; //scroll bar buffer
    var iframe = document.getElementById('ifm');
    
    function pageY(elem) {
        return elem.offsetParent ? (elem.offsetTop + pageY(elem.offsetParent)) : elem.offsetTop;
    }
    
    function resizeIframe() {
        var height = document.documentElement.clientHeight;
        height -= pageY(document.getElementById('ifm'))+ buffer ;
        height = (height < 0) ? 0 : height;
        document.getElementById('ifm').style.height = height + 'px';
    }
    
    // .onload doesn't work with IE8 and older.
    if (iframe.attachEvent) {
        iframe.attachEvent("onload", resizeIframe);
    } else {
        iframe.onload=resizeIframe;
    }
    
    window.onresize = resizeIframe;
    

    Note: ifm is the iframe ID

    pageY() was created by John Resig (the author of jQuery)


    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 node width: 100%; and height: 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 simple width: 100%; height: 100%; width: 100%; height: 100%; CSS instruction.

    Example code


        body {
            margin: 0px;
            padding: 0px;
        }
    
        /* iframe's parent node */
        div#root {
            position: fixed;
            width: 100%;
            height: 100%;
        }
    
        /* iframe itself */
        div#root > iframe {
            display: block;
            width: 100%;
            height: 100%;
            border: none;
        }
       <html>
            <head>
                <title>iframe Test</title>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            </head>
            <body>
                <div id="root">
                    <iframe src="http://stackoverflow.com/">
                        Your browser does not support inline frames.
                    </iframe>
                </div>
            </body>
        </html>
    链接地址: http://www.djcxy.com/p/15500.html

    上一篇: 如何让div填充剩余的水平空间?

    下一篇: 使Iframe适合容器剩余高度的100%