โ๏ธ How to Build a Responsive Navbar in React with Tailwind CSS

Navigation bars are an essential part of any website. In this tutorial, weโll walk through how to create a responsive navbar in React using Tailwind CSS. It will include a mobile-friendly menu with a toggle, and smooth styling that adapts to screen size.
๐งฑ Why Tailwind CSS?
Tailwind CSS is a utility-first CSS framework that helps you build modern interfaces without writing custom CSS. It's fast, flexible, and pairs beautifully with React.
๐ ๏ธ Project Setup
If you donโt already have a React + Tailwind project, run:
bashCopyEditnpx create-react-app responsive-navbar
cd responsive-navbar
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
Update your tailwind.config.js
and index.css
to enable Tailwind.
๐ง Basic Navbar Structure
Letโs start by building a simple structure inside a Navbar.jsx
component:
jsxCopyEditimport React, { useState } from "react";
const Navbar = () => {
const [isOpen, setIsOpen] = useState(false);
return (
<nav className="bg-white shadow-md fixed w-full top-0 left-0 z-10">
<div className="max-w-7xl mx-auto px-4 flex items-center justify-between h-16">
<h1 className="text-xl font-bold text-blue-600">MySite</h1>
<div className="hidden md:flex space-x-6">
<a href="#" className="text-gray-700 hover:text-blue-600">Home</a>
<a href="#" className="text-gray-700 hover:text-blue-600">About</a>
<a href="#" className="text-gray-700 hover:text-blue-600">Contact</a>
</div>
<button onClick={() => setIsOpen(!isOpen)} className="md:hidden">
<svg className="w-6 h-6 text-gray-700" fill="none" viewBox="0 0 24 24" stroke="currentColor">
{isOpen ? (
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
) : (
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 6h16M4 12h16M4 18h16" />
)}
</svg>
</button>
</div>
{/* Mobile Menu */}
{isOpen && (
<div className="md:hidden px-4 pb-4 space-y-2">
<a href="#" className="block text-gray-700 hover:text-blue-600">Home</a>
<a href="#" className="block text-gray-700 hover:text-blue-600">About</a>
<a href="#" className="block text-gray-700 hover:text-blue-600">Contact</a>
</div>
)}
</nav>
);
};
export default Navbar;
๐ฑ Making It Responsive
The desktop menu appears on
md
screens and aboveThe mobile menu toggles with a hamburger icon
Tailwind utility classes handle spacing, hover states, and layout
๐ Optional Enhancements
Add animation on toggle using
framer-motion
orTransition
from Headless UIAdd dark mode support
Use React Router for navigation
โ Final Thoughts
React and Tailwind make building responsive interfaces a breeze. This navbar is clean, mobile-first, and ready to integrate into any project.
๐ Pro Tip: Reuse this component in all your pages by placing it in a components
folder and importing it in App.js
.
Subscribe to my newsletter
Read articles from Noura Mostafa directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Noura Mostafa
Noura Mostafa
๐ Aspiring Full-Stack Developer Blogger ๐จโ๐ป Passionate about web development and coding. โ๏ธ Sharing my journey through tech via CodeOdyssey ๐ "The code is a journey, not a destination."