Search code examples
csslayouthtmlliquid-layout

CSS liquid layout where the footer div follows the size of the page and stays at the bottom


I'm working on the following layout structure:

<div id="wrapper">
    <div id="header"></div>
    <div id="pageContainer"></div>
    <div id="footer"></div>
</div>

With the following CSS I set the footer to the bottom of the page:

#wrapper {
   min-height:100%;
   position:relative;
}
#header {
   background:#ff0;
   padding:10px;
}
#pageContainer {
   padding:10px;
   padding-bottom:60px;
}
#footer {
   position:absolute;
   bottom:0;
   width:100%;
   height:60px;
   background:#6cf;
}

issue schema

If the content in the 'pageContainer div' is small, I don't want to show the scroll bars in the div but attach the footer to the bottom of the 'pageContainer div' (right not the footer is always at the bottom of the viewport)

If the content of the 'pageContainer div' is long I need the footer to remain visible in the viewport (at the bottom) and show the scroll bars in the 'pageContainer div'.

How do I do this? Any ideas? thanks!

PS: I need a solution that doesn't use JS.


Solution

  • If I read you correctly, you're describing behavior where positioning switches from relative to fixed, depending on the size of an element relative to the real-estate available in the viewport.

    Quite certainly, you cannot achieve this without JavaScript.

    But a solution where the footer is always at the bottom of the viewport is fairly common and easy to do without JavaScript. In case you do not already know how to do that:

    #header, #pageContainer, #footer{
        position:fixed;
        left:0px;
        right:0px;
    }
    #header{
        top:0px;
        height:100px;
        background:#ff0;
    }
    #pageContainer {
        top:100px;
        bottom:60px;
        overflow:auto;
    }
    #footer {
        bottom:0px;
        width:100%;
        height:60px;
        background:#6cf;
    }