Hey, developers welcome to Day 53 of our 90Days 90Projects challenge. And in Day 53 we are going to Create Sidebar Menu using HTML CSS & JavaScript.
So to run this code you just need to copy the HTML and CSS code and run it into your code Editor.
Preview
Image: Download Now
HTML Code
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> Sidebar Menu </title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css" integrity="sha512-MV7K8+y+gLIBoVD59lQIYicR65iaqukzvf/nwasF0nqhPay5w/9lJmVM2hMDcnK1OnMGCdVK+iQrJ7lzPJQd1w==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="main-container">
<div class="left-menu">
<div class="logo">
<img src="./mainLogo.png" alt="">
<span class="logoLink"><a href="">Geeks Help</a></span>
</div>
<li class="menu"><i class="fa-sharp fa-solid fa-circle-chevron-down"></i></li>
<ul>
<li class="sidebar-item"><a class="sidebar-link" href="#"><i class="fa-solid fa-house"></i> Home </a>
</li>
<li class="sidebar-item"><a class="sidebar-link" href="#"><i class="fa-solid fa-user"></i> User </a>
</li>
<li class="sidebar-item"><a class="sidebar-link" href="#"><i class="fa-solid fa-file"></i> Files </a>
</li>
<li class="sidebar-item"><a class="sidebar-link" href="#"><i class="fa-solid fa-store"></i> Saved </a>
</li>
<li class="sidebar-item"><a class="sidebar-link" href="#"><i class="fa-solid fa-calendar-days"></i>
Events </a></li>
<li class="sidebar-item"><a class="sidebar-link" href="#"><i class="fa-solid fa-message"></i> Message
</a></li>
<li class="sidebar-item"><a href="#"><i class="fa-solid fa-chart-line"></i> Analytics </a></li>
<li class="sidebar-item"><a class="sidebar-link" href="#"><i class="fa-solid fa-rocket"></i> Services
</a></li>
<li class="sidebar-item" id="settings"><a id="setting" class="sidebar-link" href="#"><i class="fa-solid fa-gear"></i></a>
<li>
</ul>
</div>
<!-- Right Content Goes here -->
<div class="right-content"></div>
</div>
</body>
<script src="script.js"></script>
</html>
CSS Code
style.css
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300&display=swap');
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.main-container {
width: 100%;
height: 100vh;
z-index: 3000;
background-color: purple;
}
.left-menu {
width: 13rem;
height: 100vh;
display: flex;
position: relative;
background: white;
flex-direction: column;
transition: all .4s ease;
}
.menu {
position: absolute;
list-style: none;
color: #fff;
z-index: 1200;
top: 4%;
right: -20px;
cursor: pointer;
font-size: 25px;
transition: all 0.7s;
transform: rotate(-90deg);
}
.active {
transform: rotate(90deg);
}
.left-menu ul li a {
display: flex;
}
.logo {
position: relative;
width: 60px;
top: 1rem;
left: 0.5rem;
display: flex;
align-items: center;
margin: 10px 0 0 0px;
justify-content: center;
}
.logo img {
width: 100%;
z-index: 100;
position: absolute;
}
.logo span {
padding: 10px;
font-size: 18px;
font-weight: bold;
text-transform: uppercase;
}
.logo a {
position: relative;
color: #7410f0;
text-align: left;
font-size: 18px;
display: table;
left: 180px;
width: 300px;
padding: 10px;
text-decoration: none;
font-family: 'Poppins', 'sans-serif';
}
.left-menu ul {
display: flex;
margin-top: 2rem;
overflow: hidden;
margin-left: 1.5rem;
flex-direction: column;
align-items: flex-start;
}
ul li {
list-style: none;
align-items: center;
margin-bottom: 1rem;
transition: all .3s linear;
font-family: 'Poppins', sans-serif;
}
ul li a {
padding: 0.5rem;
color: #7410f0;
text-decoration: none;
}
ul li a i {
cursor: pointer;
color: #7410f0;
font-size: 1.4rem;
margin-right: 1.2rem;
text-decoration: none;
transition: all .3s linear;
}
.sidebar-item i {
width: 20px;
height: 20px;
text-align: center;
}
#settings {
position: absolute;
bottom: 1rem;
}
.left-menu ul li:hover {
transform: translateX(.4rem);
transition: all .4s linear;
}
.left-menu ul li:hover .fa-solid,
.left-menu ul li:hover a {
font-weight: bold;
color: purple;
}
JavaScript
script.js
const btn = document.querySelector('.menu');
const logo = document.querySelector('.logoLink');
const menu = document.querySelector('.left-menu');
const settings = document.getElementById('setting');
btn.addEventListener('click', function () {
btn.classList.toggle('active');
if (btn.classList.contains('active')) {
menu.style.width = '4.4rem';
logo.style.opacity = '0';
}
else {
menu.style.width = '13rem';
logo.style.opacity = '1';
}
})