A responsive navbar is crucial for modern websites. It provides users with smooth navigation on both mobile and desktop screens. In this guide, you'll learn how to build a clean and mobile-friendly navigation bar using Tailwind CSS – the utility-first framework developers love.
Live Navbar Preview
🛠️ Step 1: Add Tailwind CSS
To get started, include Tailwind CSS in your project. For quick setups or testing, use the CDN below by placing it inside your HTML <head>
tag:
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
Step 2: HTML + Tailwind Code
Here’s the full HTML structure for your responsive navbar using Tailwind CSS:
<nav class="bg-white border-b border-gray-200 fixed top-0 w-full z-50 shadow-sm">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<div class="flex justify-between h-16">
<div class="text-xl font-bold text-blue-600">MyWebsite</div>
<div class="hidden sm:flex space-x-6">
<a href="#" class="text-gray-700 hover:text-blue-600">Home</a>
<a href="#" class="text-gray-700 hover:text-blue-600">Features</a>
<a href="#" class="text-gray-700 hover:text-blue-600">Pricing</a>
<a href="#" class="text-gray-700 hover:text-blue-600">Contact</a>
</div>
<div class="sm:hidden">
<button id="menu-btn" class="text-blue-600 font-bold">☰ Menu</button>
</div>
</div>
</div>
<div id="mobile-menu" class="sm:hidden hidden px-4 pb-4 space-y-2">
<a href="#" class="block text-gray-700 hover:text-blue-600">Home</a>
<a href="#" class="block text-gray-700 hover:text-blue-600">Features</a>
<a href="#" class="block text-gray-700 hover:text-blue-600">Pricing</a>
<a href="#" class="block text-gray-700 hover:text-blue-600">Contact</a>
</div>
</nav>
Step 3: Make It Work on Mobile
Add this simple JavaScript at the end of your HTML file to toggle the mobile menu visibility:
<script>
const btn = document.getElementById('menu-btn');
const menu = document.getElementById('mobile-menu');
btn.addEventListener('click', () => {
menu.classList.toggle('hidden');
});
</script>
Optional Customizations
- Replace the brand text with your company name
- Switch color themes using Tailwind’s color utilities
- Add scroll or sticky effects to the navbar
- Use
dark:
classes to support dark mode
Final Thoughts
With Tailwind CSS, you can create powerful UI components like navbars quickly. This responsive navbar can be a strong starting point for your homepage, dashboard, or portfolio. Keep experimenting with layouts and colors to match your project’s identity.
Want more developer-friendly components like this? Visit GetCodeUI.com to speed up your UI development