HTML+CSS底部footer两种固定方式

网页常见的底部栏(footer)目前有两种:

一、永久固定,不管页面的内容有多高,footer一直位于浏览器最底部,适合做移动端底部菜单,这个比较好实现;(向立凯)

二、相对固定,当页面内容高度不沾满浏览器高度,footer显示在浏览器底部,且不会出现滚动条,如果页面内容高度超出浏览器高度,footer则相对与内容的最底部,并且自动出现滚动条;(向立凯)


废话不多说,可以直接复制代码查看效果

一、永久固定(向立凯)

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title> </title>
  6. <meta charset="utf-8" />
  7. <style>
  8. body {
  9. padding-bottom: 50px;
  10. }
  11. .footer {
  12. position: fixed;
  13. left: 0px;
  14. bottom: 0px;
  15. width: 100%;
  16. height: 50px;
  17. background-color: #eee;
  18. z-index: 9999;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. 内容,可以大量复制看效果 <br />
  24. <div class="footer">固定在底部 </div>
  25. </body>
  26. </html>


二、相对固定(向立凯)

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title> </title>
  6. <meta charset="utf-8" />
  7. <style type="text/css">
  8. * {
  9. margin: 0px;
  10. padding: 0px;
  11. }
  12. html, body {
  13. height: 100%;
  14. }
  15. .footer {
  16. margin-top: - 50px;
  17. height: 50px;
  18. background-color: #eee;
  19. z-index: 9999;
  20. }
  21. .wrap {
  22. min-height: 100%;
  23. }
  24. .main {
  25. padding-bottom: 50px;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <div class="wrap">
  31. <div class="main">
  32. 内容,可以大量复制看效果 <br />
  33. </div>
  34. </div>
  35. <div class="footer">相对在底部 </div>
  36. </body>
  37. </html>

猜你喜欢

转载自blog.csdn.net/dongsdh/article/details/80953565