教程 html tutorial

响应式首页

以下示例展示了一个基础响应式首页布局,包含导航栏、横幅和内容区域,能自动适配手机和平板等不同屏幕尺寸。

HTML 代码示例

html
<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>响应式首页</title>
    <style>
      * {
        box-sizing: border-box;
      }
      body {
        font-family: "Microsoft YaHei", sans-serif;
        margin: 0;
        padding: 0;
        color: #333;
      }
      header {
        background-color: #3498db;
        color: white;
        padding: 15px 20px;
        text-align: center;
      }
      nav {
        display: flex;
        justify-content: center;
        background-color: #2980b9;
      }
      nav a {
        color: white;
        padding: 14px 20px;
        text-decoration: none;
        text-align: center;
        flex: 1;
        border-right: 1px solid #1c5980;
      }
      nav a:last-child {
        border-right: none;
      }
      nav a:hover {
        background-color: #1c5980;
      }
      .banner {
        background: url("https://via.placeholder.com/1200x300") no-repeat
          center/cover;
        height: 300px;
        display: flex;
        align-items: center;
        justify-content: center;
        color: white;
        font-size: 2em;
        text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.7);
      }
      main {
        max-width: 1000px;
        margin: 20px auto;
        padding: 0 15px;
      }
      .content {
        display: flex;
        gap: 20px;
        flex-wrap: wrap;
      }
      .card {
        background: #f0f0f0;
        padding: 15px;
        border-radius: 8px;
        flex: 1 1 300px;
        box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
      }
      .card h3 {
        margin-top: 0;
        color: #3498db;
      }
      footer {
        text-align: center;
        padding: 15px 10px;
        background-color: #ddd;
        color: #555;
        margin-top: 40px;
      }

      /* 响应式调整 */
      @media (max-width: 768px) {
        nav {
          flex-direction: column;
        }
        nav a {
          border-right: none;
          border-bottom: 1px solid #1c5980;
          flex: none;
        }
        nav a:last-child {
          border-bottom: none;
        }
        .banner {
          height: 200px;
          font-size: 1.5em;
        }
      }
    </style>
  </head>
  <body>
    <header>
      <h1>我的响应式首页</h1>
    </header>

    <nav>
      <a href="#">首页</a>
      <a href="#">关于</a>
      <a href="#">服务</a>
      <a href="#">联系</a>
    </nav>

    <div class="banner">欢迎来到我的网站</div>

    <main>
      <div class="content">
        <section class="card">
          <h3>特色一</h3>
          <p>这里是特色一的描述,简洁明了,突出重点。</p>
        </section>
        <section class="card">
          <h3>特色二</h3>
          <p>这里是特色二的描述,内容简洁且易懂。</p>
        </section>
        <section class="card">
          <h3>特色三</h3>
          <p>这里是特色三的描述,帮助用户了解更多信息。</p>
        </section>
      </div>
    </main>

    <footer>&copy; 2025 响应式首页 版权所有</footer>
  </body>
</html>