新增工具! 探索最新内容

首页/组件/响应式导航栏

响应式导航栏 examples

移动优先的导航栏,包含 Logo、链接、行动号召,以及在中等断点以下折叠的汉堡菜单。

一个导航栏:桌面端显示水平链接行,在 md 断点以下折叠为汉堡菜单。包含 Logo 插槽、主要行动号召、键盘支持,以及屏幕阅读器正确播报菜单所需的 ARIA 属性。

HTML 版本适用于任何项目,React 版本适用于 Next.js、Vite 或 Create React App。除 Tailwind CSS 外没有任何依赖。

Classic responsive navbar

A flat white bar with a hairline border, a link row, and a square-cornered call-to-action. Collapses to a hamburger with aria-expanded wiring and Escape to close. The safe default for most sites.

Preview
"use client";

import { useEffect, useState } from "react";

const links = ["Icons", "Tools", "Components"];

export default function Navbar() {
  const [open, setOpen] = useState(false);

  useEffect(() => {
    if (!open) return;
    const onKey = (e: KeyboardEvent) => {
      if (e.key === "Escape") setOpen(false);
    };
    document.addEventListener("keydown", onKey);
    return () => document.removeEventListener("keydown", onKey);
  }, [open]);

  return (
    <header className="border-b border-gray-200 bg-white">
      <nav className="mx-auto flex max-w-7xl items-center justify-between px-4 py-4">
        <a href="#">
          <img src="https://www.utilityzoo.com/logo.svg" alt="Utility Zoo" className="h-6 w-auto" />
        </a>

        <ul className="hidden items-center gap-8 md:flex">
          {links.map((label) => (
            <li key={label}>
              <a href="#" className="text-sm font-medium text-gray-600 hover:text-gray-900">{label}</a>
            </li>
          ))}
        </ul>

        <a href="#" className="hidden rounded-md bg-gray-900 px-4 py-2 text-sm font-medium text-white hover:bg-gray-700 md:inline-block">
          Get started
        </a>

        <button
          type="button"
          onClick={() => setOpen(!open)}
          aria-expanded={open}
          aria-controls="mobile-menu"
          aria-label={open ? "Close main menu" : "Open main menu"}
          className="rounded-md p-2 text-gray-600 hover:bg-gray-100 focus:outline-none focus-visible:ring-2 focus-visible:ring-gray-900 md:hidden"
        >
          <svg className="h-6 w-6" fill="none" stroke="currentColor" strokeWidth={1.5} viewBox="0 0 24 24" aria-hidden="true">
            <path strokeLinecap="round" strokeLinejoin="round" d={open ? "M6 18 18 6M6 6l12 12" : "M3.75 6.75h16.5M3.75 12h16.5M3.75 17.25h16.5"} />
          </svg>
        </button>
      </nav>

      {open && (
        <div id="mobile-menu" className="border-t border-gray-200 md:hidden">
          <ul className="space-y-1 px-4 py-4">
            {links.map((label) => (
              <li key={label}>
                <a href="#" className="block rounded-md px-3 py-2 text-base font-medium text-gray-700 hover:bg-gray-100">{label}</a>
              </li>
            ))}
            <li className="pt-2">
              <a href="#" className="block rounded-md bg-gray-900 px-3 py-2 text-center text-base font-medium text-white">
                Get started
              </a>
            </li>
          </ul>
        </div>
      )}
    </header>
  );
}

Sticky navbar with glass blur

Stays pinned to the top while content scrolls beneath a translucent, blurred bar. Pill-shaped hover states on the links. The mobile menu uses a native details element, so there is no JavaScript.

Preview
export default function StickyNavbar() {
  return (
    <header className="sticky top-0 z-50 border-b border-gray-200/60 bg-white/70 backdrop-blur-md">
      <nav className="relative mx-auto flex max-w-7xl items-center justify-between px-4 py-3">
        <a href="#"><img src="https://www.utilityzoo.com/logo.svg" alt="Utility Zoo" className="h-6 w-auto" /></a>

        <ul className="hidden items-center gap-1 md:flex">
          <li><a href="#" className="rounded-full px-3 py-1.5 text-sm text-gray-600 hover:bg-gray-100 hover:text-gray-900">Icons</a></li>
          <li><a href="#" className="rounded-full px-3 py-1.5 text-sm text-gray-600 hover:bg-gray-100 hover:text-gray-900">Tools</a></li>
          <li><a href="#" className="rounded-full px-3 py-1.5 text-sm text-gray-600 hover:bg-gray-100 hover:text-gray-900">Components</a></li>
        </ul>

        <a href="#" className="hidden rounded-full bg-gray-900 px-4 py-1.5 text-sm font-medium text-white hover:bg-gray-700 md:inline-block">Sign in</a>

        <details className="md:hidden">
          <summary aria-label="Toggle main menu" className="flex cursor-pointer list-none rounded-md p-2 text-gray-600 hover:bg-gray-100 [&::-webkit-details-marker]:hidden">
            <svg className="h-6 w-6" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="M3.75 6.75h16.5M3.75 12h16.5M3.75 17.25h16.5" /></svg>
          </summary>
          <div className="absolute inset-x-0 top-full border-b border-gray-200 bg-white/95 px-4 py-3 shadow-sm backdrop-blur-md">
            <ul className="space-y-1">
              <li><a href="#" className="block rounded-xl px-3 py-2.5 text-base text-gray-700 hover:bg-gray-100">Icons</a></li>
              <li><a href="#" className="block rounded-xl px-3 py-2.5 text-base text-gray-700 hover:bg-gray-100">Tools</a></li>
              <li><a href="#" className="block rounded-xl px-3 py-2.5 text-base text-gray-700 hover:bg-gray-100">Components</a></li>
              <li className="pt-2"><a href="#" className="block rounded-full bg-gray-900 px-3 py-2.5 text-center text-base font-medium text-white">Sign in</a></li>
            </ul>
          </div>
        </details>
      </nav>
    </header>
  );
}

Floating rounded navbar

A detached, fully rounded bar that floats above the page with a soft colored shadow. Pairs well with a hero that has a gradient or image background.

Preview
export default function FloatingNavbar() {
  return (
    <header className="px-4 pt-4">
      <nav className="relative mx-auto flex max-w-4xl items-center justify-between rounded-full border border-white/70 bg-white/85 px-5 py-2.5 shadow-lg shadow-sky-200/60 backdrop-blur">
        <a href="#"><img src="https://www.utilityzoo.com/logo.svg" alt="Utility Zoo" className="h-6 w-auto" /></a>

        <ul className="hidden items-center gap-7 md:flex">
          <li><a href="#" className="text-sm font-medium text-gray-600 hover:text-gray-900">Icons</a></li>
          <li><a href="#" className="text-sm font-medium text-gray-600 hover:text-gray-900">Tools</a></li>
          <li><a href="#" className="text-sm font-medium text-gray-600 hover:text-gray-900">Components</a></li>
        </ul>

        <a href="#" className="hidden rounded-full bg-[#168f9a] px-4 py-1.5 text-sm font-medium text-white hover:bg-[#127782] md:inline-block">Get started</a>

        <details className="md:hidden">
          <summary aria-label="Toggle main menu" className="flex cursor-pointer list-none rounded-full p-2 text-gray-700 hover:bg-gray-100 [&::-webkit-details-marker]:hidden">
            <svg className="h-6 w-6" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="M3.75 6.75h16.5M3.75 12h16.5M3.75 17.25h16.5" /></svg>
          </summary>
          <div className="absolute inset-x-0 top-full mt-3 rounded-3xl bg-white p-3 shadow-xl ring-1 ring-black/5">
            <ul className="space-y-1">
              <li><a href="#" className="block rounded-2xl px-4 py-2.5 text-base font-medium text-gray-700 hover:bg-gray-100">Icons</a></li>
              <li><a href="#" className="block rounded-2xl px-4 py-2.5 text-base font-medium text-gray-700 hover:bg-gray-100">Tools</a></li>
              <li><a href="#" className="block rounded-2xl px-4 py-2.5 text-base font-medium text-gray-700 hover:bg-gray-100">Components</a></li>
              <li className="pt-1"><a href="#" className="block rounded-full bg-[#168f9a] px-4 py-2.5 text-center text-base font-medium text-white">Get started</a></li>
            </ul>
          </div>
        </details>
      </nav>
    </header>
  );
}

Dark navbar with dropdown menu

A near-black bar with an inverted logo and a Tools dropdown that opens on hover and on keyboard focus. On mobile the dropdown items flatten into the main list.

Preview
export default function DarkNavbar() {
  return (
    <header className="border-b border-white/10 bg-gray-950">
      <nav className="relative mx-auto flex max-w-7xl items-center justify-between px-4 py-4">
        <a href="#"><img src="https://www.utilityzoo.com/logo.svg" alt="Utility Zoo" className="h-6 w-auto brightness-0 invert" /></a>

        <ul className="hidden items-center gap-8 md:flex">
          <li className="group relative">
            <button type="button" className="flex items-center gap-1 text-sm text-gray-300 hover:text-white focus:outline-none focus-visible:text-white">
              Tools
              <svg className="h-4 w-4" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
            </button>
            <div className="invisible absolute left-0 top-full z-10 w-56 pt-3 opacity-0 transition group-focus-within:visible group-focus-within:opacity-100 group-hover:visible group-hover:opacity-100">
              <ul className="rounded-lg bg-white p-2 shadow-lg ring-1 ring-black/5">
                <li><a href="#" className="block rounded-md px-3 py-2 text-sm text-gray-700 hover:bg-gray-100">Code tools</a></li>
                <li><a href="#" className="block rounded-md px-3 py-2 text-sm text-gray-700 hover:bg-gray-100">Audio tools</a></li>
                <li><a href="#" className="block rounded-md px-3 py-2 text-sm text-gray-700 hover:bg-gray-100">PDF tools</a></li>
              </ul>
            </div>
          </li>
          <li><a href="#" className="text-sm text-gray-300 hover:text-white">Icons</a></li>
          <li><a href="#" className="text-sm text-gray-300 hover:text-white">Components</a></li>
        </ul>

        <a href="#" className="hidden rounded-md bg-white px-4 py-2 text-sm font-medium text-gray-900 hover:bg-gray-200 md:inline-block">Get started</a>

        <details className="md:hidden">
          <summary aria-label="Toggle main menu" className="flex cursor-pointer list-none rounded-md p-2 text-gray-300 hover:bg-gray-800 [&::-webkit-details-marker]:hidden">
            <svg className="h-6 w-6" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="M3.75 6.75h16.5M3.75 12h16.5M3.75 17.25h16.5" /></svg>
          </summary>
          <div className="absolute inset-x-0 top-full border-t border-white/10 bg-gray-950 px-4 py-3">
            <ul className="space-y-1">
              <li><a href="#" className="block rounded-md px-3 py-2 text-base text-gray-200 hover:bg-gray-800">Code tools</a></li>
              <li><a href="#" className="block rounded-md px-3 py-2 text-base text-gray-200 hover:bg-gray-800">Audio tools</a></li>
              <li><a href="#" className="block rounded-md px-3 py-2 text-base text-gray-200 hover:bg-gray-800">PDF tools</a></li>
              <li><a href="#" className="block rounded-md px-3 py-2 text-base text-gray-200 hover:bg-gray-800">Icons</a></li>
              <li><a href="#" className="block rounded-md px-3 py-2 text-base text-gray-200 hover:bg-gray-800">Components</a></li>
              <li className="pt-2"><a href="#" className="block rounded-md bg-white px-3 py-2 text-center text-base font-medium text-gray-900">Get started</a></li>
            </ul>
          </div>
        </details>
      </nav>
    </header>
  );
}

Centered logo navbar with announcement bar

A gradient announcement strip above a three-column bar: links on the left, centered logo, actions on the right. Uppercase tracked links give it an editorial feel.

Preview
export default function CenteredNavbar() {
  return (
    <header className="bg-white">
      <div className="bg-gradient-to-r from-[#168f9a] via-[#2bb4c0] to-[#5ed6c0] px-4 py-2 text-center text-xs font-medium text-white">
        New: 40 more free tools just launched.
        <a href="#" className="underline underline-offset-2">See what is new</a>
      </div>

      <nav className="relative mx-auto grid max-w-7xl grid-cols-3 items-center border-b border-gray-200 px-4 py-4">
        <ul className="hidden items-center gap-6 md:flex">
          <li><a href="#" className="text-xs font-semibold uppercase tracking-wider text-gray-600 hover:text-gray-900">Icons</a></li>
          <li><a href="#" className="text-xs font-semibold uppercase tracking-wider text-gray-600 hover:text-gray-900">Tools</a></li>
          <li><a href="#" className="text-xs font-semibold uppercase tracking-wider text-gray-600 hover:text-gray-900">Components</a></li>
        </ul>

        <details className="md:hidden">
          <summary aria-label="Toggle main menu" className="flex cursor-pointer list-none rounded-md p-2 text-gray-600 hover:bg-gray-100 [&::-webkit-details-marker]:hidden w-fit">
            <svg className="h-6 w-6" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="M3.75 6.75h16.5M3.75 12h16.5M3.75 17.25h16.5" /></svg>
          </summary>
          <div className="absolute inset-x-0 top-full border-b border-gray-200 bg-white px-4 py-3 shadow-sm">
            <ul className="space-y-1">
              <li><a href="#" className="block rounded-md px-3 py-2 text-sm font-semibold uppercase tracking-wider text-gray-700 hover:bg-gray-100">Icons</a></li>
              <li><a href="#" className="block rounded-md px-3 py-2 text-sm font-semibold uppercase tracking-wider text-gray-700 hover:bg-gray-100">Tools</a></li>
              <li><a href="#" className="block rounded-md px-3 py-2 text-sm font-semibold uppercase tracking-wider text-gray-700 hover:bg-gray-100">Components</a></li>
            </ul>
          </div>
        </details>

        <a href="#" className="justify-self-center"><img src="https://www.utilityzoo.com/logo.svg" alt="Utility Zoo" className="h-7 w-auto" /></a>

        <div className="flex items-center justify-end gap-4">
          <a href="#" className="hidden text-sm text-gray-600 hover:text-gray-900 sm:block">Sign in</a>
          <a href="#" className="rounded-full border border-gray-300 px-4 py-1.5 text-sm font-medium text-gray-900 hover:bg-gray-50">Get started</a>
        </div>
      </nav>
    </header>
  );
}

What is a 响应式导航栏?

响应式导航栏是页面顶部的水平条,承载网站的身份标识和主要导航。在宽屏上,它把这些链接排成一行,因为空间足够。在窄屏上,这一行要么折成难以阅读的一团,要么溢出边缘,因此链接会移入一个通过按钮(即汉堡按钮)打开和关闭的面板。这个组件实际上是同一内容的两种布局,由断点决定显示哪一种。

它是界面工作中最常被用到的组件,也是最常被做坏的组件。只在两个特定宽度下看起来正常的导航栏,把键盘用户困在打开菜单里的导航栏,或者因为面板用透明度而不是从文档中移除来隐藏、从而对辅助技术隐藏其链接的导航栏,都会以构建者看不见、而受影响者一眼可见的方式失败。下面的标记正是为避免这些具体失败而编写的,而不是为了成为最短的版本。

断点的选择比看上去更重要。在 md(768px)折叠是常见默认值,在有四五个较短链接时有效。如果有七个链接或标签较长,链接行会在 768px 之前就放不下,布局会在“放不下”和“折叠”之间的空档里崩坏。请在真实浏览器中检查最宽的标签,如果链接行很紧,就把断点提到 lg--唯一需要更改的就是类名前缀。

这里的手机面板在关闭时会从文档中移除:HTML 版本使用 hidden 属性,React 中使用条件渲染,而不是用类来隐藏。这种差别不只是外观上的:用 opacity-0 或 transform 隐藏的面板仍然可聚焦、仍会被朗读,因此键盘和屏幕阅读器用户可以 Tab 进入他们看不见的链接。移除元素才是人们所期望的行为。

Anatomy

页头
最外层的地标元素。使用真正的 header 标签而不是 div,可为辅助技术提供一个可以跳转的 banner 地标,许多屏幕阅读器用户正是这样跳到页面顶部的。
品牌标识
链接到首页的 Logo 或文字标识。让它在 DOM 中排第一,这样 Tab 进入页面时最先到达的就是它。
桌面链接行
一个链接列表,在断点以下通过 hidden md:flex 隐藏。它是真正的 ul,因此屏幕阅读器会在朗读之前先播报其中包含的项目数量。
行动号召
你最希望访客执行的那个操作,样式与链接明显不同,避免被当作另一个导航项。
切换按钮
汉堡按钮,仅在断点以下可见。带有 aria-expanded、aria-controls,以及随状态变化的 aria-label,使按钮播报它将要做什么,而不是它看起来像什么。
手机面板
由切换按钮展开的纵向链接列表。关闭时从文档中移除,并且由于要用拇指操作,点击目标比桌面链接更大。

How to Use

  1. 1

    复制标记

    根据你的技术栈选择 HTML 或 React 版本。在 Next.js App Router 中,请保留 React 版本顶部的 use client 指令--该组件持有打开状态,不能是服务端组件。

  2. 2

    替换品牌标识和链接

    把文字标识换成你的 Logo,并编辑 links 数组。在 HTML 版本中,桌面链接行和手机面板是各自独立的标记,因此两处都要改;在 React 版本中,它们共用同一个数组。

  3. 3

    设置断点

    组件默认在 md 折叠。如果你的链接行很拥挤,把每个 md: 前缀改成 lg:,它就会更早折叠。请在最长标签所在的宽度下测试。

  4. 4

    重新设计外观

    修改页头上的背景、边框和文字类来匹配你的设计。内部所有内容的间距都继承自 nav 容器,因此更换颜色不会破坏布局。

  5. 5

    用键盘检查

    用 Tab 浏览导航栏,用 Enter 或空格打开菜单,Tab 进入链接,然后按 Escape。如果其中任何一步出现意外行为,问题几乎总是缺少焦点样式,而不是缺少逻辑。

Features

移动优先,带真实断点

堆叠布局是默认,水平行是增强,这与 Tailwind 响应式前缀的设计用法一致,并避免了大量不必要的覆盖。

正确的 ARIA 关联

切换按钮带有指向面板 id 的 aria-expanded 和 aria-controls,因此辅助技术会播报菜单是否已打开以及它控制哪个元素,而不是朗读一个没有标签的按钮。

按 Escape 关闭

按 Escape 会关闭已打开的菜单并把焦点返回到切换按钮,这是键盘用户最先尝试的操作,也是大多数复制粘贴式导航栏遗漏的功能。

面板被移除,而不只是隐藏

关闭时,手机链接会完全离开文档,因此在不可见时无法被 Tab 到,也不会被朗读。

手机上更大的点击目标

堆叠的链接使用比桌面行更大的文字和更多的纵向内边距,使每个目标都舒适地超出拇指可靠点击所需的尺寸。

零依赖

没有无头 UI 库,没有动画包,没有图标包--汉堡图标是内联 SVG,行为只是几行状态代码。

Common Uses

营销和产品网站

落地页页头的标准形态:少量板块链接、一个 Logo,以及位于导航栏末端的单个转化操作。

文档网站

可作为文档侧边栏上方的顶层栏,容纳版本切换器、搜索和指向代码仓库的链接,而侧边栏负责页面树。

仪表盘和应用外壳

用作登录后应用中常驻的顶栏,链接行替换为工作区切换器,行动号召的位置放置账户菜单。

博客和出版物

顶部是分类链接,末端是订阅操作,并在大多数阅读实际发生的手机上干净地折叠。

Accessibility

Customization

Props

PropTypeDefaultDescription
links{ label: string; href: string }[][]同时渲染在桌面链接行和手机面板中的导航链接。请保持在七项或更少;超过这个数量,下拉菜单或超级菜单会更易读。
logoReactNode-替换默认的文字标识。可传入图片、SVG 或文字--该插槽不限制放入的内容。
cta{ label: string; href: string }-显示在导航栏末端和手机面板底部的主要操作。省略它即可得到仅含链接的导航栏。

Why Use These 响应式导航栏s?

大多数复制粘贴式导航栏只是为了在截图里看起来正确。这一个则是为了经受住在生产环境中搞坏导航栏的三件事:在断点之前就超出容器的链接行、无法关闭自己所开启内容的键盘用户,以及仍在 Tab 顺序中的隐藏面板。

它没有依赖。无头 UI 库会免费为你提供焦点管理,但也会带来需要追踪的版本、需要发布的包体积,以及要学习的 API,而这个组件的本质只是一个按钮和一个列表。在这里,全部行为都清楚地呈现在你复制的代码片段中。

HTML 和 React 版本生成完全相同的标记,因此在静态原型中确认的设计,在迁入框架后不会改变。

Tips

Frequently Asked Questions