推荐阅读 https://blog.csdn.net/weixin_46221198/article/details/107564706
1. 利用 calc() 计算
整体思路:
将页面分为内容和页脚两个盒子,利用ccs3新增 calc() 计算函数,计算出内容盒子大小,这样两个盒子就不会重叠了。
<style>.content {min-height: calc(100vh - 50px);}.footer {height: 50px;}</style><body><div class="content">content</div><footer class="footer"></footer></body>
2. flex布局
以上三种方法的footer高度都是固定的,通常来说这不利于网页布局:内容会改变,它们都是弹性的,一旦内容超出固定高度就会破坏布局。所以给footer使用flexbox吧,让它的高度可以变大变小变漂亮~(≧∇≦)
<style>html {height: 100%;}body {min-height: 100%;display: flex;flex-direction: column;}.content {flex: 1;}</style><body><div class="content">content</div><footer class="footer"></footer></body>
grid网格布局
grid比flexbox还要新很多,并且更佳很简洁。
遗憾的是,网格布局(Grid layout)目前仅支持Chrome Canary和Firefox Developer Edition版本。
<style>html {height: 100%;}body {min-height: 100%;display: grid;grid-template-rows: 1fr auto;}.footer {grid-row-start: 2;grid-row-end: 3;}</style><body><div class="content">content</div><footer class="footer"></footer></body>
