beginner#react#navigation bar#responsive design
Creating a Responsive Navigation Bar with React
Learn how to create a responsive navigation bar using React, including mobile and desktop versions.
Introduction to Responsive Navigation Bar
In this tutorial, we will build a responsive navigation bar using React. This navigation bar will have different versions for mobile and desktop devices.
Step 1: Setting up the Project
First, we need to set up a new React project. We can do this by running the following command in our terminal:
npx create-react-app navigation-bar-app
Step 2: Creating the Navigation Bar Component
Next, we need to create a new component for our navigation bar. We can do this by creating a new file called NavigationBar.js in the src directory:
import React from 'react';
function NavigationBar() {
return (
<nav>
<ul>
<li><a href='#'>Home</a></li>
<li><a href='#'>About</a></li>
<li><a href='#'>Contact</a></li>
</ul>
</nav>
);
}
export default NavigationBar;
Step 3: Adding Media Queries for Responsiveness
We can add media queries to our CSS file to make our navigation bar responsive:
nav ul {
list-style: none;
margin: 0;
padding: 0;
display: flex;
justify-content: space-between;
}
nav li {
margin-right: 20px;
}
@media only screen and (max-width: 768px) {
nav ul {
flex-direction: column;
}
nav li {
margin-right: 0;
margin-bottom: 10px;
}
}