1

So far with help I have this:

When content is big then all shows up as needed - sexy. But when content is for example 1 line footer offcourse sticks to the bottom of the content.

I need some magic, so that footer sticks to the end of content, but when content is smaller than page height -500 px (fixed for header and footer) than footer must remain on bottom of the page. How to do such magic? See attached pictures:

CSS:

* {
padding: 0;
margin: 0;
}

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

body{
}

.header{
width: 100%;
height: 300px;
padding-top: 30px;
background-color: gold;
}

#content{
width: 100%;

-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;

background-color: yellow;
}

#footer{
width: 100%;
height: 100px;
position: relative;
z-index: 1;
background-color: orange;
}

here is fiddle on this: http://jsfiddle.net/Cheese1991/VcK83/

enter image description here

Example:

From screenshot number 1 we see that our header (300px with margin 30px), dynamic size content ( never is known ) but for this example lets say 200px and footer (100px) fit on the &(window).height() that in the example is 775px.

So we have 775-330-200-100=145px of free size ( the white blank space )

I want to achieve 1 out of to possibilities:

1) The content will take the free space to its size, so that html, body, etc will still remain 775px and there was no overflow (scroller on the right size) because components fit in, we don't need more space than we have.

2) The footer will position itself on the bottom (not position fixed on the page, because then content will hide behind it)

Here are 2 of the possibilities in pictures:

1) enter image description here

2) enter image description here

Developer
  • 4,158
  • 5
  • 34
  • 66
  • possible duplicate of [How do you get the footer to stay at the bottom of a Web page?](http://stackoverflow.com/questions/42294/how-do-you-get-the-footer-to-stay-at-the-bottom-of-a-web-page) – Paulie_D Apr 02 '14 at 10:38
  • Not a duplicate because in that example there remains overflow if content is blank – Developer Apr 02 '14 at 10:42

1 Answers1

1

Use min-height to #content. See this-jsfiddle

EDIT

* {
padding: 0;
margin: 0;
}

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

body{
}

.header{
width: 100%;
height: 300px;
padding-top: 30px;
background-color: gold;
}

#content{
width: 100%;

-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;

background-color: yellow;
}

#footer{
width: 100%;
height: 100px;
    position: absolute;
    bottom: 0px;
    width: 100%;

background-color: orange;
}

And use this Jquery code

$(document).ready(function(){
    var $footer = $('#footer');

    var contentHeight = $('#content').outerHeight();
    var headerHeight = $('.header').outerHeight();
    var footerHeight = $footer.outerHeight();
    var bodyHeight = $('body').outerHeight();   
    var headerPContentPFooter = contentHeight+headerHeight+footerHeight;



    if(headerPContentPFooter > bodyHeight){
        $footer.css('position','relative');
    }

    $(window).resize(function(){
    var contentHeight = $('#content').outerHeight();
    var headerHeight = $('.header').outerHeight();
    var footerHeight = $('#footer').outerHeight();
    var bodyHeight = $('body').outerHeight();

    var headerPContentPFooter = contentHeight+headerHeight+footerHeight;

        if(headerPContentPFooter > bodyHeight){
            $footer.css('position','relative');
        }else{
            $footer.css('position','absolute');
        }   

    });
});
Fazal Rasel
  • 4,446
  • 2
  • 20
  • 31
  • This might help you [link](https://developer.mozilla.org/en-US/docs/Web/CSS/min-height) – Fazal Rasel Apr 02 '14 at 10:49
  • nop, this just adds pixels to content so that it looked like footer is on bot – Developer Apr 02 '14 at 11:00
  • You said as i understant ***footer sticks to the end of content, but when content is smaller than page height -500 px (fixed for header and footer) than footer must remain on bottom of the page.** So on jsfiddle, delete few 'span' from '#content', then footer goes bottom of the page. Isn't that you want? If not make it clear to me. – Fazal Rasel Apr 02 '14 at 11:06
  • Look at my question, at the screenshot. If i add min-height footer goes lower than windows size. but if my header, content, and footer fit the windows size why should i stretch content and use not needed space – Developer Apr 02 '14 at 11:10
  • Its really confusing what really you want. Could you please few screenshot that makes the question clear.... – Fazal Rasel Apr 02 '14 at 11:27
  • see my example in question – Developer Apr 02 '14 at 11:42