移动端自适应布局:头部不固定,底部不固定,中间高度自适应

45 阅读1分钟
<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
  <title>移动端自适应布局</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    html,
    body {
      height: 100%;
    }

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

    .header {
      background-color: #f0f0f0;
      padding: 15px;
      /* 头部不固定,自然高度 */
    }

    .content {
      flex: 1;
      padding: 15px;
      overflow-y: auto;
      -webkit-overflow-scrolling: touch;
    }

    .footer {
      background-color: #333;
      color: white;
      padding: 15px;
      /* 底部自适应高度 */
    }
  </style>
</head>

<body>
  <header class="header">
    <h1>头部内容</h1>
  </header>

  <main class="content">
    <!-- 主要内容区域 -->
    <p>这里是页面主体内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...</p>
    <p>更多内容...aaaa</p>
    <!-- 内容会自动撑开 -->
  </main>

  <footer class="footer">
    <p>底部自适应内容</p>
  </footer>
</body>

</html>