Tailwind Navbar
A navigation bar sits at the top of almost every site. It must look good on wide screens, collapse into a menu on phones and stay usable with a keyboard. This lesson builds one step by step.
Step 1 — a simple horizontal bar
The core is one flex row: logo on the left, links pushed to the right with ml-auto (or justify-between).
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="bg-gray-50">
<nav class="flex items-center gap-6 bg-white px-6 py-3 shadow-sm">
<a href="#" class="text-lg font-bold text-indigo-600">🌊 Wave</a>
<div class="ml-auto flex gap-5 text-sm font-medium text-gray-600">
<a href="#" class="text-indigo-600">Home</a>
<a href="#" class="hover:text-gray-900">Features</a>
<a href="#" class="hover:text-gray-900">Pricing</a>
<a href="#" class="hover:text-gray-900">Blog</a>
</div>
</nav>
</body>
</html>| Piece | Classes | Why |
|---|---|---|
| Bar | flex items-center |
one row, vertically centred |
| Spacing | px-6 py-3 gap-6 |
breathing room |
| Right group | ml-auto |
consumes the free space, pushing links right |
| Active link | a different colour | shows where you are |
Step 2 — active states, a call to action and sticky positioning
Underlines drawn with a bottom border make the active page clear. sticky top-0 keeps the bar visible while scrolling; bg-white/80 backdrop-blur gives it a glassy look over content.
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="h-[600px] bg-linear-to-b from-indigo-100 to-pink-100">
<nav class="sticky top-0 z-20 flex h-14 items-center gap-6 border-b border-gray-200 bg-white/80 px-6 backdrop-blur">
<a href="#" class="font-bold">🌊 Wave</a>
<div class="flex h-full gap-5 text-sm font-medium *:flex *:items-center *:border-b-2">
<a href="#" aria-current="page" class="border-indigo-500 text-gray-900">Home</a>
<a href="#" class="border-transparent text-gray-500 hover:border-gray-300 hover:text-gray-800">Features</a>
<a href="#" class="border-transparent text-gray-500 hover:border-gray-300 hover:text-gray-800">Pricing</a>
</div>
<a href="#" class="ml-auto rounded-full bg-indigo-600 px-4 py-1.5 text-sm font-semibold text-white hover:bg-indigo-500">Sign up</a>
</nav>
<p class="p-6 text-gray-600">Scroll down inside the preview — the bar sticks to the top.</p>
</body>
</html>Tip: Mark the current page with
aria-current="page". You can even style it directly:aria-[current=page]:text-indigo-600.
Step 3 — responsive with a mobile menu
On small screens, hide the links (hidden md:flex) and show a menu button instead (md:hidden). A few lines of JavaScript toggle the mobile panel. Drag the preview narrower than 768px to see the hamburger appear.
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="bg-gray-100">
<nav class="bg-slate-900 text-white">
<div class="flex h-14 items-center px-4">
<a href="#" class="font-bold">🌊 Wave</a>
<div class="ml-8 hidden gap-1 text-sm md:flex">
<a href="#" class="rounded-md bg-slate-700 px-3 py-2">Dashboard</a>
<a href="#" class="rounded-md px-3 py-2 text-slate-300 hover:bg-slate-800 hover:text-white">Team</a>
<a href="#" class="rounded-md px-3 py-2 text-slate-300 hover:bg-slate-800 hover:text-white">Projects</a>
</div>
<button id="menu-btn" aria-expanded="false" aria-controls="mobile-menu"
class="group ml-auto rounded-md p-2 hover:bg-slate-800 md:hidden">
<span class="sr-only">Open menu</span>
<span class="text-xl group-aria-expanded:hidden">☰</span>
<span class="hidden text-xl group-aria-expanded:inline">✕</span>
</button>
</div>
<div id="mobile-menu" class="hidden space-y-1 border-t border-slate-700 px-2 py-2 md:hidden">
<a href="#" class="block rounded-md bg-slate-700 px-3 py-2">Dashboard</a>
<a href="#" class="block rounded-md px-3 py-2 text-slate-300 hover:bg-slate-800">Team</a>
<a href="#" class="block rounded-md px-3 py-2 text-slate-300 hover:bg-slate-800">Projects</a>
</div>
</nav>
<script>
const btn = document.getElementById('menu-btn');
btn.addEventListener('click', () => {
const open = btn.getAttribute('aria-expanded') === 'true';
btn.setAttribute('aria-expanded', String(!open));
document.getElementById('mobile-menu').classList.toggle('hidden', open);
});
</script>
</body>
</html>Notice how the icon swaps without extra JavaScript: the button is a group, and group-aria-expanded: reacts to its aria-expanded="true" attribute — keeping the visual state and the accessibility state in sync.
| Breakpoint behaviour | Classes |
|---|---|
| Desktop links: hidden on mobile, flex from md | hidden md:flex |
| Menu button: visible on mobile only | md:hidden |
| Mobile panel: toggled by JS, never on desktop | hidden (+ JS) and md:hidden |
A centred-logo variation
Three equal columns with grid grid-cols-3 put the logo exactly in the middle, regardless of how wide the side groups are.
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="bg-amber-50">
<nav class="grid grid-cols-3 items-center border-b border-amber-200 px-6 py-4 text-sm">
<div class="flex gap-4 text-amber-900"><a href="#">Menu</a><a href="#">About</a></div>
<a href="#" class="justify-self-center font-serif text-2xl font-bold text-amber-900">Café ☕</a>
<a href="#" class="justify-self-end rounded-full bg-amber-900 px-4 py-1.5 text-amber-50">Book a table</a>
</nav>
</body>
</html>Test yourself
Which classes show desktop links only from the md breakpoint?
md:hiddenhidden md:flexflex md:hiddensm:hidden md:block
What does sticky top-0 do on a navbar?
- Fixes it at the bottom
- Keeps it pinned to the top once you scroll past it
- Makes it unclickable
- Removes it from the page flow entirely
Which attribute should a menu toggle button update so assistive technology knows whether the menu is open?
aria-hiddenaria-expandedroletabindex