How to keep the bottom of a CSS {position:sticky; top:0} element within the viewport without using JavaScript

I am working on creating a dashboard frame using Bootstrap 5, and I aim to achieve the functionality shown in these two images:

Here is my current setup:

<html>
  <body>
    <div class="pretoolbar">pretoolbar (non-essential information, hidden when scrolling)
    </div>
    <div class="sticky-toolbar"> sticky toolbar</div>
    <div class="container-fluid">
      <div class="sidebar col-3">
        [content for tall sidebar]
      </div>
      <main class="col-9">
        [also tall content]
      </main>
    </div>
  <body>
</html>

CSS:

.pretoolbar{
    background-color: #555;
    color:white;
    height:32px;
}
.sticky-toolbar{
    background-color: black;
    color:white;
    height:56px;
    position:sticky;
    top:0;
}
.sidebar{
    background-color: white;
    position: sticky;
    top: 56px;
    overflow: hidden auto;
    max-height: calc(100vh - 56px);
    height: 100%;
}

I have tried various approaches, such as applying position: sticky to the main toolbar and sidebar. However, there is an issue with different vertical spaces causing the bottom part of the sidebar and its scrollbar to fall out of view. I have considered using flexbox, position:fixed, position:absolute, but none seem to solve the problem.

In addition, I noticed a strange behavior in Chrome when combining position:sticky inside position:fixed.

My objective is to find a solution without relying on JavaScript. Essentially, I need the sidebar's height to adjust based on the sticky state of the toolbar and search box. The top edge of the sidebar should behave like position:sticky, while the bottom edge should act like position:fixed;bottom:0).

Is there a way to accomplish this without JavaScript?

Answer №1

Let me know if I have a good grasp on your situation. First off, my suggestion would be to remove the pre-toolbar bar. Without using JavaScript, achieving the desired behavior is not possible. Since you mentioned that the information in the pre-toolbar is not crucial, it can be placed anywhere.

Alternatively, if you still prefer to keep it at the top, you can make the toolbar and sidebar sticky or use position fixed. Avoid using calc(), opt for setting the height to 100vh, and place it behind the toolbar so it appears shorter. Then, introduce a pre-toolbar on top (z-index) of your toolbar and animate it to disappear after a few seconds.

For reference, here is a link to a codepen: https://codepen.io/oscontrerasn/pen/WNpxJwV

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

Stylesheets per module in Angular 2

For my website design, I've decided to adopt a modular structure using sass. To ensure consistency throughout the module, instead of defining stylesheets at the component level, I plan to define them at the module level and import them into each compo ...

What is the strategy used by jQuery to select elements by class name?

Is there a "getElementsByClass" function in JavaScript? If not, how does jQuery manage to do it without looping through all elements which would be very inefficient? Also, how can I specify CSS for elements with two or more classes? <a class="one two ...

Discovering the screen width and increasing its value using CSS

Is there a way to determine the screen size and then add 253px to it? For example, in CSS: .element { width: calc(100% + 253px); } What would be the best approach to achieve this? ...

When transitioning from the accordion format for mobile to the tab layout for desktop, the tab panel displays an inaccurate title despite the content being accurate

Currently, I am working on developing a navigation system. The objective is to have an accordion-like layout for mobile and tab-based layout for desktop. Although I have implemented this setup, there seems to be an issue: For instance, if "Link #2" tab is ...

Mastering the use of SVG icons in Angular 4+: A comprehensive guide

I have an icon.svg file with a collection of icons that I would like to use in my app, similar to how we display material icons. Does anyone have any ideas on how to include the file in the app and use these icons? I've tried looking for solutions a ...

What is the best way to ensure a consistent spacing between two flex items?

I am working on a project to enhance my knowledge by rebuilding Facebook. One of the new concepts I am learning is Flexbox. When you resize the login page of Facebook, you'll notice that the Navigation shrinks but the logo and login inputs remain sta ...

Tips for arranging the items within the slider according to the specified direction (rtl, ltr) in the Owl Carousel slider for Angular

Is there a way to dynamically change the direction of the owl-carousel-o angular slider based on the selected language? Currently, I have set rtl: true in the owl-carousel configuration during initialization. However, when the user switches to a different ...

Trying out basic form validation - Adjusting border color for an empty input

Currently, I am experimenting with a basic login form using PHP and testing it out on XAMPP in Chrome. Below is the code for the login form: <form action="login.php" method="POST"> <label>User: </label> <i ...

Android Font Icon Spacing Issue with IconFont (Fontastic)

After using Fontastic.me to create an iconfont, I encountered an issue specifically with the native browser of Android 4.2.2 and 4.3 (such as modern Samsung tablets). In these browsers, the characters in the entire font appear to have no width. This proble ...

issues stemming from the css of leaflets

This is my first time using leaflet and I am encountering a CSS issue. I'm not sure if there is another CSS file for leaflet, but whenever I try to adjust its position, it changes unexpectedly. I have a floating nav bar that should appear when scroll ...

What steps can be taken to modify the style of a single button belonging to a broader group of elements?

Is there a way to hide the save button within a Vue Modal without affecting other buttons with the same class? .btn.btn-primary { display: none; } Simply targeting .btn-primary in the CSS affects all elements with that class, and adding .modal woul ...

Persistent error caused by unresponsive Tailwind utility functions

Currently, I am working on a Next.js application and encountered a strange error while adjusting the styling. The error message points to a module that seems to be missing... User Import trace for requested module: ./src/app/globals.css GET /portraits 500 ...

Can you explain the function of the forward slash in the HTML img tag?

Currently, I am in the process of learning html/css by following the tutorial provided by W3Schools.com. The part of the code that is causing me some difficulty can be found at http://www.w3schools.com/css/tryit.asp?filename=trycss_vertical-align Specific ...

Determining the height of a Bootstrap column in relation to another element

Utilizing the grid layout from bootstrap, I have the following structure: <div id="prof_cont_enclose"> <div class="row"> <div class="prof_cont_row"> <div class="col-xs-12 col-sm-4 col-md-2 col-lg-2 prof_elem"&g ...

jQuery Accordion not showing up on screen

On my FAQ page, I have integrated a jQuery Accordion with a repeater control nested inside to display various questions and answers. The FAQ page utilizes a master page named LaunchPage.Master. Here is the code for LaunchPage.Master: <html> <he ...

Adjusting the border size of an SVG without using the viewbox

Currently, I am working with 2 SVGs where one has a viewbox and the other doesn't. They both are set to be 100% width of the parent element. The first SVG scales according to its viewbox while the second one scales according to the height of the paren ...

Is there a way to customize the default Bootstrap accordion button?

Looking for assistance on how to customize the default collapse button for Bootstrap 4 or 5 accordion using fontawesome icons. Can anyone help with this? Thanks in advance! Here is an example of an accordion: <div class="accordion" id="a ...

"Navigate through the page by scrolling with your mouse all the way to 100

Is it possible to trigger an action when a user starts scrolling using the mousewheel? I've searched online but all I found was information on 100% scroll on click. I want the window to automatically be scrolled down to 100% as soon as the user starts ...

javascript, issues with floating and displaying elements

I am currently working on creating a JavaScript menu that has a click function to smoothly slide up and down. Although the JavaScript code appears to be functioning correctly, it seems that there is some interference with the CSS. Despite attempting vario ...

The child fieldset is oversized with a height of 100%

I am attempting to make a fieldset child stretch to 100%, but the child (ul) is too large and causing overflow (getting cut off in my scenario). Is there a way to stretch the fieldset child to 100% without causing overflow? fieldset { height: 300px; ...