9 Mar 2023

Creating a responsive sidebar menu with HTML, CSS, and JavaScript

Creating a responsive sidebar menu is a common feature for many websites and web applications. A sidebar menu provides a way to easily navigate through the pages of a website and access various sections or features. In this tutorial, we will show you how to create a responsive sidebar menu with HTML, CSS, and JavaScript.

Getting Started

Before we get started, make sure that you have a text editor and a web browser installed on your computer. You can use any text editor, such as Notepad, Sublime Text, or Visual Studio Code. For the web browser, we recommend using Google Chrome or Mozilla Firefox.

Create a new HTML file and add the following code:

<!DOCTYPE html>
<html>
<head>
    <title>Responsive Sidebar Menu</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <div class="sidebar">
        <a href="#">Home</a>
        <a href="#">About</a>
        <a href="#">Contact</a>
        <a href="#">Blog</a>
        <a href="#">FAQ</a>
    </div>
    <div class="content">
        <h1>Responsive Sidebar Menu</h1>
        <p>Welcome to my website!</p>
    </div>
    <script src="script.js"></script>
</body>
</html>

In this code, we have created a basic HTML document with a title, a link to a CSS file, and a script file. We have also added two main divs, one for the sidebar and another for the content. Inside the sidebar div, we have added a few links that represent the menu items.

Styling the Sidebar

Now, let's style the sidebar using CSS. Create a new file called style.css and add the following code:

.sidebar {
    background-color: #333;
    height: 100%;
    width: 0;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1;
    overflow-x: hidden;
    transition: 0.5s;
    padding-top: 60px;
}

.sidebar a {
    padding: 8px 8px 8px 32px;
    text-decoration: none;
    font-size: 25px;
    color: #818181;
    display: block;
    transition: 0.3s;
}

.sidebar a:hover {
    color: #f1f1f1;
}

.sidebar .closebtn {
    position: absolute;
    top: 0;
    right: 25px;
    font-size: 36px;
    margin-left: 50px;
}

.openbtn {
    font-size: 20px;
    cursor: pointer;
    background-color: #111;
    color: white;
    padding: 10px 15px;
    border: none;
}

.openbtn:hover {
    background-color: #444;
}

In this code, we have styled the sidebar with a background color, a fixed position, and a width of 0. This means that the sidebar will not be visible until we open it using a button. We have also added padding to the top of the sidebar to make room for the button and styled the links inside the sidebar.

Next, let's add a button to open the sidebar. Add the following code to your HTML file, just before the sidebar div:

<span class="openbtn" onclick="openNav()">&#9776; Menu</span>

This code adds a button with a hamburger icon and the text "Menu" to the top of the page. The onclick attribute calls a JavaScript function called openNav() when the button is clicked.

Adding JavaScript Functionality

Now, let's add the JavaScript code to open and close the sidebar to add JavaScript functionality to our responsive sidebar menu, create a new file called script.js and add the following code:

function openNav() {
    document.querySelector(".sidebar").style.width = "250px";
}

function closeNav() {
    document.querySelector(".sidebar").style.width = "0";
}

This code defines two functions, openNav() and closeNav(), that set the width of the sidebar div to either 250px (to open the sidebar) or 0 (to close the sidebar). We use the querySelector() method to select the sidebar div and modify its CSS style.

To make the sidebar menu responsive, we need to add some additional CSS code that will adjust the layout of the page depending on the screen size. Add the following code to your style.css file:

@media screen and (max-height: 450px) {
    .sidebar {padding-top: 15px;}
    .sidebar a {font-size: 18px;}
}

@media screen and (max-width: 768px) {
    .sidebar {width: 200px;}
    .content {margin-left: 200px;}
    .sidebar a {
        padding: 8px;
        font-size: 16px;
    }
    .sidebar .closebtn {
        font-size: 30px;
        top: 5px;
        right: 5px;
        margin-left: 0;
    }
    .openbtn {
        font-size: 20px;
        padding: 10px 15px;
    }
}

In this code, we use media queries to adjust the layout of the page for different screen sizes. When the screen height is less than 450px, we reduce the font size of the links in the sidebar. When the screen width is less than 768px, we set the width of the sidebar to 200px and move the content div to the right using the margin-left property. We also adjust the font size and position of the close button and the open button.

Conclusion

In this tutorial, we have shown you how to create a responsive sidebar menu using HTML, CSS, and JavaScript. We started by creating a basic HTML document with a sidebar and a content div. We then styled the sidebar with CSS, added a button to open the sidebar, and defined two JavaScript functions to open and close the sidebar. Finally, we made the sidebar menu responsive by using media queries to adjust the layout of the page for different screen sizes.

With this knowledge, you can now create your own responsive sidebar menus for your websites and web applications.