The more violent solution is to add a minimum height to the main part
section { min-height:1080px; }
section { min-height:100vh; }
section { min-height: calc(100vh - 160px); }
Or use JS to judge the window height so that the footer is always at the bottom
$(document).ready(function(){ if($(document.body).height()<$(window).height()){ $("footer").css({"position":"fixed","bottom":"0px"}); } })
Is there any more elegant way?
<! doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled document</title> <style> * { margin: 0; padding: 0 } body, html { height: 100%; } .container { position: relative; padding-bottom: 150px; /*Greater than or equal to the height of footer*/ width: 100%; min-height: 100%; box-sizing: border-box; } header { width: 100%; height: 100px; } section { width: 100%; height: 300px; } footer { position: absolute; width: 100%; height: 150px; left: 0; bottom: 0; } </style> </head> <body> <div class="container"> <header>header</header> <section>content</section> <footer>footer</footer> </div> </body> </html>
But now, I may prefer another way - flex layout
<! doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled document</title> <style> * { margin: 0; padding: 0 } html { height: 100%; } body { display: flex; flex-direction: column; height: 100%; } header { flex: 0 0 auto; } section { flex: 1 0 auto; } footer { flex: 0 0 auto; } </style> </head> <body> <header>header</header> <section>content</section> <footer>footer</footer> </body> </html>