新标题:确保页脚始终位于页面底部
P粉318928159
P粉318928159 2023-08-27 23:13:19
[HTML讨论组]

我正在进行一个项目,我想让页脚保持在页面底部,但在滚动到页面底部之前不可见。我尝试在我的CSS中使用'position: fixed',但它浮在我的内容上方,对于绝对定位,它固定在页面中间并覆盖了内容。此外,对于内容不多的页面,当我不指定位置或使用'position: absolute'时,页脚下方会有空白。请提供建议。

* {
  margin: 0;
  padding: 0;
}

header {
  background-color: gray;
}

footer {
  background-color: gray;
  bottom: 0;
  height: 20px;
  position: fixed;
  width: 100%;
}

/* 当我使用fixed定位时,页脚固定在内容上方。我想要的是页脚保持在页面底部但不可见。 */
<html>
 <body>
  <header>标题</header>
  <main>
    <h1>标题</h1>
    <p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Adipisci eos deserunt fugiat doloremque     your text`ut.</p>
  </main>
  <footer>&copy; 版权所有, 商业</footer>
</body>
</html>
P粉318928159
P粉318928159

全部回复(1)
P粉554842091

我认为你可以给它添加一个父级div,父级div的宽度和高度与它相同。

html:

.footer {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100vw;
  height: 200px;
  background-color: red;
}
.footer-container {
  height: 200px;
  width: 100vw;
}
<div class="footer-container">
  <div class="footer"></div>
</div>
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

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