How to stick a footer to bottom in css? [duplicate]
Asked Answered
L

16

78

I am having the classic problem for the positioning of a Footer on the bottom of the browser. I've tried methods including http://ryanfait.com/resources/footer-stick-to-bottom-of-page/ but to no good result: my footer always keeps appearing in the middle of the browser window in both FF and IE.

In the HTML i got this simple structure

<form>
 ...
 <div class=Main />
 <div id=Footer />
</form>

Here is the css code that is relevant for the css footer problem:

    *
    {
        margin: 0;
    }


html, body
{
    height: 100%;
}


    #Footer
    {
        background-color: #004669;
        font-family: Tahoma, Arial;
        font-size: 0.7em;
        color: White;
        position: relative;
        height: 4em;
    }



    .Main
    {
        position:relative;
        min-height:100%;
        height:auto !important;
        height:100%;

        /*top: 50px;*/

        margin: 0 25% -4em 25%;

        font-family: Verdana, Arial, Tahoma, Times New Roman;
        font-size: 0.8em;
        word-spacing: 1px;
        line-height: 170%;
        /*padding-bottom: 40px;*/
    }

Where am I doing wrong? I really have tried everything. If I missed any useful info please let me know.

Thank you for any suggestion in advance.

Regards,


thank you all for your answers. it worked with:

position:absolute;
    width:100%;
    bottom:0px;

setting position:fixed did not work in IE for some reason(Still showed footer in the middle of the browser), only worked for FF.

Lilongwe answered 28/9, 2009 at 18:23 Comment(2)
stackoverflow.com/search?q=css+footerSqueegee
in 2019 it is very easy to do with flexboxColdhearted
D
64

Try setting the styles of your footer to position:absolute; and bottom:0;.

Deflower answered 28/9, 2009 at 18:28 Comment(5)
because what you're doing is not the classic sticky footer that sticks to the bottom of the page and if the page is 100% visible to the bottom of the window.Roper
thats correct, even if the page is longer than the fold, the footer will start to cover some of the content and remain visible at all times with this methodDeflower
Here's a sort of expanded version of this same solution: https://mcmap.net/q/266199/-my-footer-floatsLengthen
but on tablets or 10 inch laptops it even overlapes the above the fold content.Reahard
I would add that if the footer is nested into a top-level component, then this top-level component should have the property "position: relative" in the corresponding css file, like that: .top{position: relative; .footer{position: absolute; bottom: 0px;}}Starchy
M
54
#Footer {
  position:fixed;
  bottom:0;
}

That will make the footer stay at the bottom of the browser window no matter where you scroll.

Metamorphosis answered 28/9, 2009 at 18:27 Comment(2)
And precisely what I was looking for ;)Eclecticism
you might wanna add a width: 100%; to that to make it full widthDezhnev
D
16
#Footer {
position:fixed;
bottom:0;
width:100%;
}

worked for me

Decisive answered 11/11, 2011 at 23:58 Comment(0)
D
8

Assuming you know the size of your footer, you can do this:

    footer {
        position: sticky;
        height: 100px;
        top: calc( 100vh - 100px );
    }
Damnify answered 18/7, 2020 at 19:23 Comment(3)
Replacing 'fixed' Worked for me, footer { position: fixed; height: 100px; top: calc( 100vh - 100px ); }Bearskin
Using css native variable eg. --footer-height: 100px; make code easy to use :)Makings
+1 for using sticky instead of fixed. Sticky leaves the footer positioned relative to its container, whereas fixed places the footer relative to the viewport.Brundage
L
7

I think a lot of folks are looking for a footer on the bottom that scrolls instead of being fixed, called a sticky footer. Fixed footers will cover body content when the height is too short. You have to set the html, body, and page container to a height of 100%, set your footer to absolute position bottom. Your page content container needs a relative position for this to work. Your footer has a negative margin equal to height of footer minus bottom margin of page content.

Leathaleather answered 24/2, 2014 at 3:3 Comment(1)
This doesn't answer the question.Pastorship
P
5
#footer{
position: fixed; 
bottom: 0;
}

http://codepen.io/smarty_tech/pen/grzMZr

Puncheon answered 16/4, 2016 at 5:54 Comment(0)
F
2

If you use the "position:fixed; bottom:0;" your footer will always show at the bottom and will hide your content if the page is longer than the browser window.

Fouquiertinville answered 4/4, 2012 at 15:50 Comment(1)
That does work nicely and is much less troublesome than other solutions (especially if you have a variable-height footer), but the obvious downside is that it uses JS.Skyjack
S
2

For modern browser, you can use flex layout to ensure the footer stays at the bottom no matter the content length (and the bottom won't hide the content if it is too long)

HTML Layout:

<div class="layout-wrapper">
  <header>My header</header>
  <section class="page-content">My Main page content</section>
  <footer>My footer</footer>
</div>

CSS:

html, body {
  min-height: 100vh;
  width: 100%;
  padding: 0;
  margin: 0;
}

.layout-wrapper {
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}

.layout-wrapper > .page-content {
  background: cornflowerblue;
  color: white;
  padding: 20px;
}

.layout-wrapper > header, .layout-wrapper > footer {
  background: #C0C0C0;
  padding: 20px 0;
}

Demo: https://codepen.io/datvm/pen/vPWXOQ

Strictly answered 12/3, 2019 at 16:55 Comment(1)
you wrote the correct solution and you could update it as here explainedColdhearted
B
2

So a Mixed Solution from @nvdo and @Abdelhameed Mahmoud worked for me

footer {
    position: sticky;
    height: 100px;
    top: calc( 100vh - 100px );
}
Bearskin answered 15/12, 2020 at 22:34 Comment(0)
N
2

Try position attribute with fixed value to put your division in a fixed position. Following is the code for putting your footer at bottom of the page.

footer {
    position: fixed; 
    bottom: 0;
}
Newsletter answered 26/9, 2021 at 17:34 Comment(1)
position: fixed could overlap the main content where position: sticky would push the contentAnaemic
A
1

This worked for me:

.footer
{
  width: 100%;
  bottom: 0;
  clear: both;
}
Anjaanjali answered 22/4, 2014 at 6:8 Comment(0)
S
1

The following css property will do it

position: fixed;

I hope this help.

Sieber answered 8/10, 2014 at 17:42 Comment(0)
C
0

Try adding margin-top: auto; to the footer. This approach addresses any overflow that may be caused by other methods.

html, body { height: 100%}
body {
display: flex;
flex-direction: column;
}


header {
min-height: 80px;
outline: 2px solid red;
display: flex;
justify-content: center;
align-items: center;
}

main {
min-height: 280px;
outline: 2px solid green;
display: flex;
justify-content: center;
align-items: center;
}

footer {
min-height: 80px;
outline: 2px solid blue;
display: flex;
justify-content: center;
align-items: center;
}
<header>Header</header>
<main>main</main>
<footer style="margin-top: auto;">footer</footer>
Ceria answered 5/10, 2020 at 8:55 Comment(1)
I think this should be a comment as it seems to be no stand-alone-answer.Enchanting
K
0

The following code works, which is from w3schools.com

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.footer {
   position: fixed;
   left: 0;
   bottom: 0;
   width: 100%;
   background-color: red;
   color: white;
   text-align: center;
}
</style>
</head>
<body>

<h2>Fixed/Sticky Footer Example</h2>
<p>The footer is placed at the bottom of the page.</p>

<div class="footer">
  <p>Footer</p>
</div>

</body>
</html> 
Kinematics answered 21/8, 2021 at 14:27 Comment(0)
B
0

A sticky footer without covering your content.

If you position your footer using "bottom:0px", it will always stay at the bottom of the viewport even if the viewport is too small to display all the content, which means it will cover part of your content.

If you want your footer stuck to the bottom, but you always want it to remain below the content of your container, then you do this:

.body{height:100%}  // Or whatever container you want
.footer{position:sticky;top:100vh}

The 100vh will keep the footer at the bottom when there is enough space to see it, but because it's not an absolute position it will not push up above the bottom of the container's content.

Brundage answered 11/4, 2023 at 18:34 Comment(0)
D
-1

I had a similar issue with this sticky footer tutorial. If memory serves, you need to put your form tags within your <div class=Main /> section since the form tag itself causes issues with the lineup.

Dustin answered 28/9, 2009 at 18:27 Comment(3)
could you pease elaborate your solution could be correct but i did not understand it completely: put the form tags where exactly?Lilongwe
:: Looks at answer:: Look at that, it filtered out the tag. Updated the answer. Does that make sense?Dustin
Andrei is talking about sticky footer, what you're implementing is not a sticky footer as such.Roper

© 2022 - 2024 — McMap. All rights reserved.