个人练习来源于《CSS揭秘》这本书第7章-41紧贴底部的页脚这部份内容以及书中提到的连接。css
方案一html
描述:如下方案简单、干净、现代而且没有hack,适用于IE8+, Chrome, Firefox, Opera等浏览器;不须要使用任何标签包裹,由于它是基于可变的body高度;这个解决方案诞生于2012年初,今天也许应该使用更好的方案好比flexbox。git
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>css紧贴底部的页脚</title> </head> <style type="text/css"> /*footer紧贴底部的主要代码*/ html{ position: relative; min-height: 100%; } body{ margin: 0 0 100px; } footer{ position: absolute; left: 0; bottom: 0; height: 50px; width: 100%; background-color: green;/*设置颜色方便查看footer位置*/ } /*调整内容的高度以查看footer紧贴底部的效果*/ .content{ height: 100px; } </style> <body> <div class="content"></div> <footer></footer> </body> </html>
方案二github
描述:使用display的flex属性浏览器
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>css紧贴底部的页脚</title> </head> <style type="text/css"> /*footer紧贴底部的主要代码*/ body{ margin: 0; display: flex; min-height: 100vh; flex-direction: column; } footer{ height: 50px; width: 100%; background-color: green;/*设置颜色方便查看footer位置*/ } .content{ flex: 1; } </style> <body> <div class="content"></div> <footer></footer> </body> </html>
方案三flex
描述:使用 calc()方法flexbox
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>css紧贴底部的页脚</title> </head> <style type="text/css"> /*footer紧贴底部的主要代码*/ body{ margin: 0; } footer{ height: 50px; width: 100%; background-color: green;/*设置颜色方便查看footer位置*/ } .content{ min-height: calc(100vh - 50px); } </style> <body> <div class="content"> <div style="height:1000px;">这个div用来撑高度的</div> </div> <footer></footer> </body> </html>
(未完待续……)spa
参考:pwa