让web页面页脚footer固定在页面底部

野性酷女 2022-03-29 12:08 417阅读 0赞

有时候,我们发现很多页面内容不多的时候,页面底部内容飘到了中间:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ZlaW5pZmk_size_16_color_FFFFFF_t_70

这个页面底部没有固定,结果…,一个前端工程师是无法接受这样的结果的。

这里介绍一种通用的解决办法,让页面底部固定。

思路: 首先需要一个容器包裹所有的页面元素,这里使用

来做。id为container的容器需要设置高度100%,否则,当内容较少的时候,还是会飘起来。最后,主要的是footer底部元素需要绝对定位,让他固定在底部。

下面直接上代码:

content.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>layout</title>
  6. <link rel="stylesheet" type="text/css" href="css/base.css">
  7. <link rel="stylesheet" type="text/css" href="css/public.css">
  8. </head>
  9. <body>
  10. <div id="container">
  11. <header class="clearfix">
  12. <div class="user-info fr">
  13. <a class="user-name" href="" title="用户名">admin</a>
  14. <a class="logout" href="">退出</a>
  15. </div>
  16. <div class="logo fl">xxx管理系统</div>
  17. </header>
  18. <main class="content">
  19. <aside class="grid-left">菜单</aside>
  20. <section class="grid-right">
  21. <p>内容变多了</p>
  22. <p>内容变多了</p>
  23. <p>内容变多了</p>
  24. <p>内容变多了</p>
  25. <p>内容变多了</p>
  26. <p>内容变多了</p>
  27. <p>内容变多了</p>
  28. <div style="height:300px;background:lightgreen;"></div>
  29. <p>内容变多了</p>
  30. <p>内容变多了</p>
  31. <p>内容变多了</p>
  32. <p>内容变多了</p>
  33. <p>内容变多了</p>
  34. <p>内容变多了</p>
  35. <p>内容变多了</p>
  36. <p>内容变多了</p>
  37. <p>内容变多了</p>
  38. <p>内容变多了</p>
  39. <p>内容变多了</p>
  40. <p>内容变多了</p>
  41. <p>内容变多了</p>
  42. <p>内容变多了</p>
  43. </section>
  44. </main>
  45. <footer class="footer">
  46. <div class="sub-foot">
  47. <ul>
  48. <li><a>法律条款</a></li>
  49. <li><a>隐私保护</a></li>
  50. <li><a>联系我们</a></li>
  51. <li><a>常见问题</a></li>
  52. </ul>
  53. <p>版权所有@1998-2018xx集团</p>
  54. </div>
  55. </footer>
  56. </div>
  57. </body>
  58. <script type="text/javascript">
  59. window.onload = function(){
  60. var height = document.body.offsetHeight;
  61. var ele = document.querySelector(".content");
  62. ele.style.height = (height-140)+"px";
  63. }
  64. </script>
  65. </html>

base.css

  1. *{margin:0;padding:0;}
  2. html,body,div,input,h1,h2,h3,h4,h5,h6,p{font-size:100%;font-weight: normal;font-family: Helvetica;}
  3. .clearfix{*zoom:1;}
  4. .clearfix:after{
  5. display:table;
  6. content:'';
  7. clear: both;
  8. line-height:0;
  9. }
  10. .fr{float:right;}
  11. .fl{float:left;}
  12. a{color:#0088cc;text-decoration: none;}

public.css

  1. html,body{width:100%;height:100%;}
  2. #container{min-height: 100%;position: relative;height:auto !important;}
  3. header{height:59px;border-bottom: 1px solid #ddd;background: #f7f7f7;}
  4. header .logo{cursor: pointer;height: 59px;line-height: 59px;margin-left: 20px;}
  5. .user-info{margin-right: 20px;}
  6. .user-info a{text-decoration: none;color:#000;}
  7. .user-info .user-name{height:59px;line-height: 50px;}
  8. .content{display:flex;}
  9. .grid-left{flex:none;float:left;border-right:1px solid #ddd;width:160px;}
  10. .grid-right{flex:1;float:right;overflow-x:hidden;overflow-y: auto;}
  11. footer{
  12. position: absolute;
  13. bottom:0px;
  14. height:79px;
  15. border-top:1px solid #ddd;
  16. width:100%;
  17. background: #f7f7f7;
  18. }
  19. .sub-foot{width:1000px;margin:0 auto;text-align: center;}
  20. .sub-foot ul{}
  21. .sub-foot li{display:inline-block;height:30px;line-height:30px;}
  22. .sub-foot p{height:30px;line-height: 30px;}

最终的结果:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ZlaW5pZmk_size_16_color_FFFFFF_t_70 1

这里的关键地方:html,body均需要设置高度为100%,另外,id=container 的元素需要设置min-height:100%,这样页面就会被撑起来。而不是随着内容高度自适应。当页面内容过多之后,让多出容器的部分滚动,这样,页脚相当于就固定在了底部。

发表评论

表情:
评论列表 (有 0 条评论,417人围观)

还没有评论,来说两句吧...

相关阅读

    相关 HTML始终固定底部

    页脚固定在底部分两种情况考虑,一种是有滑动栏的情况,一种是没有的。后者比较简单,直接加上定位属性就是了,但是在页面内容是扩充整个页面以至于可以下滑浏览时,这时候单纯加上定位属