how to make DIV height 100% between header and footer
Asked Answered
C

3

19

Is there a way to setup a layout so that the header is 50px, body is 100% and footer is 50px?

I would like the body to use up the entire viewing area at minimum. I would like to have the footer and header to be on the screen at all times

Circuitous answered 19/4, 2012 at 12:39 Comment(2)
https://mcmap.net/q/414758/-css-100-height-layoutGetty
What behaviour would you want if the content in the body overflowed the screen height?Unless
R
18

I created an example in jsfiddle:

UPDATED JsFiddle: http://jsfiddle.net/5V288/1025/

HTML:

<body>
    <div id="header"></div>
    <div id="content"><div>
        Content 
    </div></div>
    <div id="footer"></div>
</body>

CSS:

html { height: 100%; }
body {
    height:100%;
    min-height: 100%;
    background: #000000;
    color: #FFFFFF;
    position:relative;
}
#header {
    height:50px;
    width:100%;
    top:0px;
    left:0px;
    background: #CCCCCC;
    position:fixed;
}
#footer {
    height:50px;
    width:100%;
    bottom:0px;
    left:0px;
    background: #CCCCCC;
    position:fixed;
}
#content {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    height:100%;
    padding: 0 20px;
}
#content > div {
    padding: 70px 0;
}

Without border-box the content will be height 100% + 140px padding. With the border-box the content height will be 100% and the padding will be inside.

Ravish answered 19/4, 2012 at 12:56 Comment(4)
Would be nice to include some code here in case JSFiddle isn't available. BTW, the box-sizing: border-box; doesn't seem to make any difference.Remorseless
Without border-box the content will be height 100% + 140px padding. With the border-box the content height will be 100% and the padding will be inside.Ravish
That doesn't work when scrolling, the footer scrolls with the content.Refractometer
Not a very good example this, it just scrolls past the footer bar!Musso
C
4

Just a fix for Andreas Winter solution:

http://jsfiddle.net/5V288/7/

*With the solution of it, you would have problems if the content is greater than the available window area.

Clubhaul answered 19/4, 2012 at 13:17 Comment(2)
Thanks for this - I was struggling with fixed header and footer with scrollable 100% center area. This is perfect, can now have EPIC layouts!!! ^__^Taoism
yayyyyyyy, im glad to help you :3Clubhaul
R
4

I think what you're looking for is "multiple absolute coordinates". A List Apart has an explanation here but basically, you just need to specify the body's position as absolute, and set both top: 50px and bottom: 50px:

<body>
<style>
#header {
  position: absolute;
  height: 50px;
} 

#body {     
  position: absolute;
  top: 50px;
  bottom: 50px;
  background-color: yellow;
}

#footer {
  position:absolute;
  height: 50px;
   bottom: 0;
}
</style>
<div id="header">Header</div>
<div id="body">Content goes here</div>
<div id="footer">Footer</div>

http://www.spookandpuff.com/examples/absoluteCoordinates.html shows the technique in a prettier way.

Remorseless answered 16/10, 2012 at 5:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.