Angular Responsive Navigation Menu Using HTML & CSS Only

 Add this in your index.html file for Icons

<script src="https://kit.fontawesome.com/a076d05399.js"></script>

OR

Install Font-Awesome by click below

font-awesome 

Put This Navigation Code Below In Your HTML File

<nav>
    <div class="logo">
        Brand Name
    </div>
    <input type="checkbox" id="click">
    <label for="click" class="menu-button">
        <i class="fas fa-bars"></i>
    </label>
    <ul>
        <li *ngFor="let menu of navMenu" for="click">
           <a routerLink="{{menu.link}}" 
              [ngClass]="{'active-menu': activeMenu === menu.title}" 
              (click)="selectMenu(menu.title)">{{menu.title}}</a>
         </li>
    </ul>
</nav>
<!-- Dummy Div To Show Active Menu
     You can add your pages name -->
<div style="font-size: 30px; text-align: center; margin-top: 20%;">
    Selected item is: {{activeMenu}}
</div>
 

Css Code:

/* You can add global styles to this file, and also import other style files */

@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@500&display=swap');
* {
    font-family'Roboto', sans-serif;
    margin0;
    padding0;
    box-sizing: border-box;
}

nav {
    height80px;
    background-color: #800080;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding0px 50px 0px 50px;
}

nav .menu-button {
    color: white;
    font-size23px;
    cursor: pointer;
    display: none;
}

nav .logo {
    font-size35px;
    color: #fff;
    font-weight600;
    transition: all 0.5s ease-in;
}

nav .logo:hover {
    font-size37px;
    letter-spacing2px;
    transition: all 0.5s ease-out;
}

nav ul {
    display: flex;
    list-style: none;
}

nav ul li {
    margin0;
    padding10px;
}

nav ul li a {
    text-decoration: none;
    font-size17px;
    letter-spacing1px;
    color: #fff;
    transition: all 0.5s ease-in;
}

nav ul li a:hover,
nav ul li a.active-menu {
    border-radius5px;
    background-color: lightgray;
    padding8px 10px;
    color: #000;
    transition: all 0.5s ease-out;
}

#click {
    display: none;
}

@media (max-width:940px) {
    nav .menu-button {
        display: block;
    }
    #click:checked~.menu-button i:before {
        content"\f00d";
    }
    nav ul {
        position: fixed;
        top10%;
        left-100%;
        height100%;
        width100%;
        background-color: #800080;
        display: block;
        text-align: center;
        transition: all 0.3s ease;
    }
    #click:checked~ul {
        left0%;
    }
    nav ui li {
        margin40px 0px;
    }
    nav ui li a {
        font-size20px;
        display: block;
    }
    nav ul li a:hover,
    nav ul li a.active-menu {
        border-radius5px;
        background-color: lightgray;
        padding8px 10px;
        color: #000;
        transition: all 0.5s ease-out;
    }
}

Small TS Code For Active Menu

// Your Component Name
export class YourComponent implements OnInit {
  activeMenu = "Home"
  navMenu:any = [{title:"Home",link:"/"},{title:"Gallery",link:"/"}, {title:'Services',link:"/"},{title:"About US",link:"/"},{title:"Contact US",link:"/abc"}]
  constructor() { }

  ngOnInit(): void {
  }

  selectMenu(title:any){
    this.activeMenu = title
  }
}

Comments