Design a Toggleable Sidebar using HTML CSS and JavaScript Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 1 Likes 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: Create Quiz Design a Toggleable Sidebar using HTML CSS and JavaScript Comment L lunatic1 Follow 1 Improve L lunatic1 Follow 1 Improve Article Tags : HTML JavaScript-Projects Geeks Premier League 2023 Explore HTML BasicsHTML Introduction4 min readHTML Editors4 min readHTML Basics7 min readStructure & ElementsHTML Elements4 min readHTML Attributes7 min readHTML Headings3 min readHTML Paragraphs3 min readHTML Text Formatting4 min readHTML Block and Inline Elements3 min readHTML Charsets4 min readListsHTML Lists3 min readHTML Ordered Lists5 min readHTML Unordered Lists4 min readHTML Description Lists3 min readVisuals & MediaHTML Colors11 min readHTML Links Hyperlinks2 min readHTML Images7 min readHTML Favicon4 min readHTML Video4 min readLayouts & DesignsHTML Tables9 min readHTML Iframes4 min readHTML Layout4 min readHTML File Paths3 min readProjects & Advanced TopicsHTML Forms4 min readHTML5 Semantics5 min readHTML URL Encoding4 min readHTML Responsive Web Design11 min readTop 10 Projects For Beginners To Practice HTML and CSS Skills8 min readTutorial ReferencesHTML Tags - A to Z List5 min readHTML Attributes Complete Reference8 min readHTML Global Attributes5 min readHTML5 Complete Reference8 min readHTML5 MathML Complete Reference3 min readHTML DOM Complete Reference15+ min readHTML DOM Audio/Video Complete Reference2 min readSVG Element Complete Reference5 min readSVG Attribute Complete Reference8 min readSVG Property Complete Reference7 min readHTML Canvas Complete Reference4 min read Like