How can I create a fixed sidebar that remains underneath the navbar using Bootstrap 4?

I am currently working on a website that features a Bootstrap 4 navbar at the top, with the class "sticky-top". This navbar does not have a fixed height. Below the navbar, there is a container-fluid div containing a sidebar on the left and a main content div on the right. I want the sidebar to extend the full height of the page, from the bottom of the navbar to the bottom of the window, while remaining sticky as the user scrolls through the main content.

I have been experimenting with various CSS properties like "position: sticky", but have yet to find the correct combination to achieve the desired effect. Do you have any tips or suggestions for me?

Update:

Below is a snippet of my current custom.css file where I have been making adjustments:

.wrapper { 
    display: flex; 
    width:100%; 
    align-items: flex-start; 
 /*   align-items: stretch;*/ 
} 
 
#sidebar { 
    min-width:250px; 
    max-width:250px; 
    background-color: #f8f9fa; 
    position:sticky; 
    bottom:3rem; 
    align-self:flex-start; 
}

Here is an excerpt of the HTML code related to the layout:

<nav class="navbar navbar-expand-lg navbar-light bg-light sticky-top">
   <a class="navbar-brand mr-0 mr-md-2">Tuesday</a>

   [more navbar items here]
   
        
<div class="container-fluid">
    <div class="wrapper">
        <div id="sidebar">
        <br>
        <div class="dropdown dd-custom">
            <a href="#" class="dropdown-toggle di-custom" data-toggle="dropdown">Bills</a>

             [more sidebar content]

<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main text-mainframe main-custom">

[main content section]

Answer №1

After struggling with creating a sticky sidebar below the navbar in bootstrap 4, I finally found a solution that works for me and I am excited to share it with you. Maybe someone else out there is looking for the same functionality.

The key was adding an id to the navbar and setting it as sticky-top.

<nav id="complete_navbar" class="navbar navbar-expand-lg navbar-light bg-light sticky-top">

I then made some adjustments to my containers to accommodate the sidebar.

 <div class="container-fluid" style="padding-left:0;">
            <div class="wrapper">
                <div id="sidebar" style="min-height:calc(100vh - document.getElementById('complete_navbar').clientHeight);">
                    <div style="position:fixed; margin-top:document.getElementById('complete_navbar').clientHeight;">

In the code snippet above, I used a calculation to adjust the sidebar's height based on the navbar's height. This ensures that the sidebar fits below the navbar without overlapping the main content. I also added another div inside the sidebar that is fixed and aligned with the navbar's height.

While I'm not a Javascript expert, I'm thrilled that this solution worked after days of frustration. Hopefully, this will be helpful to others facing similar challenges in the future.

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

Chrome/IE displaying text indentation issue within nested divs

Encountering issues with nested divs in Chrome and IE, while Firefox works smoothly. The outer div has both text-indent and margin-top styles set. The inner div's display style is block, so the content after it should appear on a new line without any ...

Creating a custom event for every reference within a Vuejs for loop

I'm working with a loop of data and have a modal for each iteration <div ref="vuemodal-{{loop.index}}"> Each modal is a bootstrap modal, and I want to bind an event to them that triggers whenever the modal is closed mounted(){ Obj ...

Using scope in ng-style to manipulate a portion of a CSS property value in Angular

I've been attempting to add a border using ng-style, but I'm struggling to figure out how to concatenate the value from the scope variable. None of the methods below seem to be working for me: <div ng-style="{'border-top' :'1p ...

Manipulating CSS content property in React using Material UI

I am trying to set content: "" on my i element: "& i::before": { content: "" }, "& i::after": { content: "" }, However, the style is not being applied and I am seeing an ...

I am dealing with a set of divs wrapped in a flex container that tend to overlap each other. Their heights vary and sometimes they wrap multiple times. Any suggestions on how to prevent this

I am encountering an issue where the width of the div remains the same after it has wrapped, causing overlapping. I want the first div to push the second to the right and so on. When I add a 100% width, it makes the width for the smallest div the same as ...

Are there alternative methods to improve the responsiveness of a column layout?

Check out this codesandbox I created to display my current status. When you change the column-count to three, a significant amount of information becomes hidden because it doesn't scroll. I've attempted using grid and flexbox layouts, but everyt ...

The smooth scrolling feature fails to function properly in Lenis when clicking on links with specified IDs

Is there a reason why the scroll-behavior: smooth property doesn't function properly when Lenis library for smooth scrolling is included? It seems to work fine when the Lenis code is commented out, but once it's added back in, the scrolling isn&a ...

employing the CSS `:empty` pseudo-class to conceal an element

Upon using chrome for inspection, I stumbled upon the following code snippet: <div class="entry-content"> <p>We .... </p> </div> <footer class="entry-footer"> </footer> Occasionally, this f ...

Tips for addressing the issue of button flickering due to CSS transitions

Is there a solution to prevent flickering without compromising the intended design? The issue arises when hovering over a moving or animated element and accidentally un-hovering because it moves beneath the cursor. <!DOCTYPE html> <html> &l ...

The Tailwind preset is generating CSS code, but the webpage is not displaying the intended styles

Can anyone explain why the preset is generating CSS in the output file, but the styles are not displaying in the browser? When I manually write CSS in newstyle.css, it gets outputted to output.css and renders correctly in the browser. I attempted adding t ...

Maintain the background div while scrolling horizontally

I am facing an issue with my wide bar chart where I use iScroll to enable horizontal scrolling. Behind the bar chart, there are horizontal lines that should move along in the background as the user scrolls horizontally. The challenge is to keep the lines v ...

Populate your website with both a Bootstrap Popover and Modal for interactive user engagement through hover

Here's the situation: I want to showcase a user's name with a popover that reveals a snippet of their profile information. I've got that part down, where it dynamically generates and displays the popover content as needed. The popover functi ...

Properly align and position the background of the header image

I'm currently tackling an issue on my website where I need help aligning and positioning the background image in the main header section. My goal is to ensure that the image is fully visible in this area, even when viewed on mobile devices. Attached ...

Alter color upon click using jQuery

I am facing an issue with my code where I want to make it so that when a user clicks on one of the glyphs on the side of the page, elements with ids starting with the same letter will remain black while everything else, including other glyphs, turn grey. ...

Is there a solution or workaround for the issue of fake anti-aliasing in Microsoft Edge when it comes to the border of sticky headers using Bootstrap 4?

In my current project, I came across a Pen shared by Balaji731 on Bootstrap 4 table with the scrollable body and header fixed. While testing the Pen in Microsoft Edge, I noticed that when <th> borders are enabled, they disappear while scrolling, cre ...

Creating a Javascript function to turn lights off using CSS manipulation, similar to the feature found

Is there a way to use JavaScript to obscure all elements on a page except for one specific HTML element? This web application is optimized for Chrome, so CSS3 can also be utilized. ...

HTML links remain enclosed in a box even after the code has been finalized

Hey there, I have written this code and for some reason, every link shared after it is displaying with a black background. I'm new to coding and can't seem to figure out what I'm doing wrong. Any help would be greatly appreciated. a:link, ...

Ways to customize the scrollbar appearance in a ul element

I need help with styling the scroll bar for images placed within divs in a ul tag. When there are more than 3 images, a scroll bar appears. Is it possible to apply custom styles to the scroll bar? ...

Tips for adjusting the color of multi-line text when hovering with the mouse

I'm facing a challenge in changing the color of text when someone hovers over it, but so far I've only been able to make it work if the mouse scrolls over the top border of the text. The text I'm testing is located in the top left corner an ...

Create a container div with a set width and height, and use

When wrapping a div, I typically rely on the overflow:hidden method. However, I am wondering if there are alternative methods to wrap divs that do not involve using hidden overflow. This is because I have some specific concerns that prevent me from utili ...