Creating a Sidebar with React and CSS
Introduction
In this tutorial, we will learn how to create a sidebar using React and style it using CSS. Sidebars are a common feature in web design, providing a way to navigate through different sections of a website or display additional information.
Setting Up the Project
First, make sure you have Node.js and npm installed on your computer. Then, create a new React project using the following command:
npx create-react-app sidebar-demo
This will create a new directory with all the necessary files for a React project.
Creating the Sidebar Component
Inside the src folder of your project, create a new file called Sidebar.js. This file will contain the code for our sidebar component.
import React from 'react';
import './Sidebar.css';
const Sidebar = () => {
return (
- Home
- About
- Services
- Contact
);
}
export default Sidebar;
Styling the Sidebar
Create a new file called Sidebar.css in the same directory as Sidebar.js. This file will contain the CSS styles for our sidebar.
.sidebar {
width: 200px;
height: 100%;
background-color: #333;
color: #fff;
padding: 20px;
}
.sidebar ul {
list-style: none;
padding: 0;
margin: 0;
}
.sidebar li {
margin-bottom: 10px;
}
Using the Sidebar Component
Finally, we can use the Sidebar component in our main App component. Open the App.js file and add the following code:
import React from 'react';
import Sidebar from './Sidebar';
import './App.css';
function App() {
return (
{/* Main content goes here */}
);
}
export default App;