How to create a Collapsed Sidebar?
Bootstrap Sidebar is a component that is used for vertical navigation. It can be customized in various styles according to your requirement by using CSS and can be made responsive by using JavaScript.
Creating a Simple SideBar:
The .sidebar class is used to create simple Bootstrap sidebar. To make a collapsing sidebar, you need to have a bit of JavaScript Knowledge as well, as it would be used to open and close the sidebar, would make your sidebar responsive.
Example:
<html> <head> <style> /*Position and style for the sidebar*/ .sidebar { height: 100%; width: 0; position: fixed; /*Stays in place */ background-color: green; /*green*/ overflow-x: hidden; /*for Disabling horizontal scroll */ } /* Position and style for the sidebar links */ .sidebar a { padding: 10px 10px 10px; font-size: 25px; color: #111; display: block; transition: 0.3s; } /* the links change color when mouse hovers upon them*/ .sidebar a:hover { color: #FFFFFF; } /* Position and style the for cross button */ .sidebar .closebtn { position: absolute; top: 0; right: 25px; } /* Style for the sidebar button */ .openbtn { font-size: 32px; background-color: #008000; color: #111; padding: 10px 10px 10px; border: none; } /* the sidebar button changes color when mouse hovers upon it */ .openbtn:hover { color: #FFFFFF; } /* pushes the page content to the right when you open the side navigation */ #main { transition: margin-left .5s; /* If you want a transition effect */ padding: 10px; } </style> </head> <body> <div id="sidebar" class="sidebar"> <a href="javascript:void(0)" class="closebtn" onclick="closeNav()"> × </a> <!--the cross button--> <a href="#">India</a> <a href="#">Nepal</a> <a href="#">Srilanka</a> <a href="#">Myanmar</a> </div> <div id="main"> <button class="openbtn" onclick="openNav()"> SIDEBAR </button> <!-- for the sidebar button--> </div> </body> <script> /* Sets the width of the sidebar to 250 and the left margin of the page content to 250 */ function openNav() { document.getElementById( "sidebar").style.width = "250px"; document.getElementById( "main").style.marginLeft = "250px"; } /* Set the width of the sidebar to 0 and the left margin of the page content to 0 */ function closeNav() { document.getElementById( "sidebar").style.width = "0"; document.getElementById( "main").style.marginLeft = "0"; } </script> </html> |
Output:

By running these three codes you can make a simple collapsing sidebar. You can further add more CSS features to make it even better according to your requirement.
Recommended Posts:
- How to create an unordered_map of pairs in C++?
- How to create a copy of an object in PHP?
- How to create a style tag using JavaScript?
- Different ways to create an Object in C#
- How to create an array for JSON using PHP?
- How to create a div element in jQuery ?
- How to create Radial Menu in CSS ?
- jQuery | Create a div element
- How to create a link in JavaScript ?
- PHP | Collator create() Function
- How to create an array with key value pairs in PHP?
- How to create a shallow copy of Hashtable in C#
- How to create directory with HTML button and PHP ?
- Create a Weather app using Flask | Python
- How to create a function from a string in JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



