Create A Responsive Sidebar Menu Using Angular

This article explains how to create a responsive sidebar menu in angular.

Step 1: Let’s Create a New Angular Project.

ng new demo

Step 2: Write the below code in app.component.html

<button class="sidebar-open btnsidebar" (click)="sidebarShow = !sidebarShow">
  Open Sidebar
</button>
<div class="sidebar-slider" [class.sidebar-slide-in]="sidebarShow">
  <button class="sidebar-close" (click)="sidebarShow = !sidebarShow">x</button>
  <div class="sidebar-content">
      <h2 class="set-align"></h2>
      <div class="set-sidebar-text">Dashbord</div>
      <div class="set-sidebar-text">Home</div>
      <div class="set-sidebar-text">Profile</div>
      <div class="set-sidebar-text">Message</div>
      <div class="set-sidebar-text">Help</div>
      <div class="set-sidebar-text">Setting</div>
      <div class="set-sidebar-text">Sign Out</div>
  </div>
</div>

Step 3: Write the below code in app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  public sidebarShow: boolean = false;
}

Step 4: Add the below CSS in your app.component.css

* {
    font-family: 'Gill Sans', 'Gill Sans MT', 'Trebuchet MS', sans-serif;
    box-sizing: border-box;
    font-size: 16px;
}

.sidebar-open{
    background-color: #080808;
    border: none;
    color: white;
    padding: 16px 32px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
    margin: 4px 2px;
    transition-duration: 0.4s;
    cursor: pointer;
}

.btnsidebar{
    background-color: black;
    color: white;
    border: 1px solid #555555; 
    border-radius: 10px;
}

.btnsidebar:hover{
    box-shadow: 2px 2px 5px gray;
} 

.sidebar-slider {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    background-color: black;
    transform: translateX(-100%);
    transition: ease-in-out 300ms transform;
    width: 300px;
}

.sidebar-slider.sidebar-slide-in {
    transform: translateX(0%);
    transition: ease-in-out 600ms transform;
}

.sidebar-close {
    position: fixed;
    top: 0;
    right: 0;
    color: black;
    cursor: pointer;
    background-color: white;
    border: none;
    margin: 7px;
    border-radius: 3px;
    padding-left: 10px;
    padding-right: 10px;
    font-size: 21px;
    font-weight: 400;
}

.sidebar-content {
    display: flex;
    flex-direction: column;
    color: white;
    font-size: 16px;
    padding: 10px;
    height: 100%;
    margin-top:66px;
    
}

.set-align{
    text-align: center;
}

.set-sidebar-text{
    padding: 11px;
}

.set-sidebar-text:hover{
    background-image: linear-gradient(to right, rgb(150, 218, 250),black);
    color: black;
    border-left: 10px solid rgb(3, 172, 250);
    cursor: pointer;
}

Step 5: Run the application

ng serve

Submit a Comment

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

Subscribe

Select Categories