In this post, I'm going to share simple method to create Hamburger Menu in ReactJS. Using this method you can create any type of Hamburger Menu you want.
If you want to see whole tutorial of creating Hamburger Menu with all the functionalities like React-Router with awesome radical gradient background effect then you can follow the video below.
First of all, create your react app using:
npx create-react-app HamburgerMenu
Install styled-components dependency:
npm install styled-components
Now if you want to create different file for your Hamburger Menu then you can, here I'm writing everything in the app.js file for this tutorial.
We will start by importing styled components.
import styled from "styled-components";
Let's create one fixed rounded menu first. Name it as MenuLabel and create it using styled-components.
import React,{useState} from "react";
import "./styles.css";
import styled from "styled-components";


const MenuLabel = styled.label`
  background-color: #B6EDC8;
  position: fixed;
  top: 6rem;
  right: 6rem;
  border-radius: 50%;
  height: 7rem;
  width: 7rem;
  cursor: pointer;
  z-index: 1000;
  box-shadow: 0 1rem 3rem rgba(182, 237, 200, 0.3);
  text-align: center;
`;

export default function App() {

  return (
    <>
        <MenuLabel htmlFor="navi-toggle">
        Menu
      </MenuLabel>
    </>
  );
}
In the above code, we have created on rounded Menu using styled-component at line no 6 which is label tag in html.
Output of this will look something like this:
Now we will create icon above this menu.
Remove Menu written inside of MenuLabel and create new component Icon as below:
<MenuLabel htmlFor="navi-toggle">
   <Icon>&nbsp;</Icon>
</MenuLabel>
Let's write CSS for this Icon component, which will be an span element.
const Icon = styled.span`
  position: relative;
  background-color: black;
  width: 3rem;
  height: 2px;
  display: inline-block;
  margin-top: 3.5rem;

  &::before,
  &::after {
    content: "";
    background-color: black;
    width: 3rem;
    height: 2px;
    display: inline-block;
    position: absolute;
    left: 0;
  }
  &::before {
    top: -0.8rem;
  }
  &::after {
    top: 0.8rem;
  }
`;
By only using width and height properties properly we can create simple horizontal lines.
We have copied our main line through before and after pseudo classes, and displayed one above the original and one below.
You can set separate width and height for all these three lines.
Now what we need is to create X with these 3 lines whenever someone clicks on it and to do that we have to: