Adapt your Bootstrap sidebar for icon menu on small screens below 800px

My Background

My experience lies primarily in PHP rather than modern CSS, particularly Media Queries. This has left me feeling uncertain about how to effectively troubleshoot CSS layout issues.

Currently, I am working on developing a user dashboard template by following various tutorials. The key requirements for this template are:

  1. The main content area should be bootstrap responsive
  2. The sidebar must have the ability to collapse into a compact icon menu
  3. If the screen size is less than 800px, the sidebar menu should automatically collapse
  4. When the screen size is below 800px and the sidebar is collapsed, it should still be expandable

What I've Managed So Far:

$(document).ready(function() {
        $('#sidebar-btn').click(function() {
            $('#sidebar-wrapper').toggleClass('visible');
            $('#wrapper').toggleClass('visible');
        });
    });
/* navbar */
.navbar {
    margin-left:50px;
}

/* main content */
#page-content-wrapper {
    width:100%;
    position:absolute;
    padding:15px;
    padding-top:50px;
}
/* more CSS styles ... */ 
<!-- Latest compiled and minified CSS -->

<script src="https://use.fontawesome.com/3c2244d372.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

The Question at Hand

Point four is currently presenting an issue for me. While I can successfully collapse the sidebar into an icon menu upon resizing the screen, I struggle to expand the menu again when in small mode. How can I adjust the CSS specificity so that toggling the visible class allows me to view the menu even on a small screen?

Answer №1

It appears that there may be a code issue when trying to display #sidebar-wrapper with a maximum width of 800px on the screen. You have set up an @media rule like this:

@media screen and (min-width: 800px)

You have nested all the sidebar displaying code within this rule, which means that Jquery will only expand the sidebar when the screen is larger than 800px.

Consider doing the same for

@media (max-width: 800px)

However, it seems that in this case, media queries might not be necessary since you want the sidebar to expand on all screen sizes.

UPDATE
Replace your current JS code with this:

$(document).ready(function() {
 if (window.matchMedia('(max-width: 800px)').matches) {

   $('#sidebar-wrapper').removeClass('visible')
   $('#wrapper').removeClass('visible');

   $('#sidebar-btn').click(function() {

     $('#sidebar-wrapper').toggleClass('visible');
     $('#wrapper').toggleClass('visible');
   });  
 } else {
     $('#sidebar-btn').click(function() {

       $('#sidebar-wrapper').toggleClass('visible');
       $('#wrapper').toggleClass('visible');
     });  
   }
});

Also, make sure to remove this line:

@media screen and (min-width: 800px) {

and remember to remove the closing curly brackets at the end.

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

Ways to dynamically update a div with data from a JSON response

I'm currently in the process of developing a search platform. I have three static divs on the search results page that display certain content, all containing similar code. For example: <div id="result" class="card"> <img src="hello.png" ...

Bootstrap 4 and Angular 7 application experiencing issues with toggle button functionality in the navigation bar

I am currently working with Angular7 and Bootstrap4. I am attempting to integrate a navbar into my application, but I am facing an issue where the toggle button does not work when the navbar is collapsed. It is important for me to mention that I do not wa ...

How to modify a variable within the "beforeSend: function()" using Ajax and jQuery

I am facing an issue with my ajax contact form. The beforeSend function is supposed to validate the inputs and pass the parameters to a PHP file for sending emails. However, I always receive false instead of true even when the inputs are valid. I suspect ...

Using jQuery or JavaScript to trigger a specific popup when clicking on an email link

I am looking to accomplish the following: The user will receive an email with a link. When they click the link from the email, it should take them to the homepage of the site where there are links. Clicking on a link should trigger a popup. However, I w ...

Placing text in a box in the corner while maintaining the ability to scroll

I'm looking for a solution to wrap text around a toolbox while keeping the total height limited and allowing the text to scroll. The challenge is to have the corner box stay fixed in the corner without scrolling along with the text. How can I achieve ...

"Encountering issue with $.ajax() not accepting PHP array as

I am currently utilizing ajax to fetch data from a table stored in a MySQL database. The ajax calls are being made using jQuery, with the databases being accessed through PDOs. PHP function fetchQuestions() { $database = "answerMe"; // n ...

Choosing data based on specific conditions in a database using PHP when the $_POST array is empty and contains a

I am having trouble selecting data from my database using PHP and an ajax call function. In the select function, I am using $_POST to retrieve data from a textbox. Even though I have set up a PHP and ajax call function on my page, something seems to be goi ...

How can I align two div buttons at the top and bottom to appear on the left and right sides?

How can I change the position of two buttons from top and bottom to left and right as shown in the second image? I am utilizing Floating Vue, so the buttons will be contained within a div. Is it feasible to adjust their position accordingly? Check out th ...

PHP function fails to retrieve AJAX post data

I'm struggling with some code in my javascript. Here's what I have: data = {}; data.token = '880a505d0a86b2sfwe434fdg43gwsf'; url = '/test/ajax/'; $.ajax({ url: url, type: "POST", d ...

What causes a horizontal line to form when a user enters white space?

I have written a piece of code which seems to be causing an issue when running it. Whenever I input empty spaces, it results in creating a horizontal line. import React, { Component } from 'react'; export default class App extends Component { co ...

Using JavaScript to create CSS animations triggered on hover

Looking for a CSS Animation that will play forward once when the mouse enters a specific div, and then play in reverse when the mouse leaves. Check out my JsFiddle here. The div with the class ".item" should trigger the mouse enter event. The animation co ...

Which specific HTML element triggers the initiation of ajax in Jquery?

I have a situation where various HTML elements trigger ajax requests when clicked. My goal is to determine which specific element was clicked inside the ajaxComplete event. Although I attempted to use event.target, it only returns the entire document inst ...

Using JQuery to send an array to PHP and save it to a file

I am currently working on a project where I write test code that posts an array to a PHP page. The PHP page needs to write this array to a file in order to check the data. Here is the JQuery code snippet: $("#Trash").click(function () { $.post("tests ...

What is the best method to eliminate the .invisible div entirely from your website?

I want to hide a select tag and only display it when necessary. To achieve this, I tried using the .invisible class from Bootstrap, but the issue is that even when hidden, it still takes up space. <div class="row invisible"> <div class="fo ...

Loading Bootstrap accordion content with AJAX only when clicked

Is there a way to load ajax content into an accordion only when it is active? This could help prevent unnecessary data loading. It would be helpful to have a spinner displayed while the content is loading. I've searched online but haven't found a ...

Assistance needed: How to Use Ajax-JavaScript in Django to Activate or Deactivate Status in Database

I am currently working on implementing a feature using Ajax-JavaScript where clicking on the 'Activate' option will change the value in the database for that specific field to 'Active'. This new status will then be displayed as 'Ac ...

What is the best way to anchor the components to a specific location on the screen in ASP.NET?

Currently I am working on creating a registration page in asp.net. I have been using panels to group the components such as labels, dropdown lists, and text boxes. However, when I run the page, I noticed that the positions of these components keep changing ...

Troubleshooting: jqGrid is not displaying the requested JSON information

I am implementing jqGrid in an ASP.NET page. Here is the structure in the .aspx page: <div id="divMy" style="width: 100%; overflow: auto;"> <div id="divMyTable" style="width: 97%; display: none;"> <table id="MyTable"> ...

Error: Attempted to bootstrap Angular multiple times

While developing an app using angularjs, everything functions correctly after loading a web page. However, a message appears on the console saying: WARNING: Tried to load angular more than once. Upon checking the angular.js file, I found this code snippe ...

Using JavaScript, generate ten clickable circles on the screen. Each circle should display the number of times it has been clicked in the center when interacted with

I am currently working on my JavaScript skills and would like to implement a fun project involving 10 colorful circles. While I can easily create these circles using HTML and CSS, I require assistance with the interactive functionality using JavaScript. ...