A navbar is one of the first components most projects need, and it touches almost everything you've learned so far: flexbox, spacing, position, and responsive design. This lesson builds a complete, responsive navbar.
What Does a Tailwind Navbar Need?
A functional navbar needs a flex container to align a logo and navigation links, responsive utilities to hide the full link list on mobile in favor of a toggleable menu, and often sticky or fixed positioning to stay visible while scrolling.
Structurally, a navbar is simply nav + flex items-center justify-between, everything else builds on that same foundational pattern from the Flexbox and Justify Content lessons.
flex items-center justify-between aligns the logo and links across the row.
hidden md:flex shows full navigation only at the md breakpoint and above.
md:hidden shows the mobile menu button only below the md breakpoint.
sticky top-0 or fixed top-0 keeps the navbar visible while the page scrolls.
Navbar Cheatsheet
Common navbar utility combinations.
Purpose
Classes
Notes
Navbar container
flex items-center justify-between px-6 py-4
Base layout for logo and links
Sticky navbar
sticky top-0 z-30 bg-white
Stays visible while scrolling
Desktop links
hidden gap-6 md:flex
Hidden on mobile, visible from md up
Mobile toggle button
md:hidden
Only visible below md breakpoint
Active link
text-blue-600 font-semibold
Highlights the current page
Mobile menu panel
absolute inset-x-0 top-full
Dropdown panel below the navbar
Shadow on scroll
shadow-sm (toggled by JS)
Adds elevation once page scrolls
CTA button in nav
rounded-lg bg-blue-600 px-4 py-2 text-white
Prominent call-to-action
Building a Mobile Menu Toggle
On small screens, replace the full link list with a single toggle button that reveals a menu panel, typically absolutely positioned below the navbar, when clicked. This requires a small amount of JavaScript to toggle a hidden class.
On desktop, md:static md:flex overrides the mobile absolute/hidden positioning so the same markup works at both sizes.
Sticky Navbar With a Scroll Shadow
A common enhancement adds a subtle shadow to the navbar only after the page has scrolled a small amount, giving a sense of depth without a heavy border showing at the very top of the page.
Framework routers (Astro, Next.js, React Router) typically expose the current path, letting you conditionally apply an active class to whichever navigation link matches the current page.
Navigation should always live inside a <nav> element, and mobile menu toggles need proper ARIA attributes so screen reader and keyboard users understand the menu's current open/closed state.
Wrap navigation links in a <nav> element with an appropriate aria-label if there are multiple navs on the page.
Use aria-expanded and aria-controls on the mobile menu toggle button.
Ensure the mobile menu is reachable and closeable using only the keyboard.
Common Mistakes
Building the mobile menu toggle without aria-expanded, leaving screen reader users unaware of its open/closed state.
Forgetting z-* on a sticky navbar, causing dropdown menus or hero content to render above it unexpectedly.
Not testing the mobile menu with keyboard-only navigation (Tab, Enter, Escape).
Using fixed instead of sticky without adding matching top padding elsewhere, causing content to hide behind the fixed navbar.
Hardcoding the "active" link state instead of deriving it from the current route.
Key Takeaways
A navbar is built from the same flexbox and responsive utilities covered earlier in this course.
Mobile menus typically toggle a hidden class via a small JavaScript event listener.
Sticky navbars need a z-index high enough to stay above other page content.
Active link styling should be derived dynamically from the current route, not hardcoded.
Accessible navbars require aria-expanded, aria-controls, and full keyboard operability for their mobile toggle.
Pro Tip
Reuse the exact same mobile-menu markup and classes across every page by extracting the navbar into its own component, that way accessibility and responsive fixes only need to happen in one place.
You now understand how to build a responsive, accessible navbar. Next, learn how to build a modal dialog with Tailwind CSS.