Unable to load the content

Please attempt to reload the page or alternatively, try accessing it using a different browser.

User-Friendly Navigation: Build a Collapsible Sidebar Menu using JavaScript

In the ever-evolving landscape of web design, creating a responsive and engaging user interface is paramount. One essential element that significantly contributes to a seamless user experience is the sidebar. A well-designed sidebar not only enhances the overall aesthetics of a website but also plays a crucial role in navigation.

User-Friendly-Navigation-Build-a-Collapsible-Sidebar-Menu using-JavaScript

Also Read: Typing Text Effect using HTML CSS and JavaScript | Without any library

This blog post will guide you through the process of building an animated sidebar using HTML, CSS, and JavaScript. We'll explore step-by-step instructions, providing insights into the structural markup, styling techniques, and the dynamic functionality that will bring your sidebar to life. 

HTML Structure 

The foundation of any well-crafted sidebar begins with a carefully structured HTML markup. In this section, we'll lay out the essential elements that form the backbone of our animated sidebar. Let's break down the HTML structure into key components.

 <aside>
    <div class="sidebar">
        <div class="header">
            <div class="toggle-btn bx bx-menu-alt-left"></div>
            <span class="logo">Smart UI Studio</span>
        </div>

        <ul class="menu">
            <li class="item">
                <a href="#"><span class="icon bx bxs-dashboard"></span><span class="name">Dashboard</span></a>
            </li>

  • <aside>: The HTML5 <aside> element is used to represent the sidebar section, providing a semantic structure to our layout.
  • .sidebar: This div encapsulates the entire sidebar content, including the header and navigation menu.
  • .header: Within the sidebar, the header section contains the toggle button and the studio logo.
  • .toggle-btn: This div, adorned with the "bx-menu-alt-left" icon, serves as the button to toggle the sidebar's visibility.
  • .logo: The logo or brand name is included for a personalized touch.
  • <ul class="menu">: The unordered list represents the navigation menu, which will contain various list items corresponding to different sections of the website.
  • <main>: This element represents the main content area of the webpage, positioned to the right of the sidebar.

CSS Styling

Creating an aesthetically pleasing and functional sidebar involves carefully styling each element. In this section, we'll go through the CSS rules to achieve the desired visual effects for our sidebar.

aside {
    position: fixed;
    top: 0;
    left: 0;
}

.sidebar {
    width: 4.1rem;
    height: 100vh;
    padding: .8rem;
    transition: .3s;
    background: #09101c;
    overflow: hidden scroll;
    font-family: 'Baloo Bhai 2', sans-serif;
}

  • .submenu.expanded: Reveals the submenu and adjusts visibility when it is expanded.
  • .bxs-chevron-down: Styles the chevron-down icon for submenu triggers. It is initially hidden.
  • .is-open .bxs-chevron-down: Displays the chevron-down icon when the sidebar is in the open state.
  • .item:has(.expanded) .bxs-chevron-down: Rotates the chevron-down icon when the submenu is expanded.
  • aside:has(.is-open)~main: Adjusts the width and left position of the main content area when the sidebar is open.

JavaScript Implementation

Now, let's move on to the JavaScript implementation, which is crucial for adding functionality to the sidebar, enabling toggle actions, and handling submenu interactions.

const btn = document.querySelector('.toggle-btn');
const sidebar = document.querySelector('.sidebar');
const trigger = document.querySelectorAll('.sb-trigger');
function showHideMenu() {
    sidebar.classList.toggle('is-open');
    const isOpen = sidebar.classList.contains('is-open');
    const listener = isOpen ? 'removeEventListener' : 'addEventListener';

    trigger.forEach(elem => {
        elem[listener]('click', toggleSubmenu);
        elem[listener]('click', showHideMenu); 

  • Initial Setup: Start by selecting the toggle button, the sidebar, and all submenu triggers.
  • showHideMenu  Function: It toggles the is-open class on the sidebar. It also dynamically adds or removes event listeners based on whether the sidebar is open or closed.

Also Read: Custom Volume Slider using HTML CSS and JS

Overall Flow

  • Clicking the toggle button (btn) triggers the showHideMenu function, toggling the sidebar's visibility and adjusting event listeners accordingly.
  • Clicking a submenu trigger (trigger) triggers both the toggleSubmenu and showHideMenu functions. Submenu visibility is toggled, and the sidebar's state is managed.

By understanding the code structure and working logic, you can grasp the fundamentals of creating interactive sidebars with expandable submenus. This implementation is not only functional but also serves as a foundation for more complex navigation systems in web development.

Feel free to experiment with the code, customize styles, and adapt it to your specific project needs. Enhancing user interfaces with dynamic components like this sidebar contributes to a more engaging and user-friendly web experience.

Download