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

Noura MostafaNoura Mostafa
2 min read

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 above

  • The 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 or Transition from Headless UI

  • Add 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.

10
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."