如何将页脚(footer)保持在页面底部

1. 问题描述

footer保持在视觉窗口的底部是常见的需求。当页面的内容主体高度较大(超出屏幕可视区域的高度)时,footer元素直接跟在内容后面即可。但是当一个 HTML 页面包含少量的内容时,footer元素就会坐落在页面的中间,在下面留下空白,这看起来很糟糕,尤其是在大屏幕上。
如下图:
问题描述

2. 解决方案

我们先给出基础的页面代码:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>footer</title>
    <style>
        * {
            box-sizing: border-box;
            text-align: center;
            margin: 0;
            padding: 0;
        }
        header, footer {
            color: #FFF;
            padding: 20px;
        }
        header {
            background-color: #7BE0AD;
        }
        main {
            padding: 20px;
            border: 2px dashed red;
        }
        footer {
            background-color: #457B9D;
        }
    </style>
</head>

<body>
    <div id="container">
        <header>
            <h1>我是header</h1>
        </header>
        <main>
            <p>我是内容(内容不是很长)</p>
        </main>
        <footer>
            <p>我是footer &copy;码飞_CC</p>
        </footer>
    </div>
</body>

</html>

显示效果如下:
基础页面的显示

2.1 使用position属性实现(兼容较老的浏览器)

代码如下:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>footer</title>
    <style>
        * {
            box-sizing: border-box;
            text-align: center;
            margin: 0;
            padding: 0;
        }
        html, body {
            height: 100%;  /* 1. 需要将页面的高度设置成浏览器可视区域的高度 */
        }
        #container {
            min-height: 100%; /* 2. 需要将容器的高度设置成100% */
            position: relative; /* 3. 容器的position设置为relative,给子元素定位提供基点 */
        }
        header, footer {
            color: #FFF;
            padding: 20px;
        }
        header {
            background-color: #7BE0AD;
        }
        main {
            padding: 20px;
            border: 2px dashed red;
        }
        footer {
            position: absolute;
            bottom: 0; /* 4. 设置页脚position为absolute之后,再将其bottom值设为0,即可使其处于页面的底部 */
            width: 100%;
            background-color: #457B9D;
        }
    </style>
</head>

<body>
    <div id="container">
        <header>
            <h1>我是header</h1>
        </header>
        <main>
            <p>我是内容(内容不是很长)</p>
        </main>
        <footer>
            <p>我是footer &copy;码飞_CC</p>
        </footer>
    </div>
</body>

</html>

显示的效果如下:
position实现的方式
几个注意点

  1. HTMLbody标记必须设置为 height: 100% ;,这保证我们后面能够设置容器 div的百分比高度。
  2. 容器 div具有一个 min-height: 100% ;,这将确保它在几乎没有任何内容的情况下,也可以保持屏幕的完整高度。容器 div也被设置为 position: relative;,这使我们能够绝对定位其子元素。
  3. 页脚容器设置为绝对定位,并且bottom:0;。宽度设置为100%,width: 100%;这样页脚才能撑开为可视区域的宽度。
  4. 这里有个小问题,就是当窗口缩得特别小的时候,footer元素会在容器div上,使得内容被覆盖了。解决方法是设置容器divpadding-bottomborder-bottom的值为footer元素的高度(这里使用margin-bottom是没有效果的)。

2.2 使用Flex实现

CSS代码:

* {
    box-sizing: border-box;
    text-align: center;
    margin: 0;
    padding: 0;
}
html, body {
    height: 100%;
}
#container {
    min-height: 100%;
    display: flex; /* 容器为flex布局 */
    flex-direction: column; /* 方向为纵向,保持正常的从上到下文档流 */
}
header, footer {
    color: #FFF;
    padding: 20px;
}
header {
    background-color: #7BE0AD;
}
main {
    padding: 20px;
    border: 2px dashed red;
}
footer {
    margin-top: auto; /* 设置footer的上外边距为auto */
    background-color: #457B9D;
}

效果跟使用position的方法一样,但是更加简洁完美,没有position小节中几个注意点部分的第4条的那个问题。

2.3 使用Grid实现

* {
    box-sizing: border-box;
    text-align: center;
    margin: 0;
    padding: 0;
}
html, body {
    height: 100%;
}
#container {
    min-height: 100%;
    display: grid; /* 容器为grid布局 */ 
    grid-template-rows: auto 1fr auto;
}
header, footer {
    color: #FFF;
    padding: 20px;
}
header {
    background-color: #7BE0AD;
}
main {
    padding: 20px;
    border: 2px dashed red;
}
footer {
    background-color: #457B9D;
}

效果如下:
grid
使用grid实现需要注意兼容性问题,从效果图可以看到,它的实现方式是用特殊的网格单元 fr,使main元素撑满剩余空间来实现的。

参考内容

在 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
发出的红包

打赏作者

码飞_CC

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值