Question

i want that my footer is position absolute on the bottom of the page even if the content is less than window height. I've tried all possible tutorials and everything what could think of but couldn't do it.

My HTML syntax is:

<html>

<head></head>

<body>

<header>
     <div class="wrap"></div>
</header>

<div class="content wrap">
    <div class="left"></div>
    <div class="right"></div>
</div>

<footer>
    <div class="inner"></div>
</footer>

</body>

</html>

The site is full width i use .wrap and .inner class for width: 1000px and margin 0 auto;

If anyone could give a solution please?

Was it helpful?

Solution

here is a fiddle doing what you asked using jQuery: FIDDLE

basically you just compare body and window heights, if body is longer set footer to absolute, if shorter set it to fixed:

$(function () {
    //change this to 'display:none'
    $('.right').css({'display':'static'});
    var footer = $('footer');
    var theDocument = $('body');
    var theWindow = $(window);
    if (theDocument.height() < theWindow.height()) {
        footer.css({
            'position': 'fixed'
        });
    } else {
        theWindow.height();
        footer.css({
            'position': 'absolute'
        });

    }
});

UPDATE:

here is a version that fixes the footer being over the content, you just need to move the footer down by the size of its height

FIDDLE

//######  code inside $(function () {}) will run as soon as DOM loads
$(function () {
    //change this to 'display:static' to add more content
    $('.right').css({'display':'none'});
    //sets a custom event handler to window resize + zoom, which triggers the 
    //footer fix function
    $(window).resize(function(){
        adjust_footer();
    });
    //also call this function as soon as document finishes loading
    adjust_footer();
});
//#####


function adjust_footer(){
    var footer = $('footer');
    var theDocument = $('body');
    var theWindow = $(window);
    //the  +footer.height() checks if there is enough space for footer
    //to stick it as fixed without having it cover content
    if (theDocument.height() +footer.height() < theWindow.height()) {
        footer.css({
            'position': 'fixed',
            //important, or footer will remain misplaced
            'bottom':0
        });
    } else {
        theWindow.height();
        footer.css({
            'position': 'absolute',
            //push footer down and align it to the end of content
            //meaning if footer's height is 50px, it will be pushed 50px
            //from the bottom of the content
            //* remember, bottom attribute aligns the element by its bottom
            'bottom':- footer.height()
        });
    }
}

OTHER TIPS

If I understand your question correctly you actually want to position the footer as a fixed element and set bottom to 0px. This will keep it at the bottom of the page no matter what.

position:fixed;
bottom:0px;

You can use "fixed" property from CSS and add "bottom:0px;" so its gonna be at the bottom forever ;)

<header>
     <div class="wrap">Header stuff</div>
</header>

<div class="content wrap">
    <div class="left">Left Stuff</div>
    <div class="right">Right stuff</div>
</div>

<footer>
    <div class="inner">Footer stuff</div>
</footer>

And CSS:

header
{
    width:100%;
    height:50px;
    background:green;
}

.content
{
    width:100%;
    background:blue;
}

.content .left
{
    width:30%;
    float:left;
    background:blue;
}

.content .right
{
    width:70%;
    float:right;
    background:yellow;
}

.inner
{
    padding:10px;
}

footer
{
    position:fixed;
    bottom:0px;
    height:40px;
    width:100%;
    background:red;
}

Outcome is here: http://jsfiddle.net/67zR6/1/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top