agyanadda@gmail.com

Tailwind Navbar

Building Components 1 min read Updated Dec 1, 2026 Topic 21 of 27

Tailwind has no navbar component, so you build one with Flexbox and responsive classes. The mobile menu needs a few lines of JavaScript.

Desktop bar

  • Wrapper: flex items-center justify-between
  • Links: hidden md:flex gap-6 (hidden on phones)
  • Sticky: sticky top-0 z-50 bg-white/90 backdrop-blur border-b

Mobile menu

  • Hamburger button: md:hidden
  • Menu panel: starts with hidden, and JavaScript toggles that class
  • Update aria-expanded on the button for screen readers
btn.addEventListener('click', () => {
  menu.classList.toggle('hidden');
  btn.setAttribute('aria-expanded', !menu.classList.contains('hidden'));
});

In Laravel projects, Alpine.js (x-data, @click) is a popular way to do this without writing separate JavaScript.

Example

Example — Responsive navbar with mobile menu HTML
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>

<header class="sticky top-0 z-50 bg-white/90 backdrop-blur border-b border-slate-200">
  <nav class="max-w-6xl mx-auto px-4 h-16 flex items-center justify-between">
    <a href="#" class="text-xl font-extrabold text-indigo-600">AgyanAdda</a>
    <div class="hidden md:flex items-center gap-6 text-slate-700">
      <a href="#" class="hover:text-indigo-600">Home</a>
      <a href="#" class="hover:text-indigo-600">Tutorials</a>
      <a href="#" class="hover:text-indigo-600">Exams</a>
      <a href="#" class="bg-indigo-600 hover:bg-indigo-700 text-white px-4 py-2 rounded-lg">Login</a>
    </div>
    <button id="menuBtn" class="md:hidden p-2 rounded-lg hover:bg-slate-100" aria-expanded="false" aria-controls="mobileMenu" aria-label="Open menu">
      <svg class="size-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path stroke-linecap="round" d="M4 6h16M4 12h16M4 18h16"/></svg>
    </button>
  </nav>
  <div id="mobileMenu" class="hidden md:hidden border-t border-slate-200 px-4 py-3 space-y-1">
    <a href="#" class="block px-3 py-2 rounded-lg hover:bg-slate-100">Home</a>
    <a href="#" class="block px-3 py-2 rounded-lg hover:bg-slate-100">Tutorials</a>
    <a href="#" class="block px-3 py-2 rounded-lg hover:bg-slate-100">Exams</a>
    <a href="#" class="block text-center bg-indigo-600 text-white px-3 py-2 rounded-lg">Login</a>
  </div>
</header>
<main class="max-w-6xl mx-auto p-6 text-slate-600">Make the preview narrow and click the menu icon.</main>

<script>
  const btn = document.getElementById('menuBtn');
  const menu = document.getElementById('mobileMenu');
  btn.addEventListener('click', () => {
    menu.classList.toggle('hidden');
    btn.setAttribute('aria-expanded', String(!menu.classList.contains('hidden')));
  });
</script>

Test Yourself With Exercises

Exercise 1 Multiple Choice

Which classes hide desktop links on phones and show them from md up?

Exercise 2 True / False

Tailwind toggles the mobile menu automatically without JavaScript.

Exercise 3 Fill in the Blank

The hamburger button should update the aria-___ attribute when opened.

Frequently Asked Questions

It blurs whatever is behind a semi-transparent element, giving a frosted-glass effect to sticky headers.