Responsive Navbar examples
แถบนำทางที่ออกแบบด้วยแนวคิด mobile-first ประกอบด้วยโลโก้, ลิงก์, ปุ่มเรียกร้องให้ดำเนินการ (call-to-action) และเมนูแฮมเบอร์เกอร์ที่จะพับซ่อนเมื่อหน้าจอมีขนาดเล็กกว่า breakpoint ขนาด medium
แถบนำทางที่จะแสดงลิงก์เรียงเป็นแนวนอนบนหน้าจอเดสก์ท็อป และจะยุบรวมเป็นเมนูแฮมเบอร์เกอร์เมื่อหน้าจอมีขนาดต่ำกว่า breakpoint ขนาด md มาพร้อมกับช่องสำหรับใส่โลโก้, ปุ่มเรียกร้องให้ดำเนินการหลัก (primary call-to-action), การรองรับคีย์บอร์ด และแอตทริบิวต์ 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.
"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.
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.
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.
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.
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 Responsive Navbar?
Responsive navbar คือแถบแนวนอนที่อยู่ด้านบนสุดของหน้าเว็บซึ่งทำหน้าที่แสดงอัตลักษณ์ของเว็บไซต์และการนำทางหลัก บนหน้าจอขนาดกว้าง แถบนี้จะแสดงลิงก์เรียงเป็นแถวเนื่องจากมีพื้นที่เพียงพอ แต่บนหน้าจอขนาดแคบ แถวนี้จะแสดงผลผิดเพี้ยนกลายเป็นกลุ่มข้อความที่อ่านไม่ออกหรือล้นขอบจอ ดังนั้น ลิงก์จึงถูกย้ายไปอยู่ในพาเนลที่เปิดและปิดได้ผ่านปุ่มแฮมเบอร์เกอร์ คอมโพเนนต์นี้แท้จริงแล้วคือเลย์เอาต์สองแบบสำหรับเนื้อหาเดียวกัน โดยมี breakpoint เป็นตัวตัดสินว่าเลย์เอาต์ใดควรแสดงผล
นี่คือคอมโพเนนต์ที่มีการเรียกใช้งานบ่อยที่สุดในการออกแบบส่วนติดต่อผู้ใช้ และในขณะเดียวกันก็เป็นคอมโพเนนต์ที่มักจะถูกสร้างขึ้นมาได้อย่างย่ำแย่บ่อยที่สุดด้วยเช่นกัน แนบาร์ที่ดูดีเฉพาะในความกว้างสองขนาดเท่านั้น, แนบาร์ที่กักขังผู้ใช้คีย์บอร์ดให้อยู่ภายในเมนูที่เปิดอยู่, หรือแนบาร์ที่ซ่อนลิงก์จากเทคโนโลยีสิ่งอำนวยความสะดวกเนื่องจากพาเนลถูกกำหนดสไตล์ด้วย opacity แทนที่จะถูกถอดออกจาก DOM ทั้งหมดนี้ล้วนล้มเหลวในรูปแบบที่ผู้สร้างมองไม่เห็น แต่ผู้ใช้งานจริงกลับพบเจอได้อย่างชัดเจน โค้ดด้านล่างนี้ถูกเขียนขึ้นมาเพื่อหลีกเลี่ยงความผิดพลาดเฉพาะเหล่านี้ แทนที่จะทำให้เป็นเวอร์ชันที่สั้นที่สุดเท่าที่จะเป็นไปได้
การเลือก breakpoint มีความสำคัญมากกว่าที่คิด การยุบเมนูที่ขนาด md (768px) คือค่าเริ่มต้นทั่วไป และจะใช้งานได้ดีเมื่อคุณมีลิงก์สั้นๆ สี่หรือห้าลิงก์ แต่หากมีลิงก์เจ็ดลิงก์ หรือมีป้ายกำกับ (label) ที่ยาว แถวนี้จะหมดพื้นที่ก่อนถึง 768px และเลย์เอาต์จะพังในช่วงรอยต่อระหว่างจุดที่พื้นที่ไม่พอและจุดที่เมนเริ่มยุบ ตรวจสอบป้ายกำกับที่ยาวที่สุดในเบราว์เซอร์จริง และขยับ breakpoint ขึ้นไปที่ lg หากแถวเริ่มแน่น ซึ่งคำนำหน้าคลาส (class prefixes) เป็นสิ่งเดียวที่คุณจะต้องเปลี่ยน
พาเนลมือถือในที่นี้จะถูกนำออกจากเอกสารเมื่อปิดการใช้งาน โดยใช้แอตทริบิวต์ hidden ในเวอร์ชัน HTML และใช้การเรนเดอร์ตามเงื่อนไข (conditional rendering) ใน React แทนที่จะเป็นการซ่อนด้วยคลาส ความแตกต่างนี้ไม่ใช่แค่เรื่องความสวยงาม: พาเนลที่ถูกซ่อนด้วย opacity-0 หรือ transform นั้นยังคงสามารถโฟกัสได้และยังคงถูกอ่านออกเสียง ดังนั้นผู้ใช้คีย์บอร์ดและโปรแกรมอ่านหน้าจอจึงยังสามารถกดแท็บเข้าไปในลิงก์ที่มองไม่เห็นได้ การเอาอีเลเมนต์นั้นออกไปจาก DOM คือพฤติกรรมที่ผู้ใช้งานคาดหวัง
Anatomy
- Header
- อีเลเมนต์แลนด์มาร์กชั้นนอก การใช้แท็ก header จริงๆ แทนที่จะเป็น div จะช่วยให้เทคโนโลยีสิ่งอำนวยความสะดวก認識ถึงแบนเนอร์แลนด์มาร์กที่สามารถข้ามไปได้ ซึ่งเป็นวิธีที่ผู้ใช้โปรแกรมอ่านหน้าจอจำนวนมากใช้ในการนำทางไปยังส่วนบนสุดของหน้าเว็บ
- Brand
- โลโก้หรือข้อความชื่อแบรนด์ที่ลิงก์ไปยังหน้าแรก วางไว้เป็นลำดับแรกใน DOM เพื่อให้เป็นสิ่งแรกที่เข้าถึงได้เมื่อกดแท็บเข้ามาในหน้าเว็บ
- Desktop link row
- รายการลิงก์ที่ถูกซ่อนไว้ใต้ breakpoint ด้วยคลาส hidden md:flex นี่คือแท็ก ul จริงๆ เพื่อให้โปรแกรมอ่านหน้าจอนำเสนอจำนวนรายการทั้งหมดก่อนที่จะเริ่มอ่านเนื้อหาด้านใน
- Call-to-action
- การกระทำอย่างเดียวที่คุณต้องการให้ผู้เยี่ยมชมดำเนินการมากที่สุด โดยมีการจัดสไตล์ที่แตกต่างจากลิงก์ทั่วไปอย่างชัดเจน เพื่อไม่ให้ผู้ใช้อ่านมันเป็นรายการนำทางอื่น
- Toggle button
- ปุ่มแฮมเบอร์เกอร์ที่จะแสดงผลเฉพาะใต้ breakpoint เท่านั้น มาพร้อมกับแอตทริบิวต์ aria-expanded, aria-controls และ aria-label ที่เปลี่ยนแปลงตามสถานะ เพื่อให้ปุ่มประกาศว่ามันจะทำอะไร แทนที่จะบอกว่าหน้าตาของมันเป็นอย่างไร
- Mobile panel
- รายการลิงก์แบบซ้อนกันที่จะแสดงขึ้นมาเมื่อถูกเปิดใช้งานด้วยปุ่ม toggle จะถูกนำออกจากเอกสารเมื่อปิด และกำหนดให้มี tap target ที่ใหญ่กว่าลิงก์บนเดสก์ท็อปเนื่องจากถูกใช้งานด้วยนิ้วโป้ง
How to Use
- 1
คัดลอกมาร์กอัป
เลือกเวอร์ชัน HTML หรือ React ตามสแต็กของคุณ ใน Next.js App Router ให้ใส่ directive use client ไว้ที่ด้านบนสุดของเวอร์ชัน React เนื่องจากคอมโพเนนต์นี้มีการเก็บสถานะเปิด/ปิด จึงไม่สามารถเป็น Server Component ได้
- 2
แทนที่แบรนด์และลิงก์
เปลี่ยนเวิร์ดมาร์กเป็นโลโก้ของคุณและแก้ไขอาร์เรย์ links สำหรับเวอร์ชัน HTML แถวเดสก์ท็อปและโมบายล์แพเนลจะเป็นมาร์กอัปแยกกัน ดังนั้นจึงต้องแก้ไขทั้งสองส่วน ส่วนเวอร์ชัน React จะใช้อาร์เรย์เดียวกันร่วมกัน
- 3
กำหนด breakpoint ของคุณ
คอมโพเนนต์นี้จะยุบการแสดงผลที่ breakpoint md เป็นค่าเริ่มต้น หากแถวลิงก์ของคุณแน่นเกินไป ให้เปลี่ยน prefix md: ทุกตัวเป็น lg: เพื่อให้ยุบเร็วยิ่งขึ้น ให้ทดสอบที่ความขนาดหน้าจอที่ป้ายข้อความที่ยาวที่สุดของคุณแสดงผลอยู่
- 4
ปรับแต่งสไตล์พื้นผิว
เปลี่ยนคลาส background, border และ text บน Header ให้เข้ากับดีไซน์ของคุณ ทุกอย่างข้างในจะสืบทอดระยะห่างมาจาก nav container ทำให้เลย์เอาต์ยังคงอยู่ครบถ้วนเมื่อเปลี่ยนสี
- 5
ตรวจสอบด้วยคีย์บอร์ด
กด Tab เพื่อวนผ่านแถบบาร์, เปิดเมนูด้วย Enter หรือ Space, กด Tab เข้าไปยังลิงก์ และกด Escape หากส่วนใดทำงานผิดปกติ สาเกตุส่วนใหญ่มักมาจากการขาด focus style มากกว่าตรรกะที่ขาดหายไป
Features
ออกแบบแบบ Mobile-first ด้วย breakpoint จริง
เลย์เอาต์แบบซ้อนกันคือค่าเริ่มต้น และแถวแนวนอนคือการเพิ่มประสิทธิภาพ ซึ่งตรงกับการออกแบบ responsive prefix ของ Tailwind และช่วยหลีกเลี่ยงการเขียน override ที่ไม่จำเป็นจำนวนมาก
การเชื่อมต่อ ARIA ที่ถูกต้อง
ปุ่ม toggle มีแอตทริบิวต์ aria-expanded และ aria-controls ชี้ไปยัง id ของแพเนล เพื่อให้ assistive technology ประกาศว่าเมนูกำลังเปิดอยู่และควบคุมองค์ประกอบใด แทนที่จะอ่านปุ่มที่ไม่มีป้ายกำกับ
กด Escape เพื่อปิด
การกด Escape จะเป็นการปิดเมนูที่เปิดอยู่และคืนโฟกัสกลับไปยังปุ่ม toggle ซึ่งเป็นสิ่งที่ผู้ใช้คีย์บอร์ดใช้งานเป็นอันดับแรก และเป็นสิ่งที่ Navbar แบบก๊อปปี้วางส่วนใหญ่มักมองข้าม
เอาแพเนลออก ไม่ใช่แค่ซ่อน
เมื่อปิด ลิงก์บนมือถือจะถูกนำออกจาก DOM โดยสิ้นเชิง จึงไม่สามารถกด Tab เข้าถึงหรือถูกอ่านออกเสียงได้ในขณะที่มองไม่เห็น
เพิ่มขนาด tap target บนมือถือ
ลิงก์แบบซ้อนกันจะใช้ขนาดตัวอักษรที่ใหญ่ขึ้นและ padding แนวตั้งมากกว่าแถวบนเดสก์ท็อป ทำให้แต่ละ target มีขนาดใหญ่พอดีและนิ้วโป้งสามารถกดได้อย่างแม่นยำ
ไม่มี dependency
ไม่มีไลบรารี Headless UI, ไม่มีแพ็กเกจแอนิเมชัน, ไม่มีแพ็กเกจไอคอน - แฮมเบอร์เกอร์คือ SVG แบบอินไลน์ และพฤติกรรมการทำงานใช้โค้ดสถานะเพียงไม่กี่บรรทัด
Common Uses
เว็บไซต์ด้านการตลาดและผลิตภัณฑ์
รูปแบบมาตรฐานสำหรับ Header ของ Landing Page ได้แก่ ลิงก์หมวดหมู่จำนวนหนึ่ง, โลโก้ และปุ่มกระตุ้นการตัดสินใจ (CTA) หนึ่งปุ่มที่ปลายสุดของแถบบาร์
เว็บไซต์เอกสารคู่มือ (Documentation)
ใช้งานเป็นแถบบาร์ระดับบนสุดเหนือแถบด้านข้างของเอกสาร ทำหน้าที่เก็บตัวสลับเวอร์ชัน, ช่องค้นหา และลิงก์ไปยังรีโพสิทอรี ในขณะที่แถบด้านข้างทำหน้าที่จัดการโครงสร้างหน้า
แดชบอร์ดและแอปพลิเคชันเชลล์
ทำหน้าที่เป็นแถบบาร์ด้านบนสุดในแอปพลิเคชันที่มีการเข้าสู่ระบบ โดยแทนที่แถบลิงก์ด้วยตัวสลับ Workspace และช่อง Call-to-action จะเปลี่ยนเป็นเมนูบัญชีผู้ใช้
บล็อกและเว็บไซต์สำนักพิมพ์
ลิงก์หมวดหมู่เรียงอยู่ด้านบนพร้อมปุ่มสมัครสมาชิกที่ปลายสุด ยุบการแสดงผลได้อย่างสะอาดตาบนมือถือซึ่งเป็นอุปกรณ์ที่มีการใช้งานอ่านจริงมากที่สุด
Accessibility
- ปุ่ม toggle เป็นอิลิเมนต์ button จริงๆ จึงตอบสนองต่อปุ่ม Enter และ Space และถูกประกาศว่าเป็นปุ่ม แทนที่จะเป็นรูปภาพหรือ div ที่คลิกได้
- aria-expanded สะท้อนสถานะเปิด/ปิดและอัปเดตทุกครั้งที่มีการกด toggle ซึ่งเป็นวิธีที่ screen reader ใช้แจ้งผู้ใช้ว่าการกดปุ่มจะเป็นการเปิดหรือปิดเมนู
- aria-controls ชี้ไปยัง id ของแพเนล เพื่อเชื่อมโยงสององค์ประกอบเข้าด้วยกันเพื่อให้ assistive technology สามารถสลับการทำงานระหว่างกันได้
- ชื่อที่เข้าถึงได้ (Accessible name) ของปุ่มจะเปลี่ยนไปตามสถานะ - จาก Open main menu เป็น Close main menu - เนื่องจากตัวไอคอนอย่างเดียวไม่สามารถสื่อความหมายใดๆ ให้กับผู้ที่มองไม่เห็นได้
- การกด Escape จะปิดเมนูและย้ายโฟกัสกลับไปยังปุ่ม toggle เพื่อไม่ให้ผู้ใช้คีย์บอร์ดต้องค้างโฟกัสไว้ที่องค์ประกอบที่เพิ่งถูกลบออกจากหน้า
- สไตล์โฟกัสใช้ focus-visible แทนที่จะเป็น focus เพื่อให้แสดงกรอบโฟกัสสำหรับการนำทางด้วยคีย์บอร์ดโดยไม่แสดงกรอบดังกล่าวเมื่อคลิกด้วยเมาส์
- แพเนลที่ปิดอยู่จะถูกนำออกจากเอกสารแทนที่จะถูกซ่อนด้วยการมองเห็น เพื่อให้พ้นจากทั้งลำดับการกด Tab (Tab order) และ Accessibility tree
- Links sit inside a ul so their count is announced before they are read, which helps someone decide whether to listen through the list or skip it.
Customization
- Move the collapse point by changing the md: prefixes to sm: or lg:. Every responsive class in the component uses the same prefix, so it is a single find-and-replace.
- Pin the bar with sticky top-0 z-50 on the header. If it will sit over content, add a background with some opacity plus backdrop-blur so text behind it does not show through as noise.
- Center the links by replacing justify-between on the nav with grid grid-cols-3 items-center, then letting the brand, list, and action sit in the three columns.
- Add a dropdown by nesting a group container around a link and revealing an absolutely positioned panel on hover and focus-within - focus-within is what makes it reachable by keyboard.
- Animate the mobile panel by keeping it mounted and animating max-height, but if you do, also set aria-hidden and inert on the collapsed state so the accessibility behaviour survives the change.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| links | { label: string; href: string }[] | [] | ลิงก์นำทางที่จะแสดงผลทั้งในรูปแบบแถวบนเดสก์ท็อปและในพาเนลมือถือ ควรจำกัดจำนวนไว้ที่ไม่เกิน 7 รายการ หากมากกว่านั้น การใช้ dropdown หรือ mega menu จะแสดงผลได้ดีกว่า |
| logo | ReactNode | - | ใช้แทนที่ข้อความโลโก้เริ่มต้น คุณสามารถใส่รูปภาพ, SVG หรือข้อความก็ได้ โดยช่องนี้จะไม่จำกัดประเภทของเนื้อหาที่ใส่ลงไป |
| cta | { label: string; href: string } | - | การดำเนินการหลักที่จะแสดงที่ส่วนท้ายของแถบและที่ด้านล่างของพาเนลมือถือ สามารถละเว้นได้หากต้องการแสดงแถบนำทางที่มีเพียงลิงก์อย่างเดียว |
Why Use These Responsive Navbars?
Most copy-paste navbars are built to look right in a screenshot. This one is built to survive the three things that break navbars in production: a link row that outgrows its container before the breakpoint, a keyboard user who cannot close what they opened, and a hidden panel that is still in the tab order.
It has no dependencies. A headless UI library would give you focus management for free, but it also gives you a version to track, a bundle to ship, and an API to learn for a component that is fundamentally a button and a list. Here the entire behaviour is visible in the snippet you copy.
The HTML and React versions produce identical markup, so a design signed off in a static prototype does not shift when it moves into a framework.
Tips
- Test at 320px, not just at your phone's width. That is the narrowest viewport still in common use, and it is where a long brand name next to a hamburger first starts to collide.
- If you have more than about seven top-level links, the problem is the information architecture rather than the navbar. Group them behind two or three parent items before reaching for a mega menu.
- Lock body scroll while a full-screen mobile menu is open, or the page behind it will scroll under the panel when someone swipes. A single overflow-hidden class on the body, added and removed with the open state, is enough.
- In Next.js, swap the anchor tags for the Link component so navigation is client-side. The classes stay exactly the same.
- Do not animate the hamburger into an X unless the animation is short. Anything over about 200ms makes the menu feel slower than it is, because the panel cannot be read until the motion settles.
- If the navbar sits over a hero image, check the contrast of the link text against the lightest part of that image, not against the average.