Footer won't stay in the bottom of the page

荒凉一梦 提交于 2019-12-04 16:47:28

You could use absolute position on the footer with some tricks as follows.

http://jsfiddle.net/a5q2u4bv/1/

html {
    position: relative;
    min-height: 100%;
}
body {
    margin: 0 0 150px;
}
footer {
    background: silver;
    position: absolute;
    left: 0;
    bottom: 0;
    width: 100%;
    height: 150px;
}
<body>
    <main>main</main>
    <footer>footer</footer>
</body>

1: Why do you have it floated? float: left can be easily removed if it doesn't have any purpose. Floating could also cause problems with positioning, and be the reason that it doesn't work.

2: The bottom property only works when the element is something other than position: static. If it's not specified, it's position: static by default. position: absolute is probably what you want.

3: Does your site have a vertical scrollbar yet (so much content so that you could scroll it)? If you add more content to the site so that there becomes a scrollbar on the site and your footer code is the farthest down in the HTML, it will probably be at the bottom without specifying anything more. Demo: http://fiddle.jshell.net/j1hhc98v/2/

Never mind, I fixed it my self, I will leave the code here if anyone has the same problem.

html part:

<body>

        <div id="header">

        </div>

        <div id="container">

        <div id="content">

        </div>

        </div>

        <div id="footer">

        </div>


</body>

css part:

body {
    padding:0;
    height:100%;
}

body > #container {
    height: auto; 
    min-height: 100%;
}

html {
    height:100%;
}   

#footer{
    width: 100%;
    height: 200px;
    background-color: #000000;
    z-index: 10;
    margin-top: 1000px;
    position: relative;
    clear: both;
}

#content{
    padding-bottom: 200px;
}

#container{
    height:100%;
}

#header {
    width: 100%;
    height: 200px;
    background-color: #000000;
    float: left;
    position: relative;
}

Check out this sticky footer site. This is probably what you are looking for.

This is the first one I saw and still use. http://ryanfait.com/sticky-footer/

However CSS tricks has a good one too. Very similar but uses the pseudo :after instead of a div. https://css-tricks.com/snippets/css/sticky-footer/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!