Today we will learn how to make Responsive Sidebar | side navigation menu using HTML and CSS . this is a very quick tutorial so that you will easily understand and implement this sidebar in your project .
If you want to see the Like demo of the sidebar you can easily visit to this link : Click here
Step 1) Add HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sidebar</title>
<link rel="stylesheet" href="style.css" />
<link rel="stylesheet" href="https://unpkg.com/boxicons@latest/css/boxicons.min.css">
</head>
<body>
<div class="sidebar">
<div class="logo-details">
<i class='bx bx-alarm-snooze'></i>
<div class="logo_name">ziontutorial</div>
<i class='bx bx-menu' id="btn" ></i>
</div>
<ul class="nav-list">
<li>
<i class='bx bx-search'></i>
<input type="text" placeholder="Search..."/>
<span class="tooltip">Search</span>
</li>
<li>
<a href="#">
<i class='bx bxs-grid-alt'></i>
<span class="link_name">Dashboard</span>
</a>
<span class="tooltip">Dashboard</span>
</li>
<!-- second menu -->
<li>
<a href="#">
<i class='bx bx-user'></i>
<span class="link_name">User</span>
</a>
<span class="tooltip">User</span>
</li>
<!-- third menu -->
<li>
<a href="#">
<i class='bx bxs-folder' ></i>
<span class="link_name">File Manager</span>
</a>
<span class="tooltip">File Manager</span>
</li>
<!-- fourth menu -->
<li>
<a href="#">
<i class='bx bx-pie-chart-alt' ></i>
<span class="link_name">Analytics</span>
</a>
<span class="tooltip">Analytics</span>
</li>
<!-- fivth menu -->
<li>
<a href="#">
<i class='bx bx-pie-chart-alt' ></i>
<span class="link_name">Setting</span>
</a>
<span class="tooltip">Setting</span>
</li>
<!-- Profile -->
<li class="profile">
<div class="profile-details">
<img src="Icon.png" alt="profileImg" />
<div class="name_job">
<div class="name">Koray Okumus</div>
<div class="job">Web designer</div>
</div>
</div>
<i class="bx bx-log-out" id="log_out" ></i>
</li>
</ul>
</div>
<!-- main area -->
<section class="home-section">
<div class="text">Dashboard</div>
</section>
<script src="script.js"></script>
</body>
</html>
Step 2): Add CSS:
Now lets add CSS for styling part so that we can see beautiful output of the sidebar . Here is the code for sidebar CSS.
/* Google Font Link */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@100..900&display=swap');
*{
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Inter", sans-serif;
}
.sidebar{
position: fixed;
left: 0;
top: 0;
height: 100%;
width: 78px;
background: #101727;
padding: 6px 14px;
z-index: 99;
transition: all 0.5s ease;
}
.sidebar.open{
width: 250px;
}
.sidebar .logo-details{
height: 60px;
display: flex;
align-items: center;
position: relative;
}
.sidebar .logo-details .icon{
opacity: 0;
transition: all 0.5s ease;
}
.sidebar .logo-details .logo_name{
color: #fff;
font-size: 20px;
font-weight: 600;
opacity: 0;
transition: all 0.5s ease;
}
.sidebar.open .logo-details .icon,
.sidebar.open .logo-details .logo_name{
opacity: 1;
}
.sidebar .logo-details #btn{
position: absolute;
top: 50%;
right: 0;
transform: translateY(-50%);
font-size: 22px;
transition: all 0.4s ease;
font-size: 23px;
text-align: center;
cursor: pointer;
transition: all 0.5s ease;
}
.sidebar.open .logo-details #btn {
text-align: right;
}
.sidebar i {
color: #fff;
height: 60px;
min-width: 50px;
font-size: 28px;
text-align: center;
line-height: 60px;
}
.sidebar .nav-list {
margin-top: 20px;
height: 100%;
}
.sidebar li {
position: relative;
margin: 8px 0;
list-style: none;
}
.sidebar li .tooltip {
position: absolute;
top: -20px;
left: calc(100% + 15px);
z-index: 3;
background: #fff;
box-shadow: 0 5px 10px #101727;
padding: 6px 12px;
border-radius: 4px;
font-size: 15px;
font-weight: 400;
opacity: 0;
white-space: nowrap;
pointer-events: none;
transition: 0s;
}
.sidebar li:hover .tooltip {
opacity: 1;
pointer-events: auto;
transition: all 0.4s ease;
top: 50%;
transform: translateY(-50%);
}
.sidebar.open li .tooltip {
display: none;
}
.sidebar input {
font-size: 15px;
color: #fff;
font-weight: 400;
outline: none;
height: 50px;
width: 100%;
width: 50px;
border: none;
border-radius: 12px;
transition: all 0.5s ease;
background: #344154;
}
.sidebar.open input {
padding: 0 20px 0 50px;
width: 100%;
}
.sidebar .bx-search {
position: absolute;
top: 50%;
left: 0;
transform: translateY(-50%);
font-size: 22px;
background: #344154;
color: #fff;
}
.sidebar.open .bx-search:hover{
background: #344154;
color: #fff;
}
.sidebar .bx-search:hover{
background: #fff;
color: #344154;
}
.sidebar li i {
height: 50px;
line-height: 50px;
font-size: 18px;
border-radius: 12px;
}
.sidebar li a {
display: flex;
height: 100%;
width: 100%;
border-radius: 12px;
align-items: center;
text-decoration: none;
transition: all 0.4 ease;
background: #101727;
}
.sidebar li a:hover {
background: #fff;
}
.sidebar li a .link_name {
color: #fff;
font-size: 15px;
font-weight: 400;
white-space: nowrap;
opacity: 0;
pointer-events: none;
transition: 0.4s;
}
.sidebar.open li a .link_name {
opacity: 1;
pointer-events: auto;
}
.sidebar li a:hover .link_name ,
.sidebar li a:hover i {
transition: all 0.5s ease;
color: #11101d;
}
/* fix this issue */
.sidebar li.profile {
position: fixed;
height: 60px;
width: 78px;
left: 0;
bottom: -8px;
padding: 10px 14px;
background: #1d1b31;
transition: all 0.5s ease;
overflow: hidden;
}
.sidebar.open li.profile {
width: 250px;
}
.sidebar li .profile-details {
display: flex;
align-items: center;
flex-wrap: nowrap;
}
.sidebar li img {
height: 45px;
width: 45px;
object-fit: cover;
border-radius: 6px;
margin-right: 10px;
}
.sidebar li.profile .name,
.sidebar li.profile .job {
font-size: 15px;
font-weight: 400;
color: #fff;
white-space: nowrap;
}
.sidebar li.profile .job{
font-size: 12px;
}
.sidebar .profile #log_out {
position: absolute;
top: 50%;
right: 0;
transform: translateY(-50%);
background: #1d1b31;
width: 100%;
height: 60px;
line-height: 60px;
border-radius: 0px;
transition: all 0.5s ease;
}
.sidebar.open .profile #log_out {
width: 50px;
background: none;
}
.home-section
{
position: relative;
background: #E4E9F7;
min-height: 100vh;
top: 0;
left: 78px;
width: calc(100% - 78px);
transition: all 0.5s ease;
z-index: 2;
}
.sidebar.open ~ .home-section{
left: 250px;
width: calc(100% - 250px);
}
.home-section .text {
display: inline-block;
color: #11101d;
font-size: 25px;
font-weight: 500;
margin: 18px;
}
@media (max-width:420px) {
.sidebar li .tooltip {
display: none;
}
}
Step 3) Add JavaScript
Now lets add some JavaScript for interactivity to the sidebar . Here we know that when we click on menu that will open the sidebar and again when we we click on menu it will close the sidebar .
let sidebar = document.querySelector(".sidebar")
let closeBtn = document.querySelector("#btn")
closeBtn.addEventListener("click", ()=>{
sidebar.classList.toggle("open");
menuBtnChange();
})
function menuBtnChange() {
if(sidebar.classList.contains("open")){
closeBtn.classList.replace("bx-menu","bx-menu-alt-right");
}
else{
closeBtn.classList.replace("bx-menu-alt-right","bx-menu");
}
}