How to stick
element at the bottom of the page (HTML5 and CSS3)?

后端 未结 4 1290
离开以前
离开以前 2020-12-09 04:16

When I use position relative with no content, footer goes up, with absolute with a lot of content, the footer goes down, and with f

4条回答
  •  悲哀的现实
    2020-12-09 04:25

    Here is an example using css3:

    CSS:

    html, body {
        height: 100%;
        margin: 0;
    }
    #wrap {
        padding: 10px;
        min-height: -webkit-calc(100% - 100px);     /* Chrome */
        min-height: -moz-calc(100% - 100px);     /* Firefox */
        min-height: calc(100% - 100px);     /* native */
    }
    .footer {
        position: relative;
        clear:both;
    }
    

    HTML:

    body content....
    footer content....

    jsfiddle

    Update
    As @Martin pointed, the ´position: relative´ is not mandatory on the .footer element, the same for clear:both. These properties are only there as an example. So, the minimum css necessary to stick the footer on the bottom should be:

    html, body {
        height: 100%;
        margin: 0;
    }
    #wrap {
        min-height: -webkit-calc(100% - 100px);     /* Chrome */
        min-height: -moz-calc(100% - 100px);     /* Firefox */
        min-height: calc(100% - 100px);     /* native */
    }
    

    Also, there is an excellent article at css-tricks showing different ways to do this: https://css-tricks.com/couple-takes-sticky-footer/

提交回复
热议问题