首页 > web前端 > css教程 > 正文

使用CSS实现Footer置底的五种方式

高洛峰
发布: 2017-03-16 17:43:10
原创
2492人浏览过

页脚置底(Sticky footer)就是让网页的footer部分始终在浏览器窗口的底部。

当网页内容足够长以至超出浏览器可视高度时,页脚会随着内容被推到网页底部;
但如果网页内容不够长,置底的页脚就会保持在浏览器窗口底部。

使用CSS实现Footer置底的五种方式

方法一:将内容部分的margin-bottom设为负数

<p class="wrapper">
    <!-- content -->
    <p class="push"></p>
</p>
<p class="footer">footer</p>
登录后复制
html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}
.wrapper {
  min-height: 100%;  
  margin-bottom: -50px; /* 等于footer的高度 */
}
.footer, .push {
  height: 50px;
}
登录后复制
  1. 这个方法需要容器里有额外的占位元素(p.push)。

  2. p.wrapper的margin-bottom需要和p.footer的-height值一样,注意是负height。

方法二:将页脚的margin-top设为负数

  • 给内容外增加父元素,并让内容部分的padding-bottom与页脚的height相等。

<p class="content">
  <p class="content-inside">
    <!-- content -->
  </p>
</p>
<p class="footer">footer</p>
登录后复制
html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}
.content {
  min-height: 100%;
}
.content-inside {
  padding: 20px;
  padding-bottom: 50px;
}
.footer {
  height: 50px;
  margin-top: -50px;
}
登录后复制

方法三:使用calc()设置内容高度

<p class="content">
  <!-- content -->
</p>
<p class="footer">footer</p>
登录后复制
登录后复制
登录后复制
.content {
  min-height: calc(100vh - 70px);
}
.footer {
  height: 50px;
}
登录后复制
  • 这里假设p.content和p.footer之间有20px的间距,所以70px=50px+20px

方法四:使用flexbox弹性盒布局

以上三种方法的footer高度都是固定的,如果footer的内容太多则可能会破坏布局。

立即学习前端免费学习笔记(深入)”;

<p class="content">
  <!-- content -->
</p>
<p class="footer">footer</p>
登录后复制
登录后复制
登录后复制
html {
  height: 100%;
}
body {
  min-height: 100%;
  display: flex;
  flex-direction: column;
}
.content {
  flex: 1;
}
登录后复制

方法五:使用Grid网格布局

<p class="content">
  <!-- content -->
</p>
<p class="footer">footer</p>
登录后复制
登录后复制
登录后复制
html {
  height: 100%;
}
body {
  min-height: 100%;
  display: grid;
  grid-template-rows: 1fr auto;
}
.footer {
  grid-row-start: 2;
  grid-row-end: 3;
}
登录后复制

                                               

以上就是使用CSS实现Footer置底的五种方式的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
相关标签:
css
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号