Creating a Responsive Navbar in React

Creating a responsive navbar in React is a great way to enhance your web application’s user experience. Follow this step-by-step guide to build a simple yet effective navbar using React.

Step 1: Set Up Your React Application

First, ensure you have Node.js and npm installed. Then, create a new React app:

npx create-react-app my-navbar
cd my-navbar

Step 2: Create Navbar Component

Inside the src folder, create a new folder named components, and within it, a file named Navbar.js.

// src/components/Navbar.js

import React, { useState } from 'react';
import './Navbar.css';

const Navbar = () => {
  const [isOpen, setIsOpen] = useState(false);

  const toggleMenu = () => {
    setIsOpen(!isOpen);
  };

  return (
    <nav className="navbar">
      <div className="brand-title">MyApp</div>
      <a href="#" className="toggle-button" onClick={toggleMenu}>
        <span className="bar"></span>
        <span className="bar"></span>
        <span className="bar"></span>
      </a>
      <div className={`navbar-links ${isOpen ? 'active' : ''}`}>
        <ul>
          <li><a href="#home">Home</a></li>
          <li><a href="#about">About</a></li>
          <li><a href="#services">Services</a></li>
          <li><a href="#contact">Contact</a></li>
        </ul>
      </div>
    </nav>
  );
};

export default Navbar;

Step 3: Style Your Navbar

Create a Navbar.css file in the same components directory to style your navbar.

/* src/components/Navbar.css */

.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #333;
  padding: 1rem;
}

.brand-title {
  font-size: 1.5rem;
  color: white;
}

.navbar-links ul {
  list-style: none;
  padding: 0;
  display: flex;
}

.navbar-links li {
  margin: 0 1rem;
}

.navbar-links a {
  color: white;
  text-decoration: none;
}

.toggle-button {
  display: none;
  flex-direction: column;
  cursor: pointer;
}

.toggle-button .bar {
  width: 25px;
  height: 3px;
  background-color: white;
  margin: 4px;
}

@media (max-width: 768px) {
  .toggle-button {
    display: flex;
  }

  .navbar-links {
    display: none;
    width: 100%;
  }

  .navbar-links.active {
    display: flex;
    flex-direction: column;
  }

  .navbar-links ul {
    width: 100%;
    flex-direction: column;
  }

  .navbar-links li {
    text-align: center;
    margin: 1rem 0;
  }
}

Step 4: Integrate Navbar into App

Import and use the Navbar component in your App.js.

// src/App.js

import React from 'react';
import Navbar from './components/Navbar';

function App() {
  return (
    <div className="App">
      <Navbar />
      {/* Other components */}
    </div>
  );
}

export default App;

Step 5: Test Your Navbar

Run your application to see the responsive navbar in action:

npm start

Conclusion

With these steps, you’ve created a responsive navbar using React. Customize the design and functionality further to fit your project’s needs. Happy coding!

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *