How to create a curtain navigation menu with CSS and JavaScript?

CSSWeb DevelopmentFront End TechnologyJavascript

Following is the code to create a curtain navigation menu with CSS and JavaScript −


 Live Demo

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
.sideNav {
   height: 100vh;
   width: 0;
   position: fixed;
   z-index: 1;
   top: 0;
   left: 0;
   background-color: rgba(46, 218, 195,0.4);
   overflow-x: hidden;
   padding-top: 60px;
   transition: 0.5s;
.sideNav a {
   padding: 8px 8px 8px 32px;
   text-decoration: none;
   font-size: 25px;
   color: #000000;
   font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   display: block;
   transition: 0.3s;
   text-align: center;
.sidenav a:hover {
   color: #f1f1f1;
.sideNav .closeBtn {
   position: absolute;
   top: 0;
   right: 25px;
   font-size: 36px;
   margin-left: 50px;
button {
   padding: 15px;
   background-color: rgb(0, 27, 145);
   color: rgb(255, 255, 255);
   font-size: 20px;
   border: none;
   border-radius: 2%;
<nav class="sideNav">
<a href="#" class="closeBtn">×</a>
<a href="#">Login</a>
<a href="#">Register</a>
<a href="#">Home</a>
<a href="#">About Us</a>
<button class="openSideNav">Click here to open sideNav</button>
<div class="main-content">
<h1>Header One</h1>
<p> Some paragraph text</p>
<h2> Header Two</h2>
<p> Some more paragraph text</p>
let openBtn = document.querySelector(".openSideNav");
openBtn.addEventListener("click", () => {
let closeBtn = document.querySelector(".closeBtn");
closeBtn.addEventListener("click", () => {
function showNav() {
   document.querySelector(".sideNav").style.width = "100%";
function hideNav() {
   document.querySelector(".sideNav").style.width = "0";


The above code will produce the following output −

On clicking the open sideNav button −

Published on 06-Apr-2020 14:53:03