The content section sits discreetly behind the sidebar

Upon loading my Bootstrap 5 webpage, the toggle button successfully moves the navbar and body section to show or hide the full sidebar. However, an issue arises where the body section goes behind the sidebar when using the toggle button.

Below is a portion of the html file:

{% extends "base.html" %}
{% block content %}

<!-- Sidebar section -->
<nav class="navbar navbar-expand d-flex flex-column align-item-start" id="sidebar">
  <div class="p-4 pt-5">

<!-- User image -->
  <div class="text-center">
    <img src="{{url_for('static',filename='profile_pics/two.jpg')}}" class="rounded" alt="Profile">
  </div>

 <!-- Navbar menus -->
  <ul class="navbar-nav d-flex flex-column mt-5 w-100">
      <li class="nav-item dropdown w-100">
        <a href="#equitiesSubmenu" class="nav-link dropdown-toggle text-light pl-4" id="equitiesSubmenu" role="button" data-bs-toggle="collapse" aria-expanded="true">Equities</a>
        <ul class="collapse lisst-unstyled" id="equitiesSubmenu">
            <li><a href="{{url_for('core._simulator')}}" class="dropdown-item text-light pl-4 p-2">Simulator</a></li>
            <li><a href="{{url_for('core.portfolio')}}" class="dropdown-item active text-light pl-4 p-2">Portfolio</a></li>
        </ul>
      </li>       
  </ul>

  <br>
</nav>

<!-- Container for the body page -->
  <section class="my-container">
    <div class="row">
      <div class="col-md-12">
        <div class="bg-light p-3">
<!-- Container for the toggle button and topbar -->
          <div class="container">
            <div class="row">

<!-- Bootstrap Icons -->
              <div class="col">
                <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="395b56564d4a4d4b584914505a56574a7908170c1709">[email protected]</a>/font/bootstrap-icons.css">

<!-- Sidebar Toggle Button -->
                <button class="btn btn-primary float-start" id="menu-btn">
                  <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-list" viewBox="0 0 16 16">
                    <path fill-rule="evenodd" d="M2.5 12a.5.5 0 0 1 .5-.5h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5zm0-4a.5.5 0 0 1 .5-.5h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5zm0-4a.5.5 0 0 1 .5-.5h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5z"/>
                  </svg>
                </button>
              </div>
 
            </div>
          </div>

</section>
<!-- JS To Activate Sidebar -->
    <!-- bootstrap js -->
    <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="63130c131306114d091023524d52554d52">[email protected]</a>/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
    <!-- JavaScript Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d2b0bdbda6a1a6a0b3a292e7fce2fce3">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4" crossorigin="anonymous"></script>
    <!-- custom js -->
    <script>
        var menu_btn = document.querySelector("#menu-btn")
        var sidebar = document.querySelector("#sidebar")
        var container = document.querySelector(".my-container")
        menu_btn.addEventListener("click", () => {
            sidebar.classList.toggle("active-nav")
            container.classList.toggle("active-cont")
        })

    </script>

{% endblock %}

This portion involves the style.css:

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box; }

body {
    min-height: 100vh;
    background-color: #fff; }

.navbar {
    width: 250px;
    height: 100vh;
    position: fixed;
    background-color: #212529;
    transition: 0.4s; }

.nav-link {
    font-size: 1.25em; }

.nav-link:active, .nav-link:focus, .nav-link:hover {
    background-color: #c0c2c5; }

.dropdown-menu {
    background-color: #be1ca9; }

.dropdown-item:active, .dropdown-item:focus, .dropdown-item:hover {
    background-color: #a81219; }

.my-container {
    transition: 0.4s;
    margin-left: 250px; }

/* for navbar */

.sidebar {
    z-index: 0; }

.active-nav {
    margin-left: 0; }

/* for main section */

.active-cont {
    margin-left: 0px;
    z-index: 1; }

.footer p {
    color: rgba(255, 255, 255, 0.5); }

The webpage appears as shown below initially before toggling the sidebar:

https://i.sstatic.net/bTDkA.png

After toggling to hide the sidebar, the webpage looks like this:

https://i.sstatic.net/NkjNr.png

Answer №1

To shift your .my-container to the right, simply apply a margin-left of 250px

When you add the .active-cont class to the .my-container, it will have a margin-left value of 0

If you toggle the same class on the .sidebar, it will not move due to lack of margin adjustment and will remain in its original position.

An easy solution is to assign a default margin: -200px (or the sidebar width) to the .sidebar. This way, both elements will shift when the class is toggled.

Alternatively, consider giving the .active-cont a z-index: 1 to resolve the issue as well.

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

Switching from the .html extension to the absence of .html in Angular

I currently have this htaccess file set up: <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] #R ...

I would like to know how to create a dropdown menu that shows the country name, flag, and code based on the

I have successfully generated the telephone input field. One requirement is to display the Country flag along with the country name as soon as the dropdown is selected. Another requirement is that once the country is selected, it should display the countr ...

"Is there a way for me to retrieve the response header within the .then function in AngularJS

How can I retrieve the response header from a GET request? I have a service function that returns a promise. In my Controller, I resolve the promise and in the .then function, I need to access the content type from the response header. I attempted to use ...

Is there a way for me to immediately send data after receiving it?

When I try to perform onPress={() => kakaoLosing() I am attempting to retrieve data (profile) from getProfile using async await and immediately dispatch that data to KAKAOLOG_IN_REQUEST, This is my current code snippet: import { ...

Asserting types for promises with more than one possible return value

Struggling with type assertions when dealing with multiple promise return types? Check out this simplified code snippet: interface SimpleResponseType { key1: string }; interface SimpleResponseType2 { property1: string property2: number }; inter ...

Retrieve the scrollTop, scrollLeft properties, and other scroll-related data from an element using Selenium

When testing a Python/Django element that is scrolled using scrollTop, I am trying to retrieve the value of scrollTop. In JavaScript, I can access this value with: element.scrollTop I attempted to do this in Python using: element.get_attribute('sc ...

What is the best way to export assets into a React Native JS file?

After watching a video on YouTube, I noticed that at 3:20 the individual in the video quickly exported multiple assets into a file without providing any explanation. Can someone please view this specific moment in the video and clarify how this person mana ...

What is the best way to conceal content within a URL while still transmitting it to the server using node.js and express?

I have been trying to figure out how to hide certain content from the URL, but still need to send that information to the server in my Express app. So far, I haven't found any solutions that work. For example, if I have a URL like www.abc.com/viewblo ...

Material Design Forms in Angular: A Winning Combination

I'm currently working on developing a form using Angular Material. This form allows the user to update their personal information through input fields. I am utilizing "mat-form-field" components for this purpose. However, there are certain fields tha ...

The title element is persistently appearing on the same line

As a beginner, I've created a document with a 3x3 checkerboard using HTML and CSS. However, I'm facing an issue where the heading element is not displaying on a separate line as expected. I believe the problem lies in the CSS styling, specifical ...

Error Message: React encountered an issue when trying to access the 'theme' property of an undefined object

Currently developing a web application using React, I encountered an issue when trying to implement the react-datepicker component in my code. Upon adding the following lines of code, my web application breaks and an error is thrown: import {SingleDatePic ...

Transform an asynchronous callback into an asynchronous generator format

I have the following function from a third-party package that I am unable to modify async function runTransaction(callback) { const client = await createClient(); try { await client.query("BEGIN"); await callback(client); } ...

Is it possible to have a field automatically calculate its value based on another field?

Consider the following data structure: { "rating": 0, "reviews": [ {"name": "alice", rating: 4}, {"name": "david", rating: 2} ] } What is the best way to recalculate the overall rating when new reviews are added or existing reviews are upda ...

Looking to merge two components into one single form using Angular?

I am currently developing an Angular application with a dynamic form feature. The data for the dynamic form is loaded through JSON, which is divided into two parts: part 1 and part 2. // JSON Data Part 1 jsonDataPart1: any = [ { "e ...

Navigational elements, drawers, and flexible designs in Material-UI

I'm working on implementing a rechart in a component, but I've encountered an issue related to a flex tag. This is causing some problems as I don't have enough knowledge about CSS to find a workaround. In my nav style, I have display: flex, ...

Tips for displaying buttons and images on the same line using CSS

Below is the JavaScript code I am using: $('#needdiv').append('<img src="/images/Connect.png" id="connectimage">' + '</img>' + '<input type="button" id="connect" value=connect >' + '&l ...

CSS Tips: Defining Pseudo-Element Sizes

Currently, I am trying to adjust the width of the :before pseudo-element in order to match it with the dimensions of the element itself, specifically an anchor tag. This is part of my goal to create an overlay that completely covers the entire element. As ...

Issue with React hook state persistence in recursive function

I implemented a recursion custom hook that utilizes a setTimeout function to provide 3 chances for an operation. Once the chances run out, the recursion should stop. However, I encountered an issue where the setTimeout function is not properly decrementin ...

Is there a way to utilize an AXIOS GET response from one component in a different component?

I'm having trouble getting my answer from App.tsx, as I keep getting an error saying data.map is not a function. Can anyone offer some assistance? App.tsx import React, {useState} from 'react'; import axios from "axios"; import {g ...

Submitting both $_POST[] data and $_FILES[] in a single AJAX request

Struggling with uploading images along with dates on my website. I am passing the date using $_POST and the image files in $_FILES. Here is the Javascript code snippet: (function post_image_content() { var input = document.getElementById("images"), ...