footer固定在页面底部的几种方法

http://blog.youkuaiyun.com/m0_37070714/article/details/77587753
在 HTML 页面中,将页脚固定页面底部可以使用多种方法实现。下面介绍其中的几种方法。 ### 方法一:使用 CSS 使用 CSS 的 `position: fixed` 属性可以将元素固定页面的某个位置。将页脚元素使用 `position: fixed` 属性,设置 `bottom: 0`,即可将页脚固定页面底部。 ``` <style> footer { position: fixed; bottom: 0; width: 100%; background-color: #333; color: #fff; text-align: center; padding: 10px; } </style> <body> <main> <!--页面内容--> </main> <footer> <!--页脚内容--> </footer> </body> ``` ### 方法二:使用 Flexbox 使用 Flexbox 布局可以方便地将元素固定页面底部。将页面内容和页脚包裹在一个容器中,使用 `display: flex` 和 `flex-direction: column` 属性将容器变成纵向排列,然后将容器的高度设置为 `100vh`,将页脚元素的 `margin-top` 设置为 `auto`,即可将页脚固定页面底部。 ``` <style> html, body { height: 100%; } .container { display: flex; flex-direction: column; min-height: 100vh; } main { flex: 1; } footer { margin-top: auto; width: 100%; background-color: #333; color: #fff; text-align: center; padding: 10px; } </style> <body> <div class="container"> <main> <!--页面内容--> </main> <footer> <!--页脚内容--> </footer> </div> </body> ``` ### 方法三:使用 Grid 使用 Grid 布局也可以将元素固定页面底部。将页面内容和页脚包裹在一个容器中,使用 `display: grid` 和 `grid-template-rows: 1fr auto` 属性将容器分成两行,第一行占据剩余的空间,第二行为自适应高度,即可将页脚固定页面底部。 ``` <style> html, body { height: 100%; } .container { display: grid; grid-template-rows: 1fr auto; min-height: 100vh; } main { /*不需要设置高度*/ } footer { width: 100%; background-color: #333; color: #fff; text-align: center; padding: 10px; } </style> <body> <div class="container"> <main> <!--页面内容--> </main> <footer> <!--页脚内容--> </footer> </div> </body> ``` 以上是三种将页脚固定页面底部方法,可以根据实际情况选择使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值