What are some ways to keep the Bootstrap sidebar closed by default?

Is there a way to have the sidebar automatically collapsed when the page loads, rather than having to click on the collapse button? How can I achieve this?

The HTML structure:

 <div class="wrapper">
   <nav id="sidebar">
    <div class="sidebar-header">
      <button type="button" id="sidebarCollapse" class="btn btn-info">
          <i class="fa fa-bars"></i>                  
      </button>
  </div>

        <ul class="list-unstyled components">
          <li class="nav-item active">
           ----------------
        </ul>

    </nav>    
      <div id="base" class="col pt-2" style="overflow: auto;">

      </div>
    </div>
  </div>

The JavaScript function:

$('#sidebar').addClass('active'); // Add this line to have the sidebar collapsed by default

$('#sidebarCollapse').on('click', function () {
    $('#sidebar').toggleClass('active');
  });

  $("#sidebar .nav-item").on("click", function () {
    $("#sidebar .nav-item").removeClass("active");
    $(this).addClass("active");
  });  

The CSS styles:

    #sidebar {
        min-width: 80px;
        max-width: 80px;
        text-align: center;
    }
    #sidebar.active {
        margin-left: 0 !important;
    }
    #sidebar .sidebar-header h3,
    #sidebar .CTAs {
        display: none;
    }
    #sidebar .sidebar-header strong {
        display: block;
    }
    #sidebar {
        margin-left: 0;
    }
    #sidebarCollapse span {
        display: none;
    }
}

Answer №1

For those utilizing jQuery, consider the following approach:

$(document).ready(function() {
  $("#container").toggleClass("active");
});

An alternative is using HTML:

<div id="container" class="active">

Before posting any inquiries, please browse through the existing questions to see if yours has already been answered.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What are the methods for integrating a JavaScript framework in a Rails application without utilizing any gems?

I am seeking guidance on integrating frameworks such as angularjs, polymer js, bootstrap, etc., into a Rails application without relying on corresponding gems. The reason for this approach is that the availability of gems may change and some projects may ...

Place the child atop the stacked blocks

I need the .square div to always remain at the top, creating a visual effect where it appears as if the following section with its own .square is sliding up and the .square halts precisely at the position of the previous section's square. The code sn ...

What are some tips for efficiently troubleshooting a JQuery GET request to MailChimp 3.0 servers?

I am encountering an issue while trying to include users' emails in my Mailchimp 3.0 audience list. I am making a GET request using JQuery, but I keep running into the following error: {"type":"http://developer.mailchimp.com/documentation/mailchimp/g ...

The 'name' property of Axios and Vuex is not defined and cannot be read

When making a call to axios in my mounted function to retrieve profile data, I send the payload to the 'set_account' Vuex store upon success. To access this data, I utilize MapGetters (currentAccount) in computed properties. However, when tryin ...

Creating a Gulpfile.js that efficiently manages a multitude of files spread across various folders

For my ongoing WordPress projects, I am simultaneously working on themes and plugins. In my theme folder, I have a gulpfile.js that compiles .scss files into .css. I am considering creating a central "master" gulpfile in the root folder to compile .scss fi ...

Error in Javascript: Null Object

I have created an upload page with a PHP script for AJAX, but I'm encountering errors in Firebug. Also, the upload percentage is not being returned properly. Currently, I can only do one upload in Firefox and the script doesn't work in Chrome. H ...

modifying the state in a stateless React component

Hello everyone! I am here trying to work with hooks and I'm not sure how to achieve my goal. Here is the code I have: import React, { useState } from 'react' import { useForm } from 'react-hook-form' import {useDispatch } from &a ...

Utilizing a variable for choosing a specific element within a JSON array

Can a specific element within a JSON array be selected by providing a variable? Let's imagine there are numerous users: "users": [ { "id":"000000", "uname": "admin", "password": "admin", "phash": "b109f3bbbc244eb82441917ed06d618b9008dd09b3befd1b ...

Unable to identify collision in JavaScript's 3D environment

I'm currently using three.js to develop a simulation of Brownian Motion, but I've hit a roadblock when it comes to getting the tiny molecules to collide with each other. This is the snippet I have at the moment: function intersects(sphere, other ...

Error Message: Uncaught TypeError - Trying to access properties of an undefined variable

Can anyone help me understand why I am encountering this error message? I am trying to pass the setCookie function to ParseUserCookie in order to handle multiplayer click, but I keep receiving an error that says "Uncaught TypeError: Cannot read properties ...

What is causing my div exchange using .class to fail?

Struggling to convert this code from working with IDs to Classes. The code reveals one Div while hiding another based on the onClick event. Originally, it was straightforward because the div location was absolute. In the classes version, I need to revea ...

What is the best way to position an image beside a jquery dialog box?

Apologies for not finding a suitable topic to discuss. Is it possible to place an image next to a jQuery dialog, similar to the following example? If so, how can this be achieved? ...

Using Prototype Library to Implement the Nth-child Selector

I've been experimenting with using "nth-child(n)" in Prototype, similar to how it's done in jQuery. See the example code below... function myFunction() { $$('div.amfinder-horizontal td:nth-child(1) select').simulate('click'); ...

Does adding the Angular bootstrap script at the end of the body tag impact webpage speed more than using ng-app on the body tag?

In the past, we had placed ng-app on the body tag which led to problems with dependency injection when multiple modules were being used on the same webpage. This required module creators to be aware of the existing app and inject their app into it. We are ...

ways to assign active state to navigation menu using Bootstrap

I am just starting to work with Bootstrap and I have a question about setting the active class in their navigation menus. Here is my menu structure: <li class="nav-parent"> <a class="nav-link" href="#"> ...

Steps for Implementing an Event Listener in JavaScript

While working on a page in Chrome, I encountered an issue where I wanted a modal to show up when a user clicked on an image. However, the functionality was not working as expected on my localhost. Upon further inspection, I believe there might be a problem ...

Steps for displaying a confirmation popup and redirecting to a new route using AngularJS

Is it possible to display a confirmation popup with "Yes" and "No" buttons every time there is a route change in my AngularJS App? I attempted the following approach. This event gets triggered before the route change, but despite selecting 'NO', ...

JavaScript implementation of Ancient Egyptian Multiplication using PHP code

Let's dive into the algorithm. If we have two integers, A and B (remember, only integers), that need to be multiplied, here is how it works: we continuously multiply A by 2 and divide B by 2 until B cannot be divided any further, or in other words, un ...

"Unraveling Vue.js: A guide to fetching JSON data one object at a time

I am facing a challenge with a large JSON file (40 MB) that includes data on countries, their IDs, and a total sum that I need to calculate. { "0": { "id": 0, "country": "usa", "sum": 201, }, ...

Preventing multiple clicks by toggling the HTML tag on and off

Here is the jQuery structure that I am currently working with: $(document).on('click', '.view-details', function () { $(this).prop("disabled", true); // API call1 // API call2 // API call3 $(this).prop("disabled" ...