Skip to content

Responsive 导航 Bar

简单 约 20 分钟

你将构建什么

HTML page 使用导航 bar 包含 logo on 左 and 4 链接 on 正确, using flexbox. On mobile, 链接 stack vertically below logo.

学习目标

  • Use 语义 <nav> 元素 for 导航
  • Apply flexbox 到 align logo and 链接 horizontally
  • 添加 media 查询 for mobile vertical 布局
  • 样式链接使用 hover 状态 for interactivity

前置要求

  • HTML 结构 and 语义标签
  • 基础 CSS 包含 flexbox

步骤

  1. 1

    编写 HTML 结构

    创建 nav.HTML 使用 nav 元素包含 logo (as 文本) and ul 的 4 链接.

    代码
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Navigation</title>
        <link rel="stylesheet" href="nav.css">
      </head>
      <body>
        <nav>
          <div class="logo">MySite</div>
          <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Services</a></li>
            <li><a href="#">Contact</a></li>
          </ul>
        </nav>
      </body>
    </html>

    预期结果

    page 使用 nav 包含 logo and 列表的 链接使用默认样式.

  2. 2

    样式 nav 使用 flexbox

    创建 nav.CSS. Use flexbox on nav 到推送 logo 左 and 链接正确使用 justify-内容: 空间-之间.

    代码
    nav {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 1rem 2rem;
      background: #1e293b;
    }
    .logo {
      font-size: 1.5rem;
      font-weight: bold;
      color: #fff;
    }

    预期结果

    logo appears on 左 and 列表 on 正确, vertically centered.

  3. 3

    样式链接

    移除列表样式 and 制作链接 horizontal 使用 flexbox. 添加 hover 状态.

    代码
    nav ul {
      display: flex;
      gap: 1.5rem;
      list-style: none;
      margin: 0;
      padding: 0;
    }
    nav a {
      color: #cbd5e1;
      text-decoration: none;
      transition: color 0.2s;
    }
    nav a:hover {
      color: #fff;
    }

    预期结果

    链接是 horizontal 使用否 bullets, and turn white on hover.

  4. 4

    添加 mobile responsive 布局

    添加 media 查询 for 最大值-宽度: 600px 到 stack nav vertically on mobile.

    代码
    @media (max-width: 600px) {
      nav {
        flex-direction: column;
        gap: 1rem;
      }
      nav ul {
        flex-direction: column;
        gap: 0.5rem;
        align-items: center;
      }
    }

    预期结果

    On mobile, logo and 链接 stack vertically and 中心.

  5. 5

    添加响应式样式

    使用媒体查询为移动端调整布局:在小屏幕上将多列改为单列、缩小字号、调整间距。移动优先意味着小屏幕体验同样重要。

    代码
    @media (max-width: 640px) {
      .container {
        flex-direction: column;
        padding: 1rem;
      }
      h1 { font-size: 1.5rem; }
      .card { width: 100%; margin-bottom: 1rem; }
    }

    预期结果

    在 640px 以下视口,布局变为单列,字号和间距缩小,内容不溢出。

自己动手试试

你学到了什么

你构建 responsive 导航 bar 使用 flexbox, 语义 HTML, and mobile breakpoint — 导航模式 used on virtually 每个 website.

相关资源

相关课程

相关练习

相关速查表

相关术语

  • CSS

    Cascading 样式 Sheets.

  • HTML

    HyperText Markup Language.

相关术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

  • CSS

    Cascading 样式 Sheets.

  • HTML

    HyperText Markup Language.

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回主题