css - css页面底部固定的几种方法

在日常开发中,在移动端经常会有一些按钮需要固定在页面的底部(比如:提交按钮,发布按钮,确定按钮,下一步…等等);所以这边总结了3种比较常用好使的固底方法;

效果图如下:
在这里插入图片描述

1,使用相对定位和绝对定位

注意:当页面高度高于可视窗口的时候(也就是出现了滚动条);这时如果再使用绝对定位 向上滑动的时候,就已经不固底了,因为页面进来定位只计算一次;

解决方案:使用fixed固定定位,永远把footer固定到底部,有滚动条也是不影响的;

<template>
  <div class="container">
    <header style="background: #f9cc9d">Header</header>
    <main style="background: #c3d08b">Content</main>
    <footer style="background: #2b93f5">Footer</footer>
  </div>
</template>

<script>
export default {
};
</script>
<style scoped lang="scss">
// 第一种方式
.container {
// 使用子绝父相
  position: relative;
  height: 100vh;
  background: #f1f3f4;
  header, main,footer {
    height: 80px;
    line-height: 80px;
    width: 100%;
  }
  // 设置绝对定位
  footer{
    height: 60px;
    line-height: 60px;
    position: absolute;
    bottom: 0;
    left: 0;
  }
}
</style>

2,使用flex弹性布局,将footer的margin-top设置为auto

注意:在浏览器中,100vh的高度包括了底部的工具栏,而我们真实需要的高度其实是浏览器的可视高度也就是100%;
在使用100vh的时候,手机浏览器底部的操作栏可能会遮挡底部按钮;
所以这时页面的高度应该使用100%;

<template>
  <div class="container">
    <header style="background: #f9cc9d">Header</header>
    <main style="background: #c3d08b">Content</main>
    <footer style="background: #2b93f5">Footer</footer>
  </div>
</template>
<script>
export default {};
</script>
<style scoped lang="scss">
// 第二种方式
.container {
  display: flex;
  flex-direction: column; // 设置成上下排列方式
  height: 100vh;
  background: #f1f3f4;
  header,  main,  footer {
    height: 80px;
    line-height: 80px;
    width: 100%;
  }
  // 使用
  footer {
    margin-top: auto;
    height: 60px;
    line-height: 60px;
  }
}
</style>

3,通过css内置函数calc()动态计算内容的高度

min-height: calc(100vh - 130px); 130 其实就是 header和footer的高度;

<template>
  <div class="container">
    <header style="background: #f9cc9d">Header</header>
    <main style="background: #c3d08b">Content</main>
    <footer style="background: #2b93f5">Footer</footer>
  </div>
</template>

<script>
export default {};
</script>
<style scoped lang="scss">
// 第三种方式
.container {
  height: 100vh;
  background: #f1f3f4;
  header {
    height: 80px;
    line-height: 80px;
    width: 100%;
  }
  footer{
    height: 50;
    line-height: 50px;
  }
  // 内容区 130 其实就是 header和footer的高度
  main{
    min-height: calc(100vh - 130px);
  }
}
</style>

### 实现按钮固定页面底部方法 要实现按钮固定页面底部的功能,可以采用 `position: fixed` 的 CSS 属性来完成。这种方法能够确保按钮始终位于视口的底部位置,并且不会随着页面滚动而移动。 以下是具体的代码示例: ```css <style> .fixed-button { position: fixed; /* 设置固定定位 */ bottom: 0; /* 距离底部为0 */ left: 0; /* 左边距设置为0 */ width: 100%; /* 宽度覆盖整个屏幕宽度 */ text-align: center; /* 文本居中显示 */ background-color: #f8f9fa; /* 按钮背景颜色 */ padding: 10px; /* 内部填充 */ box-shadow: 0 -2px 5px rgba(0, 0, 0, 0.1); /* 添加阴影效果提升视觉层次感 */ } </style> <div class="fixed-button"> <button>点击我</button> </div> ``` 上述代码通过定义 `.fixed-button` 类实现了按钮固定页面底部的效果[^1]。其中的关键属性解释如下: - **`position: fixed;`**:该属性使得元素相对于浏览器窗口进行定位,而不是文档流中的其他元素。 - **`bottom: 0;` 和 `left: 0;`**:这两个属性分别控制按钮距离页面底部和左侧的距离,从而将其放置到左下角的位置。 - **`width: 100%;`**:设定按钮容器占据整个页面宽度,以便适配不同设备尺寸。 如果希望按钮不仅停留在可视区域底部,还能够在内容较多的情况下紧贴实际内容下方,则可参考另一种布局方式。这种方式涉及调整 HTML 结构并配合特定样式的运用,具体做法见下面的例子: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Fixed Button Example</title> <style> body, html { margin: 0; padding: 0; height: 100%; width: 100%; overflow: auto; } .content { min-height: calc(100% - 400px); padding-bottom: 400px; /* 预留空间给footer */ } footer { width: 100%; height: 400px; margin-top: -400px; /* 抵消预留的高度 */ background: lightgray; display: flex; justify-content: center; align-items: center; } </style> </head> <body> <div class="content">这里是主要内容...</div> <footer> <button>这是底部按钮</button> </footer> </body> </html> ``` 此方案利用了负外边距与最小高度相结合的技术手段,保证当页面内容较少时,按钮仍能处于真正的页面末端;而在内容超过一屏高之后,它会像平常一样随主体部分一同向上滑动[^2]。 综上所述,对于大多数场景而言,单纯依靠 `position: fixed` 即足以满足需求。然而针对某些特殊情形下的精确控制,则可能需要用到更复杂的结构设计以及相应的样式规则。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值