Design a Toggleable Sidebar using HTML CSS and JavaScript Last Updated : 26 Jul, 2024 Comments Improve Suggest changes Like Article Like Report In this article, we will create a toggleable sidebar navigation menu using HTML, CSS, and JavaScript. The navigation menu allows users to open and close a sidebar to access various sections of a website. When the user clicks on a navigation item, the corresponding content is displayed in the main content area. We'll make the website responsive to different screen sizes for a seamless user experience.PreviewApproachFirst, we will make nav items with the help of anchor tags.Now by using CSS properties like position fixed, left, and right properties, we will align the navbar to the sidebar.Design the navbar including headings, and paragraphs by using CSS properties. Change the content of the heading dynamically by using the showContent () function.Lastly, make a button for closing the navbar by using the function name closeNav().Example: In this example, we write a code to build a toggleable sidebar using HTML CSS and JavaScript. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content= "width=device-width, initial-scale=1.0" /> <link rel="stylesheet" href="style.css" /> <title> Toggleable Sidebar using HTML CSS & JavaScript </title> </head> <body> <div id="sideMenu" class="sideMenu"> <a href="javascript:void(0)" class="closeBtn" onclick="closeNav()">×</a> <div class="mainMenu"> <h2>SideMenu</h2> <a href="javascript:void(0)" onclick="showContent('Home')">Home</a> <a href="javascript:void(0)" onclick="showContent('About')">About</a> <a href="javascript:void(0)" onclick="showContent('Portfolio')">Portfolio</a> <a href="javascript:void(0)" onclick="showContent('Services')">Services</a> <a href="javascript:void(0)" onclick="showContent('Contact')">Contact</a> </div> </div> <div id="contentArea"> <span style="font-size: 30px; cursor: pointer" onclick="openNav()">☰</span> <div class="contentText"> <h2 id="contentTitle"> Toggle Sidebar Navigation</h2> <h3>HTML CSS JS</h3> </div> </div> <script src="script.js"></script> </body> </html> CSS /* style.css */ body { margin: 0; font-family: poppins; } .sideMenu { height: 100%; width: 0; position: fixed; z-index: 1; top: 0; left: 0; background: #478cff; overflow-x: hidden; transition: 0.5s; padding-top: 60px; } .mainMenu h2 { text-align: center; letter-spacing: 7px; color: #fff; background: #111; padding: 20px 0; } .sideMenu a { padding: 8px 8px 8px 32px; text-decoration: none; color: #fff; display: block; transition: 0.3s; font-size: 18px; margin-bottom: 20px; text-transform: uppercase; font-weight: bold; } .mainMenu a:hover { color: #fff; background: #111; } .sideMenu .closeBtn { position: absolute; top: 0; right: 25px; font-size: 36px; margin-left: 50px; } #contentArea { transition: margin-left 0.5s; padding: 16px; } .contentText { padding: 100px 20px; text-align: center; } .contentText h2 { background: #478cff; display: inline-block; padding: 15px 10px; text-transform: uppercase; font-size: 24px; color: #fff; } .contentText h3 { text-transform: uppercase; font-size: 18px; margin: 0; letter-spacing: 3px; } @media screen and (min-width: 768px) { .contentText { padding: 100px 180px; } .contentText h2 { padding: 15px 35px; font-size: 50px; } .contentText h3 { font-size: 45px; } } JavaScript function openNav() { document.getElementById("sideMenu") .style.width = "300px"; document.getElementById("contentArea") .style.marginLeft = "300px"; } function closeNav() { document.getElementById("sideMenu") .style.width = "0"; document.getElementById("contentArea") .style.marginLeft = "0"; } function showContent(content) { document.getElementById("contentTitle") .textContent = content + " page"; closeNav(); } Output: Comment More infoAdvertise with us Next Article Design a Toggleable Sidebar using HTML CSS and JavaScript lunatic1 Follow Improve Article Tags : HTML JavaScript-Projects Geeks Premier League 2023 Similar Reads Create a Toggle Profile Card Details using HTML CSS and JavaScript In this article, we will develop an interactive Toggle Profile Card Details application using HTML, CSS, and JavaScript Language. In this application, we have a Card Component with the user's details like their profile picture and Name. There is a toggle button, and when the button is clicked, the d 3 min read Design a Navigation Bar with Toggle Dark Mode in HTML CSS & JavaScript One can create a navigation bar with a toggle switch for dark mode using HTML, CSS, and JavaScript. If we use the dark mode effect in our website then it can attract user attention and users can use our website in any situation. With the help of JavaScript, we'll implement the functionality to toggl 6 min read Slide Down a Navigation Bar on Scroll using HTML, CSS and JavaScript To create a slide down navigation bar you need to use HTML, CSS, and JavaScript. HTML will make the structure of the body, CSS will make it looks good. This kind of sliding navbar looks attractive on a site. By using JavaScript you can easily make the navigation bar slideable when the user scrolls d 4 min read Create a Mobile Toggle Navigation Menu using HTML, CSS and JavaScript To create a Mobile Toggle Navigation Menu you need HTML, CSS, and JavaScript. If you want to attach the icons with the menu then you need a font-awesome CDN link. This article is divided into two sections: Creating Structure and Designing Structure.A glimpse of complete Navigation: Creating Structur 3 min read How to create a Landing page using HTML CSS and JavaScript ? A landing page, also referred to as a lead capture page, static page, or destination page, serves a specific purpose and typically appears as a result of online advertising or search engine optimization efforts. Unlike a homepage, a landing page is stripped of distractions and focuses on capturing v 7 min read How to Create Tab Headers using CSS & JavaScript? Tab headers are a commonly used user interface element in web development. They provide a way to organize content into distinct sections, allowing users to switch between them easily. Each tab represents a different category or topic, and clicking on a tab displays the corresponding content while hi 2 min read Create ChatGPT Template using HTML CSS & JavaScript Create a customizable ChatGPT interface using HTML, CSS, and JavaScript. Implement features like message input, display area, and toggle buttons for themes. Customize styles and interactions to suit your preferences and requirements. Prerequisites:HTMLCSSJavaScriptApproachCreate a new HTML file and 7 min read Create a Music Website Template using HTML, CSS & JavaScript A Music Website Template is a pre-designed layout for creating a music-themed webpage. By utilizing HTML, CSS, and JavaScript, developers can craft a functional and visually appealing website with features like play/pause controls, sections for home, music, about, and contact.Step-by-Step Guide to c 3 min read Create a Single Page Application using HTML CSS & JavaScript In this article, we are going to design and build a cool and user-friendly single-page application (SPA) using just HTML, CSS, and JavaScript. A single-page application contains multiple pages which can be navigated or visited without loading the page every time. This makes things faster and more in 4 min read Design a Responsive Sliding Login & Registration Form using HTML CSS & JavaScript In this article, we will see how to create the Responsive Sliding Login & Registration Form using HTML CSS & JavaScript, along with understanding its implementation through the illustration.Many websites, nowadays, implement sliding login & registration forms for their sites. A Registrat 5 min read Like