Open In App

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.

Screenshot-2023-11-17-155942
Preview

Approach

  • First, 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 JavaScript

Output:


Next Article

Similar Reads