Fluid height main body with header and footer

2019-03-06 09:46发布

问题:

What I am trying to do is have the header at the top of the page and the footer at the bottom, with the map full up all availiable space in-between. I also want the header and footer to have fixed heights as px not %.

However, at the moment because I set the main map to 100% it forces the footer off the bottom of the page and introduces a scroll bar.

This is my example code: http://jsfiddle.net/W4mXP/20/

CSS

html, body { height: 100%;
    width: 100%;
}

#topbar {
    height: 50px; 
    width: 100%; 
    background-color: black;
    padding-left: 50px;
    padding-right: 50px;
}

#main {
    height: 100%; 
    width: 100%;
    background-color: green;
}
#bottombar {
    height: 25px; 
    width: 100%; 
    background-color: black;
    padding-left: 50px;
    padding-right: 50px;
}


.left {float: left;}
.right {float: right;}

HTML

<html>
    <head>
         <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
    </head>
    <body>
    <!-- Header -->
    <div id="topbar">
        <div class="left">
        Logo
        </div>
        <div class="right">
<form>
  <div id="radio">
    <input type="radio" id="radio1" name="radio" /><label for="radio1">Choice 1</label>
    <input type="radio" id="radio2" name="radio" checked="checked" /><label for="radio2">Choice 2</label>
    <input type="radio" id="radio3" name="radio" /><label for="radio3">Choice 3</label>
  </div>
</form>
        </div>
    </div>
    <!-- Map -->
    <div id="main">
    Map
    </div>
    <!-- Footer -->
    <div id="bottombar">
        <div class="left">
        Name
        </div>
        <div class="right">
        About
        </div>
    </div>
    </body>
</html>

回答1:

My answer here may help: Set div block to 100% height

Simply set the height of the footer and a negative margin on the content:

#footer { height:100px; }
#container { margin-bottom:-100px; padding-bottom:100px; }

Here is an example of how you'd include a footer: JSFiddle.

Do note that this will not work in IE7 due to its usage of box-sizing.

Edit: You'll also need to add padding to the container as well to prevent container text appearing under the footer. I've fixed the JSFiddle example to include this.