How to Quickly Create a Sidebar with Bootstrap

How to Quickly Create a Sidebar with Bootstrap
Hello everyone, welcome back to porkaone. On this occasion we will learn how to make a cool sidebar using bootstrap. Curious?, let's follow in full below.



Currently, there are lots of sidebar examples that you can emulate, either using bootstrap or not using bootstrap. But if you are curious, this article can help you to create your own version of the sidebar.

On this occasion we will create a simple sidebar that can be toggled. Since it can be toggled here we will use a bit of javascript. I will provide the source code that is easy to understand for all of you. Please scroll to the bottom to see the final result.



How to Make Bootstrap Sidebar

1. Create a new file with the name index.html where you want it. Then copy the script below and run it in the browser.
    
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Bootstrap Demo</title> <!-- bootstrap 5 css --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha2/css/bootstrap.min.css" integrity="sha384-DhY6onE6f3zzKbjUPRc2hOzGAdEf4/Dz+WJwBvEYL/lkkIsI3ihufq9hk9K4lVoK" crossorigin="anonymous" /> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.9.1/font/bootstrap-icons.css" /> </head> <body> <div> <div class="sidebar p-4 bg-primary" id="sidebar"> <h4 class="mb-5 text-white">SuperVideo</h4> <li> <a class="text-white" href="#"> <i class="bi bi-house mr-2"></i> Dashboard </a> </li> <li> <a class="text-white" href="#"> <i class="bi bi-fire mr-2"></i> Populer </a> </li> <li> <a class="text-white" href="#"> <i class="bi bi-newspaper mr-2"></i> News </a> </li> <li> <a class="text-white" href="#"> <i class="bi bi-bicycle mr-2"></i> Sports </a> </li> <li> <a class="text-white" href="#"> <i class="bi bi-boombox mr-2"></i> Music </a> </li> <li> <a class="text-white" href="#"> <i class="bi bi-film mr-2"></i> Film </a> </li> <li> <a class="text-white" href="#"> <i class="bi bi-bookmark mr-2"></i> Bookmark </a> </li> </div> </div> <section class="p-4" id="main-content"> <button class="btn btn-primary" id="button-toggle"> <i class="bi bi-list"></i> </button> <div class="card mt-5"> <div class="card-body"> <h4>Lorem Ipsum</h4> <p> Lorem ipsum dolor sit amet consectetur adipisicing elit. Doloremque animi maxime at minima. Totam vero omnis ducimus commodi placeat accusamus, repudiandae nemo, harum magni aperiam esse voluptates. Non, sapiente vero? </p> </div> </div> </section> </body> </html>


If you run it, the design view will look like below. The display below is still very messy, for that we need to add css so that the sidebar is in the left position.
Membuat Sidebar dengan Bootstrap
Tampilan Tanpa CSS




2. Copy the css script below and paste it above the tag </head>.


<style>     li {     list-style: none; margin: 20px 0 20px 0; } a {     text-decoration: none; } .sidebar { width: 250px; height: 100vh; position: fixed; margin-left: -300px; transition: 0.4s; } .active-main-content { margin-left: 250px; } .active-sidebar { margin-left: 0; } #main-content {     transition: 0.4s; } </style>


3. When the script above is run again, the sidebar is not visible. To make it visible and toggled, we need to add javascript. Please copy and paste the script below before tagging </body>


<script>     // event will be executed when the toggle-button is clicked document.getElementById("button-toggle").addEventListener("click", () => {     // when the button-toggle is clicked, it will add/remove the active-sidebar class     document.getElementById("sidebar").classList.toggle("active-sidebar"); // when the button-toggle is clicked, it will add/remove the active-main-content class document.getElementById("main-content").classList.toggle("active-main-content");     }); </script>


We have run all the steps. It's time for us to try again. You can see the result as shown in the image below.
Bootstrap Sidebar
Hasil Akhir




Full Script 



<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Bootstrap 5 Side Bar Navigation</title> <!-- bootstrap 5 css --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha2/css/bootstrap.min.css" integrity="sha384-DhY6onE6f3zzKbjUPRc2hOzGAdEf4/Dz+WJwBvEYL/lkkIsI3ihufq9hk9K4lVoK" crossorigin="anonymous" /> <!-- custom css --> <!-- <link rel="stylesheet" href="style.css" /> --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.9.1/font/bootstrap-icons.css" /> <style> li { list-style: none; margin: 20px 0 20px 0; } a { text-decoration: none; } .sidebar { width: 250px; height: 100vh; position: fixed; margin-left: -300px; transition: 0.4s; } .active-main-content { margin-left: 250px; } .active-sidebar { margin-left: 0; } #main-content { transition: 0.4s; } </style> </head> <body> <div> <div class="sidebar p-4 bg-primary" id="sidebar"> <h4 class="mb-5 text-white">SuperVideo</h4> <li> <a class="text-white" href="#"> <i class="bi bi-house mr-2"></i> Dashboard </a> </li> <li> <a class="text-white" href="#"> <i class="bi bi-fire mr-2"></i> Populer </a> </li> <li> <a class="text-white" href="#"> <i class="bi bi-newspaper mr-2"></i> News </a> </li> <li> <a class="text-white" href="#"> <i class="bi bi-bicycle mr-2"></i> Sports </a> </li> <li> <a class="text-white" href="#"> <i class="bi bi-boombox mr-2"></i> Music </a> </li> <li> <a class="text-white" href="#"> <i class="bi bi-film mr-2"></i> Film </a> </li> <li> <a class="text-white" href="#"> <i class="bi bi-bookmark mr-2"></i> Bookmark </a> </li> </div> </div> <div class="p-4" id="main-content"> <button class="btn btn-primary" id="button-toggle"> <i class="bi bi-list"></i> </button> <div class="card mt-5"> <div class="card-body"> <h4>Lorem Ipsum</h4> <p> Lorem ipsum dolor sit amet consectetur adipisicing elit. Doloremque animi maxime at minima. Totam vero omnis ducimus commodi placeat accusamus, repudiandae nemo, harum magni aperiam esse voluptates. Non, sapiente vero? </p> </div> </div> </div> <script> // event will be executed when the toggle-button is clicked document.getElementById("button-toggle").addEventListener("click", () => { // when the button-toggle is clicked, it will add/remove the active-sidebar class document.getElementById("sidebar").classList.toggle("active-sidebar"); // when the button-toggle is clicked, it will add/remove the active-main-content class document.getElementById("main-content").classList.toggle("active-main-content"); }); </script> </body> </html>

 

Wow, isn't that cool?, after you know how to make a toogle sidebar, it's time for you to improvise so that your sidebar looks even more beautiful.

Ok, that's a quick tutorial on how to make a sidebar with bootstrap. May be useful. If there is something you want to discuss, please discuss it in the comments column below. That is all and thank you.

Post a Comment

0 Comments