Code前端首页关于Code前端联系我们

如何实现页脚到底部? ? ?

terry 2年前 (2023-09-08) 阅读数 238 #Vue

将.content的margin-bottom设置为负数

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Sticky Footer</title>
    <style>
      html,
      body {
        margin: 0;
        padding: 0;
        height: 100%;
        text-align: center;
      }

      .content {
        min-height: 100%;
        margin-bottom: -50px; /* 等于 .footer 高度的相反数 */
      }

      .content-inside {
        background-color: #c2ffa9;
      }

      .push {
        height: 50px; /* .push 是额外的占位元素,高度与 .footer 一致 */
      }

      .footer {
        height: 50px;
        line-height: 50px; /* 实现文字垂直居中 */
        background-color: #3ba4f9;
      }
    </style>
  </head>
  <body>
    <div class="content">
      <!-- div.content-inside 的高度可以随意设置,亦可根据内容高度自适应 -->
      <div class="content-inside">
        <!-- content -->
        <div>Content</div>
        <div>Content</div>
        <div>Content</div>
      </div>
      <!-- div.push 是额外的占位元素,高度与 div.footer 一致 -->
      <div class="push"></div>
    </div>
    <div class="footer">Sticky Footer</div>
  </body>
</html>
 

将.footer的margin-top设置为负数

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Sticky Footer</title>
    <style>
      html,
      body {
        margin: 0;
        padding: 0;
        height: 100%;
        text-align: center;
      }

      .content {
        min-height: 100%;
      }

      .content-inside {
        background-color: #c2ffa9;
      }

      .footer {
        height: 50px;
        line-height: 50px; /* 实现文字垂直居中 */
        background-color: #3ba4f9;
        margin-top: -50px; /* 等于 .footer 高度的相反数 */
      }
    </style>
  </head>
  <body>
    <div class="content">
      <!-- div.content-inside 的高度可以随意设置,亦可根据内容高度自适应 -->
      <div class="content-inside">
        <!-- content -->
        <div>Content</div>
        <div>Content</div>
        <div>Content</div>
      </div>
    </div>
    <div class="footer">Sticky Footer</div>
  </body>
</html>
 

使用 calc() 设置 .content 的高度

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Sticky Footer</title>
    <style>
      html,
      body {
        margin: 0;
        padding: 0;
        height: 100%;
        text-align: center;
      }

      .content {
        /* 如果 .content 与 .footer 之间有间距,记得减去 */
        min-height: calc(100vh - 50px);
      }

      .content-inside {
        background-color: #c2ffa9;
      }

      .footer {
        height: 50px;
        line-height: 50px; /* 实现文字垂直居中 */
        background-color: #3ba4f9;
      }
    </style>
  </head>
  <body>
    <div class="content">
      <div class="content-inside">
        <!-- content -->
        <div>Content</div>
        <div>Content</div>
        <div>Content</div>
      </div>
    </div>
    <div class="footer">Sticky Footer</div>
  </body>
</html>
 

使用Flexbox灵活布局

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Sticky Footer</title>
    <style>
      html,
      body {
        margin: 0;
        padding: 0;
        height: 100%;
        text-align: center;
      }

      body {
        min-height: 100%;
        display: flex;
        flex-direction: column;
      }

      .content {
        flex: 1;
      }

      .content-inside {
        background-color: #c2ffa9;
      }

      .footer {
        /* 这种方案不需要固定 .footer 的高度 */
        background-color: #3ba4f9;
      }
    </style>
  </head>
  <body>
    <div class="content">
      <div class="content-inside">
        <!-- content -->
        <div>Content</div>
        <div>Content</div>
        <div>Content</div>
      </div>
    </div>
    <div class="footer">Sticky Footer</div>
  </body>
</html>

 

使用网格布局

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Sticky Footer</title>
    <style>
      html,
      body {
        margin: 0;
        padding: 0;
        height: 100%;
        text-align: center;
      }

      body {
        min-height: 100%;
        display: grid;
        grid-template-rows: 1fr auto;
      }

      .content-inside {
        background-color: #c2ffa9;
      }

      .footer {
        grid-row-start: 2;
        grid-row-end: 3;
        background-color: #3ba4f9;
      }
    </style>
  </head>
  <body>
    <div class="content">
      <div class="content-inside">
        <!-- content -->
        <div>Content</div>
        <div>Content</div>
        <div>Content</div>
      </div>
    </div>
    <div class="footer">Sticky Footer</div>
  </body>
</html>
 

版权声明

本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门