The sticky navbar fails to stay in place when the content becomes too lengthy

This is my current state of code (minified, for explanation only)

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="index.css">
</head>
<body>
    <div class="navbar-wrapper">
        <nav class="navbar">
            <h1 class="navbar-logo">
                Logo
            </h1>
            <div class="burger">
                Menu
            </div>
        </nav>
        <div class="menu">
            <div>Item 1</div>
            [There may be a lot of items in here, which
             may cause an overflow]
        </div>
    </div>

    <main>
        This is the main content of this
        <br>
        <b>Website</b>
        [This content may be very long or very small]
    </main>

    <script defer>
        const burger = document.querySelector(".burger");
        const menu = document.querySelector(".menu");

        burger.addEventListener("click", () => {
            if (menu.classList.contains("hidden")) {
                menu.classList.remove("hidden");
            }
            else {
                menu.classList.add("hidden");
            }
        });
    </script>
</body>
</html>

and these are my styles:

index.css

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

body {
  height: 100vh;
}

main {
  background-color: lightblue;

  min-height: 95vh;
}

.navbar-wrapper {
  position: sticky;
  top: 0;
}

.navbar {
  display: flex;
  background-color: gray;
  align-items: center;

  min-height: 5vh;
}

.navbar-logo {
  margin-right: auto;
}

.burger {
  font-size: 2em;
  cursor: pointer;
}

.menu {
  background-color: lightgreen;
  position: absolute;
  right: 0;
  width: 200px;
  padding: 10px;

  max-height: 95vh;

  overflow-y: scroll;
}

.hidden {
  display: none;
}

My Navbar works as expected, my Menu works as expected, and my main content works quite good aswell.

However, when the content inside <main> gets too long, the navbar wont stick anymore, neither does the menu. This is what it looks like with short main content: https://i.sstatic.net/ntg6W.png

And this is how it looks when main content gets too long (it should rather be like the first picture, so that you can still scroll through the main content, but the navbar and menu should be sticky and not move)

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

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

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

It is especially weird, because it works perfectly up to a specific point (I believe when you scroll to half of the page), where it breaks.

Does anyone have a Solution to this? Or if there is another better approach, I would love to hear it!

[As a sidenote, this is a React Project with NextJS. I just wrote it in HTML and CSS for better understanding]

Answer №1

To achieve the desired appearance, simply modify the navbar and menu to have a fixed position.

Check out the code sandbox here

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

After applying the cellClass in ng-grid, the row border mysteriously disappears

After applying cellClass to color an entire column, the row border disappears. Here is a link to the Plunker demonstration: http://plnkr.co/edit/4IeQQBTIe8sgHVEBUTAp?p=preview. Does anyone have a solution for this issue? ...

display the HTML form when the page is clicked

Trying to implement an onclick method for selecting form types. I have multiple form types available - example here Clicking on one submenu should open up the desired form type directly below the menu How can I dynamically load a form based on the select ...

Creating line breaks in Bootstrap input group formatting

My issue involves rows with checkboxes and labels, some of which are longer than others. When the browser is resized or viewed on a mobile device, the columns containing longer labels collapse to a second row while shorter labels stay beside their checkbox ...

Successfully changing the source and tracking of a video through an onclick button, but the video content remains unchanged

I apologize if this post is a duplicate, but I couldn't find the answer in previous threads. I'm attempting to change the video source using an onclick button. However, even after changing the source, the video remains the same. <video width ...

The CSV/PDF print export feature in Material-UI is not functioning properly when using the "@mui/x-data-grid" module

Is it just me or am I missing something here? I tried to export my data as a PDF using the available export option, but all I see is the CSV option. Why is that? https://i.stack.imgur.com/j6LW6.png In their example, they have access to both CSV and Print ...

Ways to stop a tag from causing disruption to the alignment of another

In a div with text-align: center; applied, I have two tags. My issue is that I want one of them to be centered on the same line, while the other is positioned to the right. However, both tags are currently sharing the centered space and extending equally t ...

What is the best method for displaying a wbmp image on an HTML page?

In my recent HTML file, I included the following code: <!DOCTYPE html> <html> <body> <img src="thieftitle.wbmp" alt="W3Schools.com" width="104" height="142"> </body> </html> t ...

Ways to display a GIF image as a pop-up during data loading in an MVC application

I am working on a project using the MVC framework. The application retrieves a large amount of data during loading. I would like to display a loading image - a .gif file while fetching records. Below is the code snippet I am currently using: //Loads rec ...

Ways to modify the screen when a click event occurs

To make the display block when clicking this icon: <div class="index-navi-frame-box"> <p onclick="expandFunction()"> <i class="fa fa-bars"></i> </p> </div> Here is what it should change to: <div class=" ...

Instructions for triggering a function upon an onClick event in ReactJs

I have a handleClick event that should run the renderDetail function in the {this.state.ShowInfo[i]} div when clicked. I have implemented it as shown below, but using callbacks did not work. class App extends React.Component { constructor(props) { super ...

Only a select few expandable elements in the jQuery accordion

How can I create an accordion menu with only a few expandable options? I am looking to include the following items in my menu: Home, Support, Sales, Other The "Home" option is just a link without any sub-menu. Clicking on it should direct users to a spec ...

Implementing Content-Security-Policy for a web application embedded in an iframe

Hey there! I've got this cool webapp called myApp, developed using Spring Boot and Vaadin. It's going to be deployed on a Tomcat server at http://tomcatserver:8080/myApp Now, what I want to do is display the webapp in an iframe like this: <if ...

Experiencing an issue with react-bootstrap when running npm start

Recently, I encountered some issues while running a react-js app. However, there were no problems during the npm install process. Package Versions: react: "^16.10.2" react-bootstrap: "^1.0.0-beta.16" I attempted the following steps to resolve the issues ...

What is the best way to expand and collapse a mat-expansion-panel using a button click

Is it possible to expand a specific mat-expansion-panel by clicking an external button? I've attempted linking to the ID of the panel, but haven't had any luck... <mat-expansion-panel id="panel1"> ... </> ... <button (click)="doc ...

jQuery is optimized to work specifically with select id tags

Here is the HTML code snippet I've put together, along with my script. While I admit it might look a bit messy, please bear with me as I'm still in the learning phase. If anyone could offer some assistance on this matter, I would be extremely gra ...

Tips for updating the CSS properties of the "html" element

html { width:100%; } Looking to dynamically update the CSS of the html tag upon button click using JavaScript. The goal is to modify the existing HTML CSS code as shown below, but achieving this dynamically with a script. Is it possible? html { w ...

Calculate the total sum of all the span elements using jQuery

I am currently working with a more simplified calculator script that looks like this: $('input').keyup(function () { // run whenever the value changes var firstValue = parseFloat($('#first').val()) || 0; // retrieve field value var sec ...

Tips for detecting the existence of a different class within a div/ul through jquery or javascript?

This is how I have my unordered list, or "ul" element, styled. As you can observe, there are two classes defined for the ul <ul class="nav-second-level collapse"> </ul> There might be an additional class added to the same ul like so: <u ...

Insert an element at the start of a sorted array object

I am currently working on a small application that has the following structure: Posts -> Post -> Comments -> Comment. The Posts component sends a request for an array of posts sorted by date in descending order. Each post also fetches its comment ...

Tips for creating a div element with a header and scrollable section

Greetings! I am currently developing a code to manage a sprinkler system interface with a modern and visually appealing design. At the moment, I have two columns on the page, each containing boxes. Unfortunately, the alignment of these boxes is not as desi ...