Create a Responsive Navbar using ReactJS
Last Updated :
04 Apr, 2025
Creating a responsive navbar in ReactJS is essential for providing an optimized user experience across various screen sizes. A responsive navbar adapts its layout based on the screen size, ensuring easy navigation on both mobile and desktop devices.
In this article, we will walk you through the steps to build a responsive navbar in ReactJS.
Preview of Final Output
Let us have a look at how the final application will look like:

Prerequisites
Approach
To create a responsive navbar in React, we will:
- use the react-router-dom for defining the routes.
- design the navbar using the styled-components library.
- define the pages using JSX
- link the pages to respective routes or we address with the help to Route component.
Steps to create Responsive Navbar
Step 1: Create a New React Project
If you don’t have a React project set up yet, create one using the following command:
npx create-react-app responsive-navbar
cd responsive-navbar
Step 2: Install Required Dependencies
Install the necessary dependencies for routing and styling by running the following command:
npm i react-router-dom react-icons styled-components@5.3.10
Step 3: Create the Components Folder
Inside the src folder, create a components folder. Then, navigate to the components folder and create another folder named Navbar. Inside the Navbar folder, create two files: index.js and NavbarElements.js.
Step 4: Define Required Page Components
Create a pages folder inside src and define the following page components: about.js, annual.js, blogs.js, events.js, index.js, signup.js, team.js.
Project Structure of Responsive Navbar
The file structure in the project will look like this:

Dependencies list after installing packages:
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-icons": "^4.11.0",
"react-router-dom": "^6.16.0",
"react-scripts": "5.0.1",
"styled-components": "^5.3.10",
"web-vitals": "^2.1.4"
}
JavaScript
// index.js
import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import App from "./App";
const root = ReactDOM.createRoot(
document.getElementById("root")
);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
JavaScript
// filename -App.js
import React from "react";
import "./App.css";
import Navbar from "./components/Navbar";
import {
BrowserRouter as Router,
Routes,
Route,
} from "react-router-dom";
import Home from "./pages";
import About from "./pages/about";
import Events from "./pages/events";
import AnnualReport from "./pages/annual";
import Teams from "./pages/team";
import Blogs from "./pages/blogs";
import SignUp from "./pages/signup";
function App() {
return (
<Router>
<Navbar />
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route
path="/events"
element={<Events />}
/>
<Route
path="/annual"
element={<AnnualReport />}
/>
<Route path="/team" element={<Teams />} />
<Route path="/blogs" element={<Blogs />} />
<Route
path="/sign-up"
element={<SignUp />}
/>
</Routes>
</Router>
);
}
export default App;
JavaScript
// components/Navbar/index.js
import React from "react";
import {
Nav,
NavLink,
Bars,
NavMenu,
NavBtn,
NavBtnLink,
} from "./NavbarElements";
const Navbar = () => {
return (
<>
<Nav>
<Bars />
<NavMenu>
<NavLink to="/about" >
About
</NavLink>
<NavLink to="/events" activeStyle>
Events
</NavLink>
<NavLink to="/annual" activeStyle>
Annual Report
</NavLink>
<NavLink to="/team" activeStyle>
Teams
</NavLink>
<NavLink to="/blogs" activeStyle>
Blogs
</NavLink>
<NavLink to="/sign-up" activeStyle>
Sign Up
</NavLink>
{/* Second Nav */}
{/* <NavBtnLink to='/sign-in'>Sign In</NavBtnLink> */}
</NavMenu>
<NavBtn>
<NavBtnLink to="/signin">
Sign In
</NavBtnLink>
</NavBtn>
</Nav>
</>
);
};
export default Navbar;
JavaScript
// components/Navbar/navbarElements.js
import { FaBars } from "react-icons/fa";
import { NavLink as Link } from "react-router-dom";
import styled from "styled-components";
export const Nav = styled.nav`
background: #63d471;
height: 85px;
display: flex;
justify-content: space-between;
padding: 0.2rem calc((100vw - 1000px) / 2);
z-index: 12;
/* Third Nav */
/* justify-content: flex-start; */
`;
export const NavLink = styled(Link)`
color: #808080;
display: flex;
align-items: center;
text-decoration: none;
padding: 0 1rem;
height: 100%;
cursor: pointer;
&.active {
color: #000000;
}
`;
export const Bars = styled(FaBars)`
display: none;
color: #808080;
@media screen and (max-width: 768px) {
display: block;
position: absolute;
top: 0;
right: 0;
transform: translate(-100%, 75%);
font-size: 1.8rem;
cursor: pointer;
}
`;
export const NavMenu = styled.div`
display: flex;
align-items: center;
margin-right: -24px;
/* Second Nav */
/* margin-right: 24px; */
/* Third Nav */
/* width: 100vw;
white-space: nowrap; */
@media screen and (max-width: 768px) {
display: none;
}
`;
export const NavBtn = styled.nav`
display: flex;
align-items: center;
margin-right: 24px;
/* Third Nav */
/* justify-content: flex-end;
width: 100vw; */
@media screen and (max-width: 768px) {
display: none;
}
`;
export const NavBtnLink = styled(Link)`
border-radius: 4px;
background: #808080;
padding: 10px 22px;
color: #000000;
outline: none;
border: none;
cursor: pointer;
transition: all 0.2s ease-in-out;
text-decoration: none;
/* Second Nav */
margin-left: 24px;
&:hover {
transition: all 0.2s ease-in-out;
background: #fff;
color: #808080;
}
`;
JavaScript
// pages/about.js
import React from "react";
const About = () => {
return (
<div
style={{
display: "flex",
justifyContent: "centre",
alignItems: "centre",
height: "100vh",
}}
>
<h1>
GeeksforGeeks is a Computer Science portal
for geeks.
</h1>
</div>
);
};
export default About;
JavaScript
// pages/annual.js
import React from "react";
const AnnualReport = () => {
return (
<div
style={{
display: "flex",
justifyContent: "centre",
alignItems: "centre",
height: "100vh",
}}
>
<h1>Annual Report</h1>
</div>
);
};
export default AnnualReport;
JavaScript
// pages/blogs.js
import React from "react";
const Blogs = () => {
return (
<div
style={{
display: "flex",
justifyContent: "centre",
alignItems: "centre",
height: "100vh",
}}
>
<h1>Welcome to GeeksforGeeks Blogs</h1>
</div>
);
};
export default Blogs;
JavaScript
// pages/events.js
import React from "react";
const Events = () => {
return (
<div
style={{
display: "flex",
justifyContent: "centre",
alignItems: "centre",
height: "100vh",
}}
>
<h1>Welcome to GeeksforGeeks Events</h1>
</div>
);
};
export default Events;
JavaScript
// pages/index.js
import React from 'react';
const Home = () => {
return (
<div
style={{
display: 'flex',
justifyContent: 'centre',
alignItems: 'centre',
height: '100vh'
}}
>
<h1>Welcome to GeeksforGeeks</h1>
</div>
);
};
export default Home;
JavaScript
// pages/signup.js
import React from "react";
const SignUp = () => {
return (
<div
style={{
display: "flex",
justifyContent: "centre",
alignItems: "centre",
height: "100vh",
}}
>
<h1>Sign Up</h1>
</div>
);
};
export default SignUp;
JavaScript
// pages/team.js
import React from "react";
const Teams = () => {
return (
<div
style={{
display: "flex",
justifyContent: "centre",
alignItems: "centre",
height: "100vh",
}}
>
<h1>Welcome to GeeksforGeeks Team</h1>
</div>
);
};
export default Teams;
Output
Similar Reads
JavaScript Tutorial
JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. JavaScript is an interpreted language that executes code line by line providing more flexibility. HTML adds Structure to a web page, CSS st
11 min read
Web Development
Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers
React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook created React. Developers with a Javascript background can easily develop web applications
15+ min read
JavaScript Interview Questions and Answers
JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. It is essential for both front-end and back-end developers to have a strong command of
15+ min read
HTML Tutorial
HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript
10 min read
AVL Tree Data Structure
An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of
4 min read
React Tutorial
React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable. Applications are built using reusable compo
8 min read
Sliding Window Technique
Sliding Window problems involve moving a fixed or variable-size window through a data structure, typically an array or string, to solve problems efficiently based on continuous subsets of elements. This technique is used when we need to find subarrays or substrings according to a given set of condit
15+ min read
Decorators in Python
In Python, decorators are a powerful and flexible way to modify or extend the behavior of functions or methods, without changing their actual code. A decorator is essentially a function that takes another function as an argument and returns a new function with enhanced functionality. Decorators are
10 min read
What is a Neural Network?
Neural networks are machine learning models that mimic the complex functions of the human brain. These models consist of interconnected nodes or neurons that process data, learn patterns, and enable tasks such as pattern recognition and decision-making. In this article, we will explore the fundament
14 min read