I have the following issue with the following code. I have tried to implement a collapsible sidebar menu in my development. I have researched many resources online and I tried to customize it for for myself after following along a Youtube tutorial. However, the collapsing functionally seems to work except at the the time of refreshing the page. I have researched other Stackoverflow solutions (i.e session storage) but I couldn't be able to implement it in my case. Can somebody please help?
Here is my HTML page
let arrow = document.querySelectorAll(".arrow");
for (var i = 0; i < arrow.length; i++) {
arrow[i].addEventListener("click", (e)=>{
let arrowParent = e.target.parentElement.parentElement;
arrowParent.classList.toggle("showMenu");
})
}
let sidebar = document.querySelector(".sidebar");
let sidebarBtn = document.querySelector(".bx-menu");
sidebarBtn.addEventListener("click" , ()=>{
sidebar.classList.toggle("close");
});
*{
box-sizing: border-box;
margin: 0;
padding: 0;
}
/* fonts */
#import url('https://fonts.googleapis.com/css2?family=Cabin&family=Roboto+Condensed:wght#400;700&display=swap');
html {
font-size: 100%;
} /*16px*/
:root {
/* colors */
--primary-50: #e0fcff;
--primary-100: #bef8fd;
--primary-200: #87eaf2;
--primary-300: #54d1db;
--primary-400: #38bec9;
--primary-500: #2cb1bc;
--primary-600: #14919b;
--primary-700: #0e7c86;
--primary-800: #0a6c74;
--primary-900: #044e54;
/* grey */
--grey-50: #f0f4f8;
--grey-100: #d9e2ec;
--grey-200: #bcccdc;
--grey-300: #9fb3c8;
--grey-400: #829ab1;
--grey-500: #627d98;
--grey-600: #486581;
--grey-700: #334e68;
--grey-800: #243b53;
--grey-900: #102a43;
/* rest of the colors */
--black: #222;
--white: #fff;
--blue: #2336e2;
--red-light: #f8d7da;
--red-dark: #842029;
--green-light: #d1e7dd;
--green-dark: #0f5132;
/* fonts */
--headingFont: 'Roboto Condensed', Sans-Serif;
--bodyFont: 'Cabin', Sans-Serif;
--small-text: 0.875rem;
--extra-small-text: 0.7em;
/* rest of the vars */
--backgroundColor: var(--grey-50);
--textColor: var(--grey-900);
--borderRadius: 0.25rem;
--letterSpacing: 1px;
--transition: 0.3s ease-in-out all;
--max-width: 1120px;
--fixed-width: 500px;
--fluid-width: 90vw;
--breakpoint-lg: 992px;
--nav-height: 6rem;
/* box shadow*/
--shadow-1: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06);
--shadow-2: 0 4px 6px -1px rgba(0, 0, 0, 0.1),
0 2px 4px -1px rgba(0, 0, 0, 0.06);
--shadow-3: 0 10px 15px -3px rgba(0, 0, 0, 0.1),
0 4px 6px -2px rgba(0, 0, 0, 0.05);
--shadow-4: 0 20px 25px -5px rgba(0, 0, 0, 0.1),
0 10px 10px -5px rgba(0, 0, 0, 0.04);
}
body {
background: var(--backgroundColor);
font-family: var(--bodyFont);
font-weight: 400;
line-height: 1.75;
color: var(--textColor);
}
p {
margin-bottom: 1.5rem;
max-width: 40em;
}
h1,
h2,
h3,
h4,
h5 {
margin: 0;
margin-bottom: 1.38rem;
font-family: var(--headingFont);
font-weight: 400;
line-height: 1.3;
text-transform: capitalize;
letter-spacing: var(--letterSpacing);
}
h1 {
margin-top: 0;
font-size: 3.052rem;
}
h2 {
font-size: 2.441rem;
}
h3 {
font-size: 1.953rem;
}
h4 {
font-size: 1.563rem;
}
h5 {
font-size: 1.25rem;
}
small,
.text-small {
font-size: var(--small-text);
}
a {
text-decoration: none;
letter-spacing: var(--letterSpacing);
}
a,
button {
line-height: 1.15;
}
button:disabled {
cursor: not-allowed;
}
ul {
list-style-type: none;
padding: 0;
}
.sidebar {
position: fixed;
top: 0;
left: 0;
background: var(--white);
height: 100%;
width: 260px;
z-index: 100;
transition: var(--transition);
}
.sidebar.close {
width: 78px;
}
.logo-details {
width: 100px;
height: 60px;
display: flex;
align-items: center;
}
.logo-details span:first-child {
font-size: 30px;
/* color: #94EB58; */
color: var(--primary-600);
height: 50px;
min-width: 78px;
text-align: left;
line-height: 50px;
}
.logo_name img {
width: 140px;
height: 100px;
margin-top: 8px;
transition: 0.3s ease;
transition-delay: 0.1s;
}
.logo_name {
transition-delay: 0s;
opacity: 0;
pointer-events: none;
}
.nav-links {
height: 100%;
padding-top: 30px 0 150px 0;
overflow: auto;
}
.nav-links {
overflow: visible;
}
.nav-links::-webkit-scrollbar{
display: none;
}
.nav-links li {
position: relative;
transition: var(--transition);
}
.nav-links li:hover {
background: var(--green-light);
}
.nav-links li span {
height: 50px;
min-width: 78px;
text-align: center;
line-height: 50px;
color: var(--primary-500);
font-size: 23px;
cursor: pointer;
transition: var(--transition);
}
.nav-links li.showMenu span.arrow {
transform: rotate(-180deg);
}
.sidebar.close .nav-links span.arrow {
display: none;
}
.link_name {
font-size: 18px;
font-weight: 400;
color: var(--grey-700);
transition: var(--transition);
}
.sidebar.close .nav-links li a .link_name{
opacity: 0;
pointer-events: none;
}
.nav-links li a {
display: flex;
align-items: center;
}
.icon-link {
display: flex;
align-items: center;
justify-content: space-between;
}
.sidebar .nav-links li .sub-menu {
padding: 6px 6px 14px 80px;
margin-top: -10px;
background: var(--green-light);
display: none;
}
.sidebar .nav-links li.showMenu .sub-menu{
display: block;
}
.sidebar .nav-links li .sub-menu a {
color: var(--primary-900);
font-size: 15px;
padding: 5px 0;
white-space: nowrap;
opacity: 0.6;
transition: var(--transition);
}
.sidebar .nav-links li .sub-menu a:hover {
opacity: 1;
}
.sidebar.close .nav-links li .sub-menu {
position: absolute;
left: 100%;
top: -10px;
margin-top: 0;
padding: 10px 20px;
border-radius: 0 6px 6px 0;
opacity: 0;
display: block;
pointer-events: none;
transition: 0s;
}
.sidebar.close .nav-links li:hover .sub-menu {
top: 0;
opacity: 1;
pointer-events: auto;
transition: var(--transition);
}
.sidebar .nav-links li .sub-menu.link_name {
display: none;
}
.sidebar.close .nav-links li .sub-menu.link_name {
display: none;
}
.sidebar.close .nav-links li .sub-menu .link_name {
font-size: 18px;
opacity: 1;
display: block;
}
.sidebar .nav-links li .sub-menu.blank {
opacity: 1;
pointer-events: auto;
padding: 3px 20px 6px 16px;
opacity: 0;
pointer-events: none;
}
.sidebar .nav-links li:hover .sub-menu.blank {
top: 50%;
transform: translateY(-50%);
}
.sidebar .profile-details {
position: fixed;
bottom: 0;
width: 260px;
display: flex;
align-items: center;
justify-content: space-between;
background: var(--green-light);
padding: 12px 0;
transition: var(--transition);
}
.sidebar.close .profile-details{
background: none;
}
.sidebar.close .profile-details{
width: 78px;
}
.profile-content {
display: flex;
align-items: center;
}
.sidebar .profile-details img {
height: 52px;
width: 52px;
object-fit: cover;
border-radius: 16px;
margin: 0 14px 0 12px;
background: var(--white);
transition: var(--transition);
}
.sidebar.close .profile-details img{
padding: 10px;
}
.profile_name, .job {
color: var(--grey-700);
font-size: 18px;
font-weight: 500;
white-space: nowrap;
}
.sidebar.close .profile-details i,
.sidebar.close .profile-details .profile_name,
.sidebar.close .profile-details .job{
display: none;
}
.job {
font-size: 12px;
}
.home-section {
position: relative;
background: var(--backgroundColor);
height: 100vh;
left: 260px;
width: calc(100%-260px);
transition: var(--transition);
}
.sidebar.close ~ .home-section {
left: 78px;
width: calc(100% -78px);
}
.home-content {
height: 60px;
display: flex;
align-items: center;
}
.home-content span:first-child,
.text {
color: var(--grey-700);
font-size: 35px;
}
.home-content span:first-child {
margin: 0 15px;
cursor: pointer;
}
.home-section .home-content .text{
font-size: 26px;
font-weight: 600;
}
#media (max-width: 400px) {
.sidebar.close .nav-links li .sub-menu{
display: none;
}
.sidebar{
width: 78px;
}
.sidebar.close{
width: 0;
}
.home-section{
left: 78px;
width: calc(100% - 78px);
z-index: 100;
}
.sidebar.close ~ .home-section{
width: 100%;
left: 0;
}
}
<!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">
<link rel="stylesheet" href="admindashcss.css">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons+Sharp" rel="stylesheet">
<title>Admin Dashboard</title>
</head>
<body>
<div class="sidebar">
<div class="logo-details">
<span class="material-icons-sharp">radio_button_checked</span>
<span class="logo_name"><img src="images/logowhite.svg" alt=""></span>
</div>
<ul class="nav-links">
<li>
<a href="#">
<span class="material-icons-sharp">grid_view</span>
<span class="link_name">Dashboard</span>
</a>
<!-- <ul class="sub-menu blank">
<li><a class="link_name" href="#">Category</a></li>
</ul> -->
</li>
<li>
<div class="icon-link">
<a href="#">
<span class="material-icons-sharp">contact_page</span>
<span class="link_name">Contacts</span>
</a>
<span class="material-icons-sharp arrow">expand_more</span>
</div>
<ul class="sub-menu">
<li><a class="link_name" href="#">Category</a></li>
<li>Customer</li>
<li>Resources</li>
</ul>
</li>
<li>
<div class="icon-link">
<a href="#">
<span class="material-icons-sharp">task</span>
<span class="link_name">Projects</span>
</a>
<span class="material-icons-sharp arrow">expand_more</span>
</div>
<ul class="sub-menu">
<li><a class="link_name" href="#">Projects</a></li>
<li>Project details</li>
<li>Job</li>
</ul>
</li>
<li>
<div class="icon-link">
<a href="#">
<span class="material-icons-sharp">payments</span>
<span class="link_name">Invoices</span>
</a>
<span class="material-icons-sharp arrow">expand_more</span>
</div>
<ul class="sub-menu">
<li><a class="link_name" href="#">Invoices</a></li>
<li>Recivables</li>
<li>Paybles</li>
</ul>
</li>
<li>
<div class="icon-link">
<a href="#">
<span class="material-icons-sharp">query_stats</span>
<span class="link_name">Queries</span>
</a>
<span class="material-icons-sharp arrow">expand_more</span>
</div>
<ul class="sub-menu">
<li><a class="link_name" href="#">Categories</a></li>
<li>Recivables</li>
<li>Payables</li>
<li>Gross Profit</li>
<li>Gross Profit Summary</li>
</ul>
</li>
<li>
<div class="icon-link">
<a href="#">
<span class="material-icons-sharp">admin_panel_settings</span>
<span class="link_name">Admin</span>
</a>
<span class="material-icons-sharp arrow">expand_more</span>
</div>
<ul class="sub-menu">
<li><a class="link_name" href="#">Categories</a></li>
<li>Settings</li>
<li>Users</li>
<li>Documents</li>
</ul>
</li>
<li>
<div class="profile-details">
<div class="profile-content">
<img src="images/messi.jpg" alt="profile">
</div>
<div class="name-job">
<div class="profile_name">Brook Beyene</div>
<div class="job">Project Manager</div>
</div>
<span class="material-icons-sharp">logout</span>
</div>
</li>
</ul>
</div>
<section class="home-section">
<div class="home-content">
<span class="material-icons-sharp bx-menu">menu</span>
<span class="text">Drop Down Sidebar</span>
</div>
</section>
</body>
</html>
:
All you nedd is to add close to class list in following line:
<div class="sidebar close">
Also if you need to keep last state of navbar, you can stor it in local storage and append it by js on load.
let arrow = document.querySelectorAll(".arrow");
for (var i = 0; i < arrow.length; i++) {
arrow[i].addEventListener("click", (e)=>{
let arrowParent = e.target.parentElement.parentElement;
arrowParent.classList.toggle("showMenu");
})
}
let sidebar = document.querySelector(".sidebar");
let sidebarBtn = document.querySelector(".bx-menu");
sidebarBtn.addEventListener("click" , ()=>{
sidebar.classList.toggle("close");
});
*{
box-sizing: border-box;
margin: 0;
padding: 0;
}
/* fonts */
#import url('https://fonts.googleapis.com/css2?family=Cabin&family=Roboto+Condensed:wght#400;700&display=swap');
html {
font-size: 100%;
} /*16px*/
:root {
/* colors */
--primary-50: #e0fcff;
--primary-100: #bef8fd;
--primary-200: #87eaf2;
--primary-300: #54d1db;
--primary-400: #38bec9;
--primary-500: #2cb1bc;
--primary-600: #14919b;
--primary-700: #0e7c86;
--primary-800: #0a6c74;
--primary-900: #044e54;
/* grey */
--grey-50: #f0f4f8;
--grey-100: #d9e2ec;
--grey-200: #bcccdc;
--grey-300: #9fb3c8;
--grey-400: #829ab1;
--grey-500: #627d98;
--grey-600: #486581;
--grey-700: #334e68;
--grey-800: #243b53;
--grey-900: #102a43;
/* rest of the colors */
--black: #222;
--white: #fff;
--blue: #2336e2;
--red-light: #f8d7da;
--red-dark: #842029;
--green-light: #d1e7dd;
--green-dark: #0f5132;
/* fonts */
--headingFont: 'Roboto Condensed', Sans-Serif;
--bodyFont: 'Cabin', Sans-Serif;
--small-text: 0.875rem;
--extra-small-text: 0.7em;
/* rest of the vars */
--backgroundColor: var(--grey-50);
--textColor: var(--grey-900);
--borderRadius: 0.25rem;
--letterSpacing: 1px;
--transition: 0.3s ease-in-out all;
--max-width: 1120px;
--fixed-width: 500px;
--fluid-width: 90vw;
--breakpoint-lg: 992px;
--nav-height: 6rem;
/* box shadow*/
--shadow-1: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06);
--shadow-2: 0 4px 6px -1px rgba(0, 0, 0, 0.1),
0 2px 4px -1px rgba(0, 0, 0, 0.06);
--shadow-3: 0 10px 15px -3px rgba(0, 0, 0, 0.1),
0 4px 6px -2px rgba(0, 0, 0, 0.05);
--shadow-4: 0 20px 25px -5px rgba(0, 0, 0, 0.1),
0 10px 10px -5px rgba(0, 0, 0, 0.04);
}
body {
background: var(--backgroundColor);
font-family: var(--bodyFont);
font-weight: 400;
line-height: 1.75;
color: var(--textColor);
}
p {
margin-bottom: 1.5rem;
max-width: 40em;
}
h1,
h2,
h3,
h4,
h5 {
margin: 0;
margin-bottom: 1.38rem;
font-family: var(--headingFont);
font-weight: 400;
line-height: 1.3;
text-transform: capitalize;
letter-spacing: var(--letterSpacing);
}
h1 {
margin-top: 0;
font-size: 3.052rem;
}
h2 {
font-size: 2.441rem;
}
h3 {
font-size: 1.953rem;
}
h4 {
font-size: 1.563rem;
}
h5 {
font-size: 1.25rem;
}
small,
.text-small {
font-size: var(--small-text);
}
a {
text-decoration: none;
letter-spacing: var(--letterSpacing);
}
a,
button {
line-height: 1.15;
}
button:disabled {
cursor: not-allowed;
}
ul {
list-style-type: none;
padding: 0;
}
.sidebar {
position: fixed;
top: 0;
left: 0;
background: var(--white);
height: 100%;
width: 260px;
z-index: 100;
transition: var(--transition);
}
.sidebar.close {
width: 78px;
}
.logo-details {
width: 100px;
height: 60px;
display: flex;
align-items: center;
}
.logo-details span:first-child {
font-size: 30px;
/* color: #94EB58; */
color: var(--primary-600);
height: 50px;
min-width: 78px;
text-align: left;
line-height: 50px;
}
.logo_name img {
width: 140px;
height: 100px;
margin-top: 8px;
transition: 0.3s ease;
transition-delay: 0.1s;
}
.logo_name {
transition-delay: 0s;
opacity: 0;
pointer-events: none;
}
.nav-links {
height: 100%;
padding-top: 30px 0 150px 0;
overflow: auto;
}
.nav-links {
overflow: visible;
}
.nav-links::-webkit-scrollbar{
display: none;
}
.nav-links li {
position: relative;
transition: var(--transition);
}
.nav-links li:hover {
background: var(--green-light);
}
.nav-links li span {
height: 50px;
min-width: 78px;
text-align: center;
line-height: 50px;
color: var(--primary-500);
font-size: 23px;
cursor: pointer;
transition: var(--transition);
}
.nav-links li.showMenu span.arrow {
transform: rotate(-180deg);
}
.sidebar.close .nav-links span.arrow {
display: none;
}
.link_name {
font-size: 18px;
font-weight: 400;
color: var(--grey-700);
transition: var(--transition);
}
.sidebar.close .nav-links li a .link_name{
opacity: 0;
pointer-events: none;
}
.nav-links li a {
display: flex;
align-items: center;
}
.icon-link {
display: flex;
align-items: center;
justify-content: space-between;
}
.sidebar .nav-links li .sub-menu {
padding: 6px 6px 14px 80px;
margin-top: -10px;
background: var(--green-light);
display: none;
}
.sidebar .nav-links li.showMenu .sub-menu{
display: block;
}
.sidebar .nav-links li .sub-menu a {
color: var(--primary-900);
font-size: 15px;
padding: 5px 0;
white-space: nowrap;
opacity: 0.6;
transition: var(--transition);
}
.sidebar .nav-links li .sub-menu a:hover {
opacity: 1;
}
.sidebar.close .nav-links li .sub-menu {
position: absolute;
left: 100%;
top: -10px;
margin-top: 0;
padding: 10px 20px;
border-radius: 0 6px 6px 0;
opacity: 0;
display: block;
pointer-events: none;
transition: 0s;
}
.sidebar.close .nav-links li:hover .sub-menu {
top: 0;
opacity: 1;
pointer-events: auto;
transition: var(--transition);
}
.sidebar .nav-links li .sub-menu.link_name {
display: none;
}
.sidebar.close .nav-links li .sub-menu.link_name {
display: none;
}
.sidebar.close .nav-links li .sub-menu .link_name {
font-size: 18px;
opacity: 1;
display: block;
}
.sidebar .nav-links li .sub-menu.blank {
opacity: 1;
pointer-events: auto;
padding: 3px 20px 6px 16px;
opacity: 0;
pointer-events: none;
}
.sidebar .nav-links li:hover .sub-menu.blank {
top: 50%;
transform: translateY(-50%);
}
.sidebar .profile-details {
position: fixed;
bottom: 0;
width: 260px;
display: flex;
align-items: center;
justify-content: space-between;
background: var(--green-light);
padding: 12px 0;
transition: var(--transition);
}
.sidebar.close .profile-details{
background: none;
}
.sidebar.close .profile-details{
width: 78px;
}
.profile-content {
display: flex;
align-items: center;
}
.sidebar .profile-details img {
height: 52px;
width: 52px;
object-fit: cover;
border-radius: 16px;
margin: 0 14px 0 12px;
background: var(--white);
transition: var(--transition);
}
.sidebar.close .profile-details img{
padding: 10px;
}
.profile_name, .job {
color: var(--grey-700);
font-size: 18px;
font-weight: 500;
white-space: nowrap;
}
.sidebar.close .profile-details i,
.sidebar.close .profile-details .profile_name,
.sidebar.close .profile-details .job{
display: none;
}
.job {
font-size: 12px;
}
.home-section {
position: relative;
background: var(--backgroundColor);
height: 100vh;
left: 260px;
width: calc(100%-260px);
transition: var(--transition);
}
.sidebar.close ~ .home-section {
left: 78px;
width: calc(100% -78px);
}
.home-content {
height: 60px;
display: flex;
align-items: center;
}
.home-content span:first-child,
.text {
color: var(--grey-700);
font-size: 35px;
}
.home-content span:first-child {
margin: 0 15px;
cursor: pointer;
}
.home-section .home-content .text{
font-size: 26px;
font-weight: 600;
}
#media (max-width: 400px) {
.sidebar.close .nav-links li .sub-menu{
display: none;
}
.sidebar{
width: 78px;
}
.sidebar.close{
width: 0;
}
.home-section{
left: 78px;
width: calc(100% - 78px);
z-index: 100;
}
.sidebar.close ~ .home-section{
width: 100%;
left: 0;
}
}
<!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">
<link rel="stylesheet" href="admindashcss.css">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons+Sharp" rel="stylesheet">
<title>Admin Dashboard</title>
</head>
<body>
<div class="sidebar close">
<div class="logo-details">
<span class="material-icons-sharp">radio_button_checked</span>
<span class="logo_name"><img src="images/logowhite.svg" alt=""></span>
</div>
<ul class="nav-links">
<li>
<a href="#">
<span class="material-icons-sharp">grid_view</span>
<span class="link_name">Dashboard</span>
</a>
<!-- <ul class="sub-menu blank">
<li><a class="link_name" href="#">Category</a></li>
</ul> -->
</li>
<li>
<div class="icon-link">
<a href="#">
<span class="material-icons-sharp">contact_page</span>
<span class="link_name">Contacts</span>
</a>
<span class="material-icons-sharp arrow">expand_more</span>
</div>
<ul class="sub-menu">
<li><a class="link_name" href="#">Category</a></li>
<li>Customer</li>
<li>Resources</li>
</ul>
</li>
<li>
<div class="icon-link">
<a href="#">
<span class="material-icons-sharp">task</span>
<span class="link_name">Projects</span>
</a>
<span class="material-icons-sharp arrow">expand_more</span>
</div>
<ul class="sub-menu">
<li><a class="link_name" href="#">Projects</a></li>
<li>Project details</li>
<li>Job</li>
</ul>
</li>
<li>
<div class="icon-link">
<a href="#">
<span class="material-icons-sharp">payments</span>
<span class="link_name">Invoices</span>
</a>
<span class="material-icons-sharp arrow">expand_more</span>
</div>
<ul class="sub-menu">
<li><a class="link_name" href="#">Invoices</a></li>
<li>Recivables</li>
<li>Paybles</li>
</ul>
</li>
<li>
<div class="icon-link">
<a href="#">
<span class="material-icons-sharp">query_stats</span>
<span class="link_name">Queries</span>
</a>
<span class="material-icons-sharp arrow">expand_more</span>
</div>
<ul class="sub-menu">
<li><a class="link_name" href="#">Categories</a></li>
<li>Recivables</li>
<li>Payables</li>
<li>Gross Profit</li>
<li>Gross Profit Summary</li>
</ul>
</li>
<li>
<div class="icon-link">
<a href="#">
<span class="material-icons-sharp">admin_panel_settings</span>
<span class="link_name">Admin</span>
</a>
<span class="material-icons-sharp arrow">expand_more</span>
</div>
<ul class="sub-menu">
<li><a class="link_name" href="#">Categories</a></li>
<li>Settings</li>
<li>Users</li>
<li>Documents</li>
</ul>
</li>
<li>
<div class="profile-details">
<div class="profile-content">
<img src="images/messi.jpg" alt="profile">
</div>
<div class="name-job">
<div class="profile_name">Brook Beyene</div>
<div class="job">Project Manager</div>
</div>
<span class="material-icons-sharp">logout</span>
</div>
</li>
</ul>
</div>
<section class="home-section">
<div class="home-content">
<span class="material-icons-sharp bx-menu">menu</span>
<span class="text">Drop Down Sidebar</span>
</div>
</section>
</body>
</html>
My suggestion is to use a cookie to store and read the value on change. It will keep the data persistant on reload that way.
I have created this nice nav bar, each link is a polygon with a nice angled border behind that appears every time i hover.My first link though, the home link, i ve decided to make it a parallelogram. When i hover over the home link i want the left side to have no border,but even though i have set the width to 100% and i have created it exactly like i wanted,i get that weird pixel of color. any ideas?
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
header {
font-size: 12px;
background: #1A2434;
position: fixed;
top: 0;
left: 0;
z-index: 100;
}
.logo:hover {
filter: opacity(1.2);
-webkit-filter: opacity(1.2);
cursor: pointer;
}
.header {
position: fixed;
width: 100vw;
background-color: #101620;
height: 140px;
}
.menu {
width: 80vw;
height: 60px;
background-color: #1A2434;
margin-left: 22vw;
margin-top: 60px;
transform: skew(-30deg);
}
.bottom {
width: 100vw;
height: 20px;
background-color: #1A2434;
}
.logo img {
width: 120px;
z-index: 2;
margin-left: 8vw;
margin-top: 1px;
filter: opacity(0.9);
-webkit-filter: opacity(0.9);
}
.nav {
width: 80vw;
margin-right: 5vw;
display: flex;
justify-content: space-around;
line-height: 5vh;
transform: skew(30deg);
}
#home{
clip-path: polygon(10% 0, 100% 0, 90% 100%, 0% 100%);
margin-left: -2vw;
background-color: #1A2434;
}
.nav li {
height: 60px;
width: 100%;
list-style: none;
text-align: center;
display: flex;
justify-content: center;
clip-path: polygon(0 0, 100% 0, 90% 100%, 10% 100%);
}
#a-home{
clip-path: polygon(10% 0, 100% 0%, 89% 96%, -2px 100%);
height:100%;
}
.nav li a {
color: white;
text-decoration: none; /*removes underline*/
display: block; /*links clickable everywhere*/
background-color: #1A2434;
height:97%;
width:100%;
clip-path: polygon(0 0, 100% 0, 89% 100%, 11% 100%);
padding-top: 7px;
}
.nav li:hover {
cursor: pointer;
background-color: rgb(140, 30, 30);
transition: 0.3s ease;
}
.nav li a:hover{
transition: 0.4s ease;
background-color:#101620;
}
#home:hover{
background-color: rgb(140, 30, 30);
}
.social {
display: flex;
transition: 0.4s;
margin-left: 35vw;
}
.social span {
width: 250px;
text-align: center;
color: white;
margin-top: 22px;
font-size: 11px;
letter-spacing: 0.2px;
}
.social ul {
margin-top: 16px;
width: 150px;
display: flex;
list-style: none;
justify-content: space-around;
}
.social ul li a {
font-size: 22px;
color: rgb(255, 255, 255);
}
.social ul a:hover,
.social ul a:focus {
color: #931d37;
transform: scale(1.2);
transition: 0.4s;
cursor: pointer;
}
.search {
margin-left: 12vw;
margin-top: 18px;
width: 175px;
}
input {
background-color: transparent;
border: none;
height: 25px;
width: 150px;
padding-left: 5px;
}
::placeholder {
opacity: 0.7;
}
button {
background-color: transparent;
border: none;
color: white;
width: 25px;
height: 25px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
cursor: pointer;
padding: 0;
margin: 0;
position: absolute;
}
input,
select,
textarea {
color: #ffffff;
font-size: 12px;
}
.logo-socials {
position: fixed;
display: flex;
}
<header>
<div class="header">
<div class="logo-socials">
<div class="logo">
<img class="logo" src="" alt="logo">
</div>
<form action="/search" method="POST">
<div class="search">
<input type="text" id="input" name="input" placeholder="Search..." autocomplete="off">
<button type="submit"><i class="fa fa-search"></i></button>
</div>
<div class="autocom-box" id="autocom-box"></div>
</form>
<div class="social">
<span>Follow us on social media :</span>
<ul>
<li><a href="https://www.facebook.com"
target="_blank"><i class="fab fa-facebook" aria-hidden="true"></i></a></li>
<li><a href="https://www.instagram.com" target="_blank"><i
class="fab fa-instagram" aria-hidden="true"></i></a></li>
<li><a href=" https://www.youtube.com" target="_blank"><i
class="fab fa-youtube" aria-hidden="true"></i></a></li>
</ul>
</div>
</div>
<div class="menu">
<ul class="nav">
<li id="home">Home</li>
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
<li>Link 4</li>
</ul>
</div>
<div class="bottom">
</div>
</div>
</header>
<script src='https://kit.fontawesome.com/a076d05399.js' crossorigin='anonymous'></script>
Please take a look at the following basic example:
html,
body {
height: 100%;
}
body {
margin: 0;
}
.sidebar {
height: 100%;
width: 300px;
background-color: pink;
float: left;
}
.students-list {
height: 100%;
width: 200px;
background-color: skyblue;
float: left;
}
<div class="sidebar">
Students List
</div>
<div class="students-list">
</div>
The pink div is a sidebar with a link. at first the second div which is the skyblue one, should be hidden, and when the user clicks on the link the whole div should slide smoothly from the left (From underneath the pink div).
Do any of you have a tip or can help me out how to achieve the animation?
EDIT: Is it possible to also use another button to slide out even a third div to the right of the second div?
Please check the below code
body {
font-family: "Roboto", sans-serif;
font-size: 18px;
overflow-x: hidden;
}
main {
padding: 40px 25px;
transition: .5s ease-in-out;
width: 100%;
}
nav {
background: #36a;
bottom: 0;
box-shadow: 0 0 5px #666;
height: 100%;
left: -17.5rem;
padding-top: 38px;
position: absolute;
top: 0;
transition: .5s ease-in-out;
width: 17rem;
user-select: none;
}
nav a {
color: #eee;
display: block;
padding: 12px 16px;
transition: .5s;
text-decoration: none;
-webkit-user-drag: none;
}
nav a:hover {
background-color: #39c;
}
#nav-collapse {
padding: 8px 12px;
position: absolute;
right: 0;
top: 0;
}
#nav-expand {
background-color: #333;
color: #eee;
display: block;
left: 0;
top: 0;
padding: 8px 12px;
position: absolute;
text-decoration: none;
transition: .5s linear;
user-select: none;
-webkit-user-drag: none;
}
#nav-expand:focus {
opacity: 0;
}
#nav-expand:focus ~ main {
margin-left: 17rem;
transition-delay: .25s;
}
#nav-expand:focus ~ nav {
left: 0;
transition-delay: .25s;
}
#nav-expand:hover {
background-color: #369;
color: #fff;
}
.icon {
display: inline-block;
background-position: center;
background-repeat: no-repeat;
height: 12px;
width: 12px;
}
.icon-cross {
background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAALCAYAAACprHcmAAAABHNCSVQICAgIfAhkiAAAAPdJREFUGJV1jz9LhWAYxc+9rVZDQ+DcEI0NLU19gxr6Ci3tuvjnA+gggSBt4qzzO90b5FBubkHgEM1CBL5K93Ia8jWv4G88/A7PeQAArusekXwRQpiGYSzQ4/v+Gcn3OI6vMRJf+cdWFXrxs8+bOI5vkCTJLckf/rMtiuJhJJIk67oWAIA0Te8mhR2klGvbtg/UvNlC27ZPSlwquaqqZwA1dmGe56uu676HxPO8U5IfMyuGp+fEt+nTQghzqWnaMYBDdUVKuXYc5yLLsnsAmz5e6Lp+AgCIouiK5FfTNCvLsvanT5dl+Wia5t6wOwiC87GoCMPwUom/I2EVwEqOzwUAAAAASUVORK5CYII=');
}
.icon-menu {
background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAMCAYAAABWdVznAAAABHNCSVQICAgIfAhkiAAAAB5JREFUKJFj/P///38GEgATKYoHKWAc9TQRgGRPAwD0IAv+FT8LPwAAAABJRU5ErkJggg==');
}
<a id="nav-expand" href="#">
<span class="icon icon-menu"></span>
Menu
</a>
<nav>
<a id="nav-collapse" href="#">
<span class="icon icon-cross"></span>
</a>
Link1
Link2
Link3
Link4
</nav>
<main>
<h2>Push Sidebar Exmaple</h2>
<p>Please click on menu button.</p>
</main>
My navigation items are not displaying on large screens, however on mobile devices they are displaying as expected. Can anyone assist me with a solution. I am suspecting that it has to do with the clip-path property, however i have tried to play around with it and haven't had any joy.I want the navigation items to display on large screens as it is displaying on mobile devices.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: sans-serif;
}
a {
text-decoration: none;
}
ul {
list-style: none;
}
.navBar {
position: relative;
height: 10vh;
display: flex;
font-family: Verdana, Arial, Helvetica, sans-serif;
background: #003300;
}
.navbrand {
padding: .8em 0 0 1em;
/* margin: 2.5em 0;*/
}
.logo {
font-size: 1.4rem;
font-weight: 600;
color: #fff;
}
.navList {
position: absolute;
background: #003300;
height: 100vh;
width: 100%;
display: flex;
flex-direction: column;
clip-path: circle(100px at 90% -10%);
-webkit-clip-path: circle(100px at 90% -10%);
transition: all 1s ease-out;
pointer-events: none;
padding-top: 9em;
}
.navList.open {
clip-path: circle(1000px at 90% -10%);
-webkit-clip-path: circle(1000px at 90% -10%);
pointer-events: all;
}
.navItem {
text-align: center;
padding-top: 1.2em;
}
.navLink {
font-size: 1rem;
font-weight: 600;
color: #fff;
line-height: 1.6;
}
.navLink:nth-child(1) {
transition: all 0.5s ease 0.2s;
}
.navLink:nth-child(2) {
transition: all 0.5s ease 0.4s;
}
.navLink:nth-child(3) {
transition: all 0.5s ease 0.6s;
}
.navLink:nth-child(4) {
transition: all 0.5s ease 0.6s;
}
.navLink:nth-child(5) {
transition: all 0.5s ease 0.6s;
}
.navLink:nth-child(6) {
transition: all 0.5s ease 0.6s;
}
.navLink.fade {
opacity: 1;
}
.navLink:focus {
color: #ff0000;
opacity: .3;
}
.navLink:hover {
color: #ff0000;
opacity: .6;
}
.socialContact {
display: flex;
margin: 1em auto;
}
.socialLink {
background: #fafafa;
width: 40px;
height: 40px;
margin: 1em 0.625em;
border-radius: 50%;
display: flex;
justify-content: center;
align-items: center;
}
.socialFB {
font-size: 1.5rem;
color: #4267b2;
padding: 1em;
}
.socialTwitter {
font-size: 1.5rem;
color: #1da1f2;
padding: 1em;
}
.socialInsta {
font-size: 1.5rem;
color: #000;
padding: 1em;
}
.hamburger-btn {
position: absolute;
cursor: pointer;
right: 5%;
top: 50%;
transform: translate(-5%, -50%);
z-index: 2;
}
.hamburger-btn_burger {
width: 20px;
height: 3px;
background: #fff;
margin: 5px;
transition: all .5s ease-in-out;
}
.hamburger-btn_burger::before,
.hamburger-btn_burger::after {
content: '';
position: absolute;
width: 20px;
height: 3px;
background: #fff;
border-radius: 5px;
transition: all .5s ease-in-out;
}
.hamburger-btn_burger::before {
transform: translateY(-7px);
}
.hamburger-btn_burger::after {
transform: translateY(7px);
}
.hamburger-btn.open .hamburger-btn_burger {
transform: translateX(-50px);
background: transparent;
}
.hamburger-btn.open .hamburger-btn_burger::before {
transform: rotate(45deg) translate(35px, -35px);
}
.hamburger-btn.open .hamburger-btn_burger::after {
transform: rotate(-45deg) translate(35px, 35px);
}
#media screen and (min-width: 768px) {
.navBar {
justify-content: space-between;
align-items: center;
flex-direction: row;
}
.navList {
width: 80%;
height: 100%;
display: flex;
flex-direction: row;
justify-content: flex-end;
align-items: center;
}
.navItem {
padding-top: 1em;
padding-right: 2em;
padding-bottom: 2em;
}
.navLink {
font-size: 1rem;
color: #fff;
cursor: pointer;
display: inline-block;
margin: 0 70px 0 0;
text-align: left;
border-bottom-style: none;
padding: 0;
}
.navbrand {
margin-bottom: 1em;
}
.logo {
margin-top: 0;
}
.hamburger-btn {
display: none;
}
.socialContact {
display: none;
}
.logo:focus {
color: #ff0000;
opacity: .1;
}
.logo:hover {
color: #ff0000;
}
}
<nav class="navBar">
<div class="navbrand">
XandY Junior School
</div>
<div class="hamburger-btn">
<div class="hamburger-btn_burger"></div>
</div>
<ul class="navList">
<li class="navItem">
</li>
<li class="navItem">
Our Story
</li>
<li class="navItem">
Sports & Clubs
</li>
<li class="navItem">
Admissions
</li>
<li class="navItem">
Parents
</li>
<li class="navItem">
BOSA
</li>
<li class="navItem">
Contact Us
</li>
<li class="socialContact">
<i class="fab fa-facebook-f socialFB"></i>
<i class="fab fa-twitter socialTwitter"></i>
<i class="fab fa-instagram socialInsta"></i>
</li>
</ul>
</nav>
You are correct clip-path is on of the issues. But background is also an issue as its overlapping the existing content when clip-path is removed
I was able to see your "navlist" items using below css. Commented background, clip-path & Padding
.navList {
position: absolute;
/* background: #003300; */
height: 100vh;
width: 100%;
display: flex;
flex-direction: column;
/* clip-path: circle(100px at 90% -10%); */
/* -webkit-clip-path: circle(100px at 90% -10%); */
transition: all 1s ease-out;
pointer-events: none;
/* padding-top: 9em; */
}
Although, I'm not able to see the Navbar in mobile mode also, when I use dev-tools.
it won't display on the large screens as per "bootstrap" for making the nav-bar not displayed with using display:none css class in the large screen.
so i will recommend for you to not use nav-bar from bootstrap and you can easily do it with html - css.
$(document).ready(function(){
$(".hamburger-btn").click(function(){
$(".navList").toggle();
});
});
body{
margin: 0;
padding: 0;
}
.navbrand {
display: block;
background: darkgreen;
padding: 10px;
}
.logo {
padding: 10px;
color: #fff;
text-decoration: none;
}
.hamburger-btn {
position: absolute;
top: -50px;
right: -3px;
background: darkgreen;
width: 150px;
height: 150px;
border-radius: 150px;
}
.hamburger-btn_burger {
width: 30px;
height: 4px;
background: #fff;
display: block;
position: absolute;
bottom: 77px;
right: 60px;
}
.hamburger-btn_burger::before{
position: absolute;
content: "";
width: 30px;
height: 4px;
background: #fff;
top: -8px;
z-index: 9;
}
.hamburger-btn_burger::after{
position: absolute;
content: "";
width: 30px;
height: 4px;
background: #fff;
bottom: -8px;
z-index: 9;
}
.navList{
display: none;
background: darkgreen;
padding: 15px;
position: absolute;
top: 22px;
width: 100%;
z-index: 1;
}
.navList li{list-style: none;display: block;}
.navList li a{display: block; color: #fff; text-decoration: none; padding: 10px;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<nav class="navBar">
<div class="navbrand">
XandY Junior School
</div>
<div class="hamburger-btn">
<div class="hamburger-btn_burger"></div>
</div>
<ul class="navList">
<li class="navItem">
About Us
</li>
<li class="navItem">
Our Story
</li>
<li class="navItem">
Sports & Clubs
</li>
<li class="navItem">
Admissions
</li>
<li class="navItem">
Parents
</li>
<li class="navItem">
BOSA
</li>
<li class="navItem">
Contact Us
</li>
<li class="socialContact">
<i class="fab fa-facebook-f socialFB"></i>
<i class="fab fa-twitter socialTwitter"></i>
<i class="fab fa-instagram socialInsta"></i>
</li>
</ul>
</nav>
Can you please look at this code...
Okay so my navigation is positioned absolute. Also I have a div above that also positioned absolute but with a z index of 2. When you click a toggle button the div with the z-index of 2 gets 240 pixels of margin added to the left revealing the navigation below it. This works perfectly apart from on mobile you can see the navigation below the div by scrolling. How can I fix this?
My website is up at http://www.zoidstudios.com/
Code Pen: http://codepen.io/ZoidCraft/pen/KrRvjr
<nav class="main-nav">
<ul>
<li>Examples</li>
<li>Blog</li>
<li>Contact</li>
</ul>
</nav>
<header class="top-header">
<button class="menu-toggle">
<span></span>
<span></span>
<span></span>
</button>
<h1>Zoid Studios</h1>
<h2>A <span>visual design</span> studio based in the <span>united kingdom</span></h2>
<a class="btn-view" href="">view our work <i class="fa fa-angle-double-right" aria-hidden="true"></i></a>
</header>
.main-nav {
position: fixed;
background-color: #080911;
height: 100%;
width: 240px;
transition: all 0.3s ease-in-out; }
.main-nav ul {
display: flex;
flex-direction: column; }
.main-nav a {
display: block;
padding: 20px 40px;
color: #FFFFFF;
text-align: center;
font-size: 1rem;
transition: all 0.3s ease-in-out; }
.main-nav a:hover {
color: #ffff00; }
.shift {
margin-left: 240px; }
.menu-toggle {
position: absolute;
z-index: 3;
top: 0;
left: 0;
padding: 10px;
margin: 20px;
background-color: transparent;
border: none;
outline: none;
cursor: pointer; }
.menu-toggle span {
display: block;
background-color: #FFFFFF;
width: 18px;
height: 2px;
margin: 4px; }
.top-header {
transition: all 0.3s ease-in-out;
width: 100%;
height: 100%;
display: flex;
position: absolute;
z-index: 2;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #101223;
box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.5); }
.top-header h1 {
color: #FFFFFF;
font-size: 3.2rem;
font-weight: 800;
padding: 25px 0;
text-align: center; }
.top-header h2 {
color: #FFFFFF;
font-size: 2.6rem;
margin-bottom: 80px;
text-align: center;
text-transform: capitalize; }
.top-header h2 span {
color: #ffff00; }
#media screen and (max-width: 768px) {
.top-header h1 {
font-size: 3rem;
margin: 0 10px 30px 10px; }
.top-header h2 {
font-size: 2.4rem;
margin: 0 10px 30px 10px; } }
.btn-view {
color: #FFFFFF;
padding: 15px 20px;
border: 1px solid #ffff00;
text-transform: capitalize;
transition: all 0.3s ease-in-out; }
.btn-view i {
padding-left: 10px; }
.btn-view:hover {
color: #101223;
background-color: #ffff00;
box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.75); }
Is there another way of doing this exact way of side navigation?
Thanks for the help :)